blob: de6bae28333884d61c32c2b8ee980abcb3827a31 [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
Jerry Yud0766ec2022-09-22 10:46:57 +0800677
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200678}
679
Hanno Beckera18d1322018-01-03 14:27:32 +0000680void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200683
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100684#if defined(MBEDTLS_USE_PSA_CRYPTO)
685 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
686 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100687#else
688 mbedtls_cipher_init( &transform->cipher_ctx_enc );
689 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100690#endif
691
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000692#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100693#if defined(MBEDTLS_USE_PSA_CRYPTO)
694 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
695 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100696#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_md_init( &transform->md_ctx_enc );
698 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000699#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100700#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200701}
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200706}
707
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200708MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000710{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200711 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000712 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200714 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200717 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718
719 /*
720 * Either the pointers are now NULL or cleared properly and can be freed.
721 * Now allocate missing structures.
722 */
723 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200724 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200725 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200726 }
Paul Bakker48916f92012-09-16 19:57:18 +0000727
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200729 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200730 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200731 }
Paul Bakker48916f92012-09-16 19:57:18 +0000732
Paul Bakker82788fb2014-10-20 13:59:19 +0200733 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200734 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200735 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500737#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
738 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400739
Andrzej Kurek4a063792020-10-21 15:08:44 +0200740 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
741 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500742#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000743
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200744 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000745 if( ssl->handshake == NULL ||
746 ssl->transform_negotiate == NULL ||
747 ssl->session_negotiate == NULL )
748 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_free( ssl->handshake );
752 mbedtls_free( ssl->transform_negotiate );
753 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200754
755 ssl->handshake = NULL;
756 ssl->transform_negotiate = NULL;
757 ssl->session_negotiate = NULL;
758
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200759 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000760 }
761
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200762 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000764 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200765 ssl_handshake_params_init( ssl->handshake );
766
Jerry Yud0766ec2022-09-22 10:46:57 +0800767#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
768 defined(MBEDTLS_SSL_SRV_C) && \
769 defined(MBEDTLS_SSL_SESSION_TICKETS)
770 ssl->handshake->new_session_tickets_count =
771 ssl->conf->new_session_tickets_count ;
772#endif
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200775 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
776 {
777 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200778
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200779 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
780 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
781 else
782 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200783
Hanno Becker0f57a652020-02-05 10:37:26 +0000784 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200785 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200786#endif
787
Brett Warrene0edc842021-08-17 09:53:13 +0100788/*
789 * curve_list is translated to IANA TLS group identifiers here because
790 * mbedtls_ssl_conf_curves returns void and so can't return
791 * any error codes.
792 */
793#if defined(MBEDTLS_ECP_C)
794#if !defined(MBEDTLS_DEPRECATED_REMOVED)
795 /* Heap allocate and translate curve_list from internal to IANA group ids */
796 if ( ssl->conf->curve_list != NULL )
797 {
798 size_t length;
799 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
800
801 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
802 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
803
804 /* Leave room for zero termination */
805 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
806 if ( group_list == NULL )
807 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
808
809 for( size_t i = 0; i < length; i++ )
810 {
811 const mbedtls_ecp_curve_info *info =
812 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
813 if ( info == NULL )
814 {
815 mbedtls_free( group_list );
816 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
817 }
818 group_list[i] = info->tls_id;
819 }
820
821 group_list[length] = 0;
822
823 ssl->handshake->group_list = group_list;
824 ssl->handshake->group_list_heap_allocated = 1;
825 }
826 else
827 {
828 ssl->handshake->group_list = ssl->conf->group_list;
829 ssl->handshake->group_list_heap_allocated = 0;
830 }
831#endif /* MBEDTLS_DEPRECATED_REMOVED */
832#endif /* MBEDTLS_ECP_C */
833
Jerry Yuf017ee42022-01-12 15:49:48 +0800834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
835#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800836#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800837 /* Heap allocate and translate sig_hashes from internal hash identifiers to
838 signature algorithms IANA identifiers. */
839 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800840 ssl->conf->sig_hashes != NULL )
841 {
842 const int *md;
843 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800844 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800845 uint16_t *p;
846
Jerry Yub476a442022-01-21 18:14:45 +0800847#if defined(static_assert)
848 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
849 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
850 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800851#endif
852
Jerry Yuf017ee42022-01-12 15:49:48 +0800853 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
854 {
855 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
856 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800857#if defined(MBEDTLS_ECDSA_C)
858 sig_algs_len += sizeof( uint16_t );
859#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800860
Jerry Yub476a442022-01-21 18:14:45 +0800861#if defined(MBEDTLS_RSA_C)
862 sig_algs_len += sizeof( uint16_t );
863#endif
864 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
865 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800866 }
867
Jerry Yub476a442022-01-21 18:14:45 +0800868 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800869 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
870
Jerry Yub476a442022-01-21 18:14:45 +0800871 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
872 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800873 if( ssl->handshake->sig_algs == NULL )
874 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
875
876 p = (uint16_t *)ssl->handshake->sig_algs;
877 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
878 {
879 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
880 if( hash == MBEDTLS_SSL_HASH_NONE )
881 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800882#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800883 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
884 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800885#endif
886#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800887 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
888 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800889#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800890 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200891 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800892 ssl->handshake->sig_algs_heap_allocated = 1;
893 }
894 else
Jerry Yua69269a2022-01-17 21:06:01 +0800895#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800897 ssl->handshake->sig_algs_heap_allocated = 0;
898 }
Jerry Yucc539102022-06-27 16:27:35 +0800899#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800900#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000901 return( 0 );
902}
903
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200904#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200905/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200906MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200907static int ssl_cookie_write_dummy( void *ctx,
908 unsigned char **p, unsigned char *end,
909 const unsigned char *cli_id, size_t cli_id_len )
910{
911 ((void) ctx);
912 ((void) p);
913 ((void) end);
914 ((void) cli_id);
915 ((void) cli_id_len);
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200918}
919
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200920MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200921static int ssl_cookie_check_dummy( void *ctx,
922 const unsigned char *cookie, size_t cookie_len,
923 const unsigned char *cli_id, size_t cli_id_len )
924{
925 ((void) ctx);
926 ((void) cookie);
927 ((void) cookie_len);
928 ((void) cli_id);
929 ((void) cli_id_len);
930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200932}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200933#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200934
Paul Bakker5121ce52009-01-03 21:22:43 +0000935/*
936 * Initialize an SSL context
937 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200938void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
939{
940 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
941}
942
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800944static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
945{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100946 const mbedtls_ssl_config *conf = ssl->conf;
947
Ronald Cron6f135e12021-12-08 16:57:54 +0100948#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100949 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
950 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000951 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
952 {
953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
954 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
955 }
956
Jerry Yu60835a82021-08-04 10:13:52 +0800957 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
958 return( 0 );
959 }
960#endif
961
962#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100963 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800964 {
965 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
966 return( 0 );
967 }
968#endif
969
Ronald Cron6f135e12021-12-08 16:57:54 +0100970#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100971 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800972 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000973 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
974 {
975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
976 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
977 }
978
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000979 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
980 {
981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
982 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
983 }
984
985
Ronald Crone1d3f062022-02-10 14:50:54 +0100986 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
987 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800988 }
989#endif
990
991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
992 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
993}
994
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200995MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800996static int ssl_conf_check(const mbedtls_ssl_context *ssl)
997{
998 int ret;
999 ret = ssl_conf_version_check( ssl );
1000 if( ret != 0 )
1001 return( ret );
1002
1003 /* Space for further checks */
1004
1005 return( 0 );
1006}
1007
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001008/*
1009 * Setup an SSL context
1010 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001011
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001012int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001013 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014{
Janos Follath865b3eb2019-12-16 11:46:15 +00001015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001016 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1017 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001019 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001020
Jerry Yu60835a82021-08-04 10:13:52 +08001021 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1022 return( ret );
1023
Paul Bakker62f2dee2012-09-28 07:31:51 +00001024 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001025 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001026 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001027
1028 /* Set to NULL in case of an error condition */
1029 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001030
Darryl Greenb33cc762019-11-28 14:29:44 +00001031#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1032 ssl->in_buf_len = in_buf_len;
1033#endif
1034 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001035 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001038 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001039 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001040 }
1041
Darryl Greenb33cc762019-11-28 14:29:44 +00001042#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1043 ssl->out_buf_len = out_buf_len;
1044#endif
1045 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001046 if( ssl->out_buf == NULL )
1047 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001049 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001050 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 }
1052
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001053 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001054
Johan Pascalb62bb512015-12-03 21:56:45 +01001055#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001056 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001057#endif
1058
Paul Bakker48916f92012-09-16 19:57:18 +00001059 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001060 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001063
1064error:
1065 mbedtls_free( ssl->in_buf );
1066 mbedtls_free( ssl->out_buf );
1067
1068 ssl->conf = NULL;
1069
Darryl Greenb33cc762019-11-28 14:29:44 +00001070#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1071 ssl->in_buf_len = 0;
1072 ssl->out_buf_len = 0;
1073#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001074 ssl->in_buf = NULL;
1075 ssl->out_buf = NULL;
1076
1077 ssl->in_hdr = NULL;
1078 ssl->in_ctr = NULL;
1079 ssl->in_len = NULL;
1080 ssl->in_iv = NULL;
1081 ssl->in_msg = NULL;
1082
1083 ssl->out_hdr = NULL;
1084 ssl->out_ctr = NULL;
1085 ssl->out_len = NULL;
1086 ssl->out_iv = NULL;
1087 ssl->out_msg = NULL;
1088
k-stachowiak9f7798e2018-07-31 16:52:32 +02001089 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090}
1091
1092/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001093 * Reset an initialized and used SSL context for re-use while retaining
1094 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001095 *
1096 * If partial is non-zero, keep data in the input buffer and client ID.
1097 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001098 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001099void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1100 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001101{
Darryl Greenb33cc762019-11-28 14:29:44 +00001102#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1103 size_t in_buf_len = ssl->in_buf_len;
1104 size_t out_buf_len = ssl->out_buf_len;
1105#else
1106 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1107 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1108#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001109
Hanno Beckerb0302c42021-08-03 09:39:42 +01001110#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1111 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001112#endif
1113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001114 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001115 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001116
Hanno Beckerb0302c42021-08-03 09:39:42 +01001117 mbedtls_ssl_reset_in_out_pointers( ssl );
1118
1119 /* Reset incoming message parsing */
1120 ssl->in_offt = NULL;
1121 ssl->nb_zero = 0;
1122 ssl->in_msgtype = 0;
1123 ssl->in_msglen = 0;
1124 ssl->in_hslen = 0;
1125 ssl->keep_current_message = 0;
1126 ssl->transform_in = NULL;
1127
1128#if defined(MBEDTLS_SSL_PROTO_DTLS)
1129 ssl->next_record_offset = 0;
1130 ssl->in_epoch = 0;
1131#endif
1132
1133 /* Keep current datagram if partial == 1 */
1134 if( partial == 0 )
1135 {
1136 ssl->in_left = 0;
1137 memset( ssl->in_buf, 0, in_buf_len );
1138 }
1139
Ronald Cronad8c17b2022-06-10 17:18:09 +02001140 ssl->send_alert = 0;
1141
Hanno Beckerb0302c42021-08-03 09:39:42 +01001142 /* Reset outgoing message writing */
1143 ssl->out_msgtype = 0;
1144 ssl->out_msglen = 0;
1145 ssl->out_left = 0;
1146 memset( ssl->out_buf, 0, out_buf_len );
1147 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1148 ssl->transform_out = NULL;
1149
1150#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1151 mbedtls_ssl_dtls_replay_reset( ssl );
1152#endif
1153
XiaokangQian2b01dc32022-01-21 02:53:13 +00001154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001155 if( ssl->transform )
1156 {
1157 mbedtls_ssl_transform_free( ssl->transform );
1158 mbedtls_free( ssl->transform );
1159 ssl->transform = NULL;
1160 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1162
XiaokangQian2b01dc32022-01-21 02:53:13 +00001163#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1164 mbedtls_ssl_transform_free( ssl->transform_application );
1165 mbedtls_free( ssl->transform_application );
1166 ssl->transform_application = NULL;
1167
1168 if( ssl->handshake != NULL )
1169 {
1170 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1171 mbedtls_free( ssl->handshake->transform_earlydata );
1172 ssl->handshake->transform_earlydata = NULL;
1173
1174 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1175 mbedtls_free( ssl->handshake->transform_handshake );
1176 ssl->handshake->transform_handshake = NULL;
1177 }
1178
XiaokangQian2b01dc32022-01-21 02:53:13 +00001179#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001180}
1181
1182int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1183{
1184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1185
1186 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1187
XiaokangQian78b1fa72022-01-19 06:56:30 +00001188 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001189
1190 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_RENEGOTIATION)
1192 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001193 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001194
1195 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1197 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001200
Hanno Beckerb0302c42021-08-03 09:39:42 +01001201 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001202 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001203 if( ssl->session )
1204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_ssl_session_free( ssl->session );
1206 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001207 ssl->session = NULL;
1208 }
1209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001211 ssl->alpn_chosen = NULL;
1212#endif
1213
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001214#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001215#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001216 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001217#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001218 {
1219 mbedtls_free( ssl->cli_id );
1220 ssl->cli_id = NULL;
1221 ssl->cli_id_len = 0;
1222 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001223#endif
1224
Paul Bakker48916f92012-09-16 19:57:18 +00001225 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1226 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001227
1228 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001229}
1230
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001231/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001232 * Reset an initialized and used SSL context for re-use while retaining
1233 * all application-set variables, function pointers and data.
1234 */
1235int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1236{
Hanno Becker43aefe22020-02-05 10:44:56 +00001237 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001238}
1239
1240/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 * SSL set accessors
1242 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001243void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001245 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246}
1247
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001248void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001250 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001251}
1252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001254void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001256 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001257}
1258#endif
1259
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001260void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001261{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001262 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001263}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001266
Hanno Becker1841b0a2018-08-24 11:13:57 +01001267void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1268 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001269{
1270 ssl->disable_datagram_packing = !allow_packing;
1271}
1272
1273void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1274 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001276 conf->hs_timeout_min = min;
1277 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001278}
1279#endif
1280
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001281void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001283 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284}
1285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001287void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001288 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001289 void *p_vrfy )
1290{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001291 conf->f_vrfy = f_vrfy;
1292 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001295
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001296void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001297 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 void *p_rng )
1299{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001300 conf->f_rng = f_rng;
1301 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001302}
1303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001304void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001305 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 void *p_dbg )
1307{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001308 conf->f_dbg = f_dbg;
1309 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310}
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001313 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001314 mbedtls_ssl_send_t *f_send,
1315 mbedtls_ssl_recv_t *f_recv,
1316 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001317{
1318 ssl->p_bio = p_bio;
1319 ssl->f_send = f_send;
1320 ssl->f_recv = f_recv;
1321 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001322}
1323
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001324#if defined(MBEDTLS_SSL_PROTO_DTLS)
1325void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1326{
1327 ssl->mtu = mtu;
1328}
1329#endif
1330
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001331void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001332{
1333 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001334}
1335
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001336void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1337 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001338 mbedtls_ssl_set_timer_t *f_set_timer,
1339 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001340{
1341 ssl->p_timer = p_timer;
1342 ssl->f_set_timer = f_set_timer;
1343 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001344
1345 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001346 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001347}
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001350void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001351 void *p_cache,
1352 mbedtls_ssl_cache_get_t *f_get_cache,
1353 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001355 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001356 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001357 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_SSL_CLI_C)
1362int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363{
Janos Follath865b3eb2019-12-16 11:46:15 +00001364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001365
1366 if( ssl == NULL ||
1367 session == NULL ||
1368 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001369 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001372 }
1373
Hanno Beckere810bbc2021-05-14 16:01:05 +01001374 if( ssl->handshake->resume == 1 )
1375 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1376
Hanno Becker52055ae2019-02-06 14:30:46 +00001377 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1378 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001379 return( ret );
1380
Paul Bakker0a597072012-09-25 21:55:46 +00001381 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001382
1383 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001387void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1388 const int *ciphersuites )
1389{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001390 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001391}
1392
Ronald Cron6f135e12021-12-08 16:57:54 +01001393#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001394void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001395 const int kex_modes )
1396{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001397 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001398}
Ronald Cron6f135e12021-12-08 16:57:54 +01001399#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001402void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001403 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001404{
1405 conf->cert_profile = profile;
1406}
1407
Glenn Strauss36872db2022-01-22 05:06:31 -05001408static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1409{
1410 mbedtls_ssl_key_cert *cur = key_cert, *next;
1411
1412 while( cur != NULL )
1413 {
1414 next = cur->next;
1415 mbedtls_free( cur );
1416 cur = next;
1417 }
1418}
1419
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001420/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001421MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001422static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1423 mbedtls_x509_crt *cert,
1424 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001425{
niisato8ee24222018-06-25 19:05:48 +09001426 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001427
Glenn Strauss36872db2022-01-22 05:06:31 -05001428 if( cert == NULL )
1429 {
1430 /* Free list if cert is null */
1431 ssl_key_cert_free( *head );
1432 *head = NULL;
1433 return( 0 );
1434 }
1435
niisato8ee24222018-06-25 19:05:48 +09001436 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1437 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001438 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001439
niisato8ee24222018-06-25 19:05:48 +09001440 new_cert->cert = cert;
1441 new_cert->key = key;
1442 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001443
Glenn Strauss36872db2022-01-22 05:06:31 -05001444 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001445 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001446 {
niisato8ee24222018-06-25 19:05:48 +09001447 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001448 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001449 else
1450 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001451 mbedtls_ssl_key_cert *cur = *head;
1452 while( cur->next != NULL )
1453 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001454 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001455 }
1456
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001457 return( 0 );
1458}
1459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001460int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001461 mbedtls_x509_crt *own_cert,
1462 mbedtls_pk_context *pk_key )
1463{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001464 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001465}
1466
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001467void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001468 mbedtls_x509_crt *ca_chain,
1469 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001470{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001471 conf->ca_chain = ca_chain;
1472 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001473
1474#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1475 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1476 * cannot be used together. */
1477 conf->f_ca_cb = NULL;
1478 conf->p_ca_cb = NULL;
1479#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001480}
Hanno Becker5adaad92019-03-27 16:54:37 +00001481
1482#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1483void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001484 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001485 void *p_ca_cb )
1486{
1487 conf->f_ca_cb = f_ca_cb;
1488 conf->p_ca_cb = p_ca_cb;
1489
1490 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1491 * cannot be used together. */
1492 conf->ca_chain = NULL;
1493 conf->ca_crl = NULL;
1494}
1495#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001497
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001498#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001499const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1500 size_t *name_len )
1501{
1502 *name_len = ssl->handshake->sni_name_len;
1503 return( ssl->handshake->sni_name );
1504}
1505
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001506int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1507 mbedtls_x509_crt *own_cert,
1508 mbedtls_pk_context *pk_key )
1509{
1510 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1511 own_cert, pk_key ) );
1512}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001513
1514void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1515 mbedtls_x509_crt *ca_chain,
1516 mbedtls_x509_crl *ca_crl )
1517{
1518 ssl->handshake->sni_ca_chain = ca_chain;
1519 ssl->handshake->sni_ca_crl = ca_crl;
1520}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001521
Glenn Strauss999ef702022-03-11 01:37:23 -05001522#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1523void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1524 const mbedtls_x509_crt *crt)
1525{
1526 ssl->handshake->dn_hints = crt;
1527}
1528#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1529
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001530void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1531 int authmode )
1532{
1533 ssl->handshake->sni_authmode = authmode;
1534}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001535#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1536
Hanno Becker8927c832019-04-03 12:52:50 +01001537#if defined(MBEDTLS_X509_CRT_PARSE_C)
1538void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1539 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1540 void *p_vrfy )
1541{
1542 ssl->f_vrfy = f_vrfy;
1543 ssl->p_vrfy = p_vrfy;
1544}
1545#endif
1546
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001547#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001548/*
1549 * Set EC J-PAKE password for current handshake
1550 */
1551int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1552 const unsigned char *pw,
1553 size_t pw_len )
1554{
1555 mbedtls_ecjpake_role role;
1556
Janos Follath8eb64132016-06-03 15:40:57 +01001557 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1559
1560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1561 role = MBEDTLS_ECJPAKE_SERVER;
1562 else
1563 role = MBEDTLS_ECJPAKE_CLIENT;
1564
1565 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1566 role,
1567 MBEDTLS_MD_SHA256,
1568 MBEDTLS_ECP_DP_SECP256R1,
1569 pw, pw_len ) );
1570}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001571#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001572
Gilles Peskineeccd8882020-03-10 12:19:08 +01001573#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001574
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001575MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001576static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1577{
1578#if defined(MBEDTLS_USE_PSA_CRYPTO)
1579 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1580 return( 1 );
1581#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001582 if( conf->psk != NULL )
1583 return( 1 );
1584
1585 return( 0 );
1586}
1587
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001588static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1589{
1590 /* Remove reference to existing PSK, if any. */
1591#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001592 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001593 {
1594 /* The maintenance of the PSK key slot is the
1595 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001596 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001597 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001598#endif /* MBEDTLS_USE_PSA_CRYPTO */
1599 if( conf->psk != NULL )
1600 {
1601 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1602
1603 mbedtls_free( conf->psk );
1604 conf->psk = NULL;
1605 conf->psk_len = 0;
1606 }
1607
1608 /* Remove reference to PSK identity, if any. */
1609 if( conf->psk_identity != NULL )
1610 {
1611 mbedtls_free( conf->psk_identity );
1612 conf->psk_identity = NULL;
1613 conf->psk_identity_len = 0;
1614 }
1615}
1616
Hanno Becker7390c712018-11-15 13:33:04 +00001617/* This function assumes that PSK identity in the SSL config is unset.
1618 * It checks that the provided identity is well-formed and attempts
1619 * to make a copy of it in the SSL config.
1620 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001621MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001622static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1623 unsigned char const *psk_identity,
1624 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001625{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001626 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001627 if( psk_identity == NULL ||
1628 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001629 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001630 {
1631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1632 }
1633
Hanno Becker7390c712018-11-15 13:33:04 +00001634 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1635 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001636 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001637
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001638 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001639 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001640
1641 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001642}
1643
Hanno Becker7390c712018-11-15 13:33:04 +00001644int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1645 const unsigned char *psk, size_t psk_len,
1646 const unsigned char *psk_identity, size_t psk_identity_len )
1647{
Janos Follath865b3eb2019-12-16 11:46:15 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001649
1650 /* We currently only support one PSK, raw or opaque. */
1651 if( ssl_conf_psk_is_configured( conf ) )
1652 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001653
1654 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001655 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001657 if( psk_len == 0 )
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1661
Hanno Becker7390c712018-11-15 13:33:04 +00001662 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1664 conf->psk_len = psk_len;
1665 memcpy( conf->psk, psk, conf->psk_len );
1666
1667 /* Check and set PSK Identity */
1668 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1669 if( ret != 0 )
1670 ssl_conf_remove_psk( conf );
1671
1672 return( ret );
1673}
1674
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001675static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1676{
1677#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001678 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001679 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001680 /* The maintenance of the external PSK key slot is the
1681 * user's responsibility. */
1682 if( ssl->handshake->psk_opaque_is_internal )
1683 {
1684 psa_destroy_key( ssl->handshake->psk_opaque );
1685 ssl->handshake->psk_opaque_is_internal = 0;
1686 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001687 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001688 }
Neil Armstronge952a302022-05-03 10:22:14 +02001689#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001690 if( ssl->handshake->psk != NULL )
1691 {
1692 mbedtls_platform_zeroize( ssl->handshake->psk,
1693 ssl->handshake->psk_len );
1694 mbedtls_free( ssl->handshake->psk );
1695 ssl->handshake->psk_len = 0;
1696 }
Neil Armstronge952a302022-05-03 10:22:14 +02001697#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001698}
1699
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001700int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1701 const unsigned char *psk, size_t psk_len )
1702{
Neil Armstrong501c9322022-05-03 09:35:09 +02001703#if defined(MBEDTLS_USE_PSA_CRYPTO)
1704 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001705 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001706 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001708#endif /* MBEDTLS_USE_PSA_CRYPTO */
1709
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001710 if( psk == NULL || ssl->handshake == NULL )
1711 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1712
1713 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1715
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001716 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001717
Neil Armstrong501c9322022-05-03 09:35:09 +02001718#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001719#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1720 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1721 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001722 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001723 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1724 else
1725 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1726 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1727 }
1728#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001729
Jerry Yu568ec252022-07-22 21:27:34 +08001730#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001731 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1732 {
1733 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1734 psa_set_key_usage_flags( &key_attributes,
1735 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1736 }
1737#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1738
Neil Armstrong501c9322022-05-03 09:35:09 +02001739 psa_set_key_algorithm( &key_attributes, alg );
1740 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1741
1742 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1743 if( status != PSA_SUCCESS )
1744 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1745
1746 /* Allow calling psa_destroy_key() on psk remove */
1747 ssl->handshake->psk_opaque_is_internal = 1;
1748 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1749#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001750 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001751 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001752
1753 ssl->handshake->psk_len = psk_len;
1754 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1755
1756 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001757#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001758}
1759
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001760#if defined(MBEDTLS_USE_PSA_CRYPTO)
1761int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001762 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001763 const unsigned char *psk_identity,
1764 size_t psk_identity_len )
1765{
Janos Follath865b3eb2019-12-16 11:46:15 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001767
1768 /* We currently only support one PSK, raw or opaque. */
1769 if( ssl_conf_psk_is_configured( conf ) )
1770 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001771
Hanno Becker7390c712018-11-15 13:33:04 +00001772 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001773 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001775 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001776
1777 /* Check and set PSK Identity */
1778 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1779 psk_identity_len );
1780 if( ret != 0 )
1781 ssl_conf_remove_psk( conf );
1782
1783 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001784}
1785
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001786int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1787 mbedtls_svc_key_id_t psk )
1788{
1789 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1790 ( ssl->handshake == NULL ) )
1791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1792
1793 ssl_remove_psk( ssl );
1794 ssl->handshake->psk_opaque = psk;
1795 return( 0 );
1796}
1797#endif /* MBEDTLS_USE_PSA_CRYPTO */
1798
1799void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1800 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1801 size_t),
1802 void *p_psk )
1803{
1804 conf->f_psk = f_psk;
1805 conf->p_psk = p_psk;
1806}
1807#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1808
1809#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001810static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1811 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001812{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001813#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001814 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001815 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001816#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001817 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001818 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001819 return( MBEDTLS_SSL_MODE_STREAM );
1820}
1821
1822#else /* MBEDTLS_USE_PSA_CRYPTO */
1823
1824static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1825 mbedtls_cipher_mode_t mode )
1826{
1827#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1828 if( mode == MBEDTLS_MODE_CBC )
1829 return( MBEDTLS_SSL_MODE_CBC );
1830#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1831
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001832#if defined(MBEDTLS_GCM_C) || \
1833 defined(MBEDTLS_CCM_C) || \
1834 defined(MBEDTLS_CHACHAPOLY_C)
1835 if( mode == MBEDTLS_MODE_GCM ||
1836 mode == MBEDTLS_MODE_CCM ||
1837 mode == MBEDTLS_MODE_CHACHAPOLY )
1838 return( MBEDTLS_SSL_MODE_AEAD );
1839#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001840
1841 return( MBEDTLS_SSL_MODE_STREAM );
1842}
Gilles Peskine301711e2022-04-26 16:57:05 +02001843#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001844
Gilles Peskinee108d982022-04-26 16:50:40 +02001845static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1846 mbedtls_ssl_mode_t base_mode,
1847 int encrypt_then_mac )
1848{
1849#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1850 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1851 base_mode == MBEDTLS_SSL_MODE_CBC )
1852 {
1853 return( MBEDTLS_SSL_MODE_CBC_ETM );
1854 }
1855#else
1856 (void) encrypt_then_mac;
1857#endif
1858 return( base_mode );
1859}
1860
Neil Armstrongab555e02022-04-04 11:07:59 +02001861mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001862 const mbedtls_ssl_transform *transform )
1863{
Gilles Peskinee108d982022-04-26 16:50:40 +02001864 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001865#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001866 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001867#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001868 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1869#endif
1870 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001871
Gilles Peskinee108d982022-04-26 16:50:40 +02001872 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001873#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001874 encrypt_then_mac = transform->encrypt_then_mac;
1875#endif
1876 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001877}
1878
Neil Armstrongab555e02022-04-04 11:07:59 +02001879mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001880#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001881 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001882#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001883 const mbedtls_ssl_ciphersuite_t *suite )
1884{
Gilles Peskinee108d982022-04-26 16:50:40 +02001885 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1886
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001887#if defined(MBEDTLS_USE_PSA_CRYPTO)
1888 psa_status_t status;
1889 psa_algorithm_t alg;
1890 psa_key_type_t type;
1891 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001892 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1893 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001894 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895#else
1896 const mbedtls_cipher_info_t *cipher =
1897 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001898 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001899 {
1900 base_mode =
1901 mbedtls_ssl_get_base_mode(
1902 mbedtls_cipher_info_get_mode( cipher ) );
1903 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001904#endif /* MBEDTLS_USE_PSA_CRYPTO */
1905
Gilles Peskinee108d982022-04-26 16:50:40 +02001906#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1907 int encrypt_then_mac = 0;
1908#endif
1909 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001910}
1911
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001912#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001913
1914#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001915/* Serialization of TLS 1.3 sessions:
1916 *
1917 * struct {
1918 * uint64 ticket_received;
1919 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001920 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001921 * } ClientOnlyData;
1922 *
1923 * struct {
1924 * uint8 endpoint;
1925 * uint8 ciphersuite[2];
1926 * uint32 ticket_age_add;
1927 * uint8 ticket_flags;
1928 * opaque resumption_key<0..255>;
1929 * select ( endpoint ) {
1930 * case client: ClientOnlyData;
1931 * case server: uint64 start_time;
1932 * };
1933 * } serialized_session_tls13;
1934 *
1935 */
1936#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001937MBEDTLS_CHECK_RETURN_CRITICAL
1938static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1939 unsigned char *buf,
1940 size_t buf_len,
1941 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001942{
1943 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001944 size_t needed = 1 /* endpoint */
1945 + 2 /* ciphersuite */
1946 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001947 + 1 /* ticket_flags */
1948 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001949 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001950
1951 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1953 needed += session->resumption_key_len; /* resumption_key */
1954
Jerry Yu438ddd82022-07-07 06:55:50 +00001955#if defined(MBEDTLS_HAVE_TIME)
1956 needed += 8; /* start_time or ticket_received */
1957#endif
1958
1959#if defined(MBEDTLS_SSL_CLI_C)
1960 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1961 {
1962 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001963 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001964
1965 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001966 if( session->ticket_len > SIZE_MAX - needed )
1967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001968
Jerry Yue28d9742022-08-18 15:44:03 +08001969 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001970 }
1971#endif /* MBEDTLS_SSL_CLI_C */
1972
Jerry Yue36fdd62022-08-17 21:31:36 +08001973 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001974 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001975 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001976
1977 p[0] = session->endpoint;
1978 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1979 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1980 p[7] = session->ticket_flags;
1981
1982 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001983 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001984 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001985 memcpy( p, session->resumption_key, session->resumption_key_len );
1986 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001987
1988#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1989 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1990 {
1991 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1992 p += 8;
1993 }
1994#endif /* MBEDTLS_HAVE_TIME */
1995
1996#if defined(MBEDTLS_SSL_CLI_C)
1997 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1998 {
1999#if defined(MBEDTLS_HAVE_TIME)
2000 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2001 p += 8;
2002#endif
2003 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2004 p += 4;
2005
2006 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2007 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002008
2009 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 {
2011 memcpy( p, session->ticket, session->ticket_len );
2012 p += session->ticket_len;
2013 }
2014 }
2015#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002016 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002017}
2018
2019MBEDTLS_CHECK_RETURN_CRITICAL
2020static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2021 const unsigned char *buf,
2022 size_t len )
2023{
2024 const unsigned char *p = buf;
2025 const unsigned char *end = buf + len;
2026
2027 if( end - p < 9 )
2028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2029 session->endpoint = p[0];
2030 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2031 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2032 session->ticket_flags = p[7];
2033
2034 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002035 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002036 p += 9;
2037
Jerry Yubc7c1a42022-07-21 22:57:37 +08002038 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2040
Jerry Yubc7c1a42022-07-21 22:57:37 +08002041 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002043 memcpy( session->resumption_key, p, session->resumption_key_len );
2044 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002045
2046#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2047 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2048 {
2049 if( end - p < 8 )
2050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2051 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2052 p += 8;
2053 }
2054#endif /* MBEDTLS_HAVE_TIME */
2055
2056#if defined(MBEDTLS_SSL_CLI_C)
2057 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2058 {
2059#if defined(MBEDTLS_HAVE_TIME)
2060 if( end - p < 8 )
2061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2062 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2063 p += 8;
2064#endif
2065 if( end - p < 4 )
2066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2067 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2068 p += 4;
2069
2070 if( end - p < 2 )
2071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2072 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2073 p += 2;
2074
2075 if( end - p < ( long int )session->ticket_len )
2076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2077 if( session->ticket_len > 0 )
2078 {
2079 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2080 if( session->ticket == NULL )
2081 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2082 memcpy( session->ticket, p, session->ticket_len );
2083 p += session->ticket_len;
2084 }
2085 }
2086#endif /* MBEDTLS_SSL_CLI_C */
2087
2088 return( 0 );
2089
2090}
2091#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002092MBEDTLS_CHECK_RETURN_CRITICAL
2093static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2094 unsigned char *buf,
2095 size_t buf_len,
2096 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002097{
2098 ((void) session);
2099 ((void) buf);
2100 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002101 *olen = 0;
2102 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002103}
Jerry Yu438ddd82022-07-07 06:55:50 +00002104
2105static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2106 unsigned char *buf,
2107 size_t buf_len )
2108{
2109 ((void) session);
2110 ((void) buf);
2111 ((void) buf_len);
2112 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2113}
2114#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002115#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2116
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002117psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002118 size_t taglen,
2119 psa_algorithm_t *alg,
2120 psa_key_type_t *key_type,
2121 size_t *key_size )
2122{
2123 switch ( mbedtls_cipher_type )
2124 {
2125 case MBEDTLS_CIPHER_AES_128_CBC:
2126 *alg = PSA_ALG_CBC_NO_PADDING;
2127 *key_type = PSA_KEY_TYPE_AES;
2128 *key_size = 128;
2129 break;
2130 case MBEDTLS_CIPHER_AES_128_CCM:
2131 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2132 *key_type = PSA_KEY_TYPE_AES;
2133 *key_size = 128;
2134 break;
2135 case MBEDTLS_CIPHER_AES_128_GCM:
2136 *alg = PSA_ALG_GCM;
2137 *key_type = PSA_KEY_TYPE_AES;
2138 *key_size = 128;
2139 break;
2140 case MBEDTLS_CIPHER_AES_192_CCM:
2141 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2142 *key_type = PSA_KEY_TYPE_AES;
2143 *key_size = 192;
2144 break;
2145 case MBEDTLS_CIPHER_AES_192_GCM:
2146 *alg = PSA_ALG_GCM;
2147 *key_type = PSA_KEY_TYPE_AES;
2148 *key_size = 192;
2149 break;
2150 case MBEDTLS_CIPHER_AES_256_CBC:
2151 *alg = PSA_ALG_CBC_NO_PADDING;
2152 *key_type = PSA_KEY_TYPE_AES;
2153 *key_size = 256;
2154 break;
2155 case MBEDTLS_CIPHER_AES_256_CCM:
2156 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2157 *key_type = PSA_KEY_TYPE_AES;
2158 *key_size = 256;
2159 break;
2160 case MBEDTLS_CIPHER_AES_256_GCM:
2161 *alg = PSA_ALG_GCM;
2162 *key_type = PSA_KEY_TYPE_AES;
2163 *key_size = 256;
2164 break;
2165 case MBEDTLS_CIPHER_ARIA_128_CBC:
2166 *alg = PSA_ALG_CBC_NO_PADDING;
2167 *key_type = PSA_KEY_TYPE_ARIA;
2168 *key_size = 128;
2169 break;
2170 case MBEDTLS_CIPHER_ARIA_128_CCM:
2171 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2172 *key_type = PSA_KEY_TYPE_ARIA;
2173 *key_size = 128;
2174 break;
2175 case MBEDTLS_CIPHER_ARIA_128_GCM:
2176 *alg = PSA_ALG_GCM;
2177 *key_type = PSA_KEY_TYPE_ARIA;
2178 *key_size = 128;
2179 break;
2180 case MBEDTLS_CIPHER_ARIA_192_CCM:
2181 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2182 *key_type = PSA_KEY_TYPE_ARIA;
2183 *key_size = 192;
2184 break;
2185 case MBEDTLS_CIPHER_ARIA_192_GCM:
2186 *alg = PSA_ALG_GCM;
2187 *key_type = PSA_KEY_TYPE_ARIA;
2188 *key_size = 192;
2189 break;
2190 case MBEDTLS_CIPHER_ARIA_256_CBC:
2191 *alg = PSA_ALG_CBC_NO_PADDING;
2192 *key_type = PSA_KEY_TYPE_ARIA;
2193 *key_size = 256;
2194 break;
2195 case MBEDTLS_CIPHER_ARIA_256_CCM:
2196 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2197 *key_type = PSA_KEY_TYPE_ARIA;
2198 *key_size = 256;
2199 break;
2200 case MBEDTLS_CIPHER_ARIA_256_GCM:
2201 *alg = PSA_ALG_GCM;
2202 *key_type = PSA_KEY_TYPE_ARIA;
2203 *key_size = 256;
2204 break;
2205 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2206 *alg = PSA_ALG_CBC_NO_PADDING;
2207 *key_type = PSA_KEY_TYPE_CAMELLIA;
2208 *key_size = 128;
2209 break;
2210 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2211 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2212 *key_type = PSA_KEY_TYPE_CAMELLIA;
2213 *key_size = 128;
2214 break;
2215 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2216 *alg = PSA_ALG_GCM;
2217 *key_type = PSA_KEY_TYPE_CAMELLIA;
2218 *key_size = 128;
2219 break;
2220 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2221 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2222 *key_type = PSA_KEY_TYPE_CAMELLIA;
2223 *key_size = 192;
2224 break;
2225 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2226 *alg = PSA_ALG_GCM;
2227 *key_type = PSA_KEY_TYPE_CAMELLIA;
2228 *key_size = 192;
2229 break;
2230 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2231 *alg = PSA_ALG_CBC_NO_PADDING;
2232 *key_type = PSA_KEY_TYPE_CAMELLIA;
2233 *key_size = 256;
2234 break;
2235 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2236 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2237 *key_type = PSA_KEY_TYPE_CAMELLIA;
2238 *key_size = 256;
2239 break;
2240 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2241 *alg = PSA_ALG_GCM;
2242 *key_type = PSA_KEY_TYPE_CAMELLIA;
2243 *key_size = 256;
2244 break;
2245 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2246 *alg = PSA_ALG_CHACHA20_POLY1305;
2247 *key_type = PSA_KEY_TYPE_CHACHA20;
2248 *key_size = 256;
2249 break;
2250 case MBEDTLS_CIPHER_NULL:
2251 *alg = MBEDTLS_SSL_NULL_CIPHER;
2252 *key_type = 0;
2253 *key_size = 0;
2254 break;
2255 default:
2256 return PSA_ERROR_NOT_SUPPORTED;
2257 }
2258
2259 return PSA_SUCCESS;
2260}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002261#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002262
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002263#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002264int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2265 const unsigned char *dhm_P, size_t P_len,
2266 const unsigned char *dhm_G, size_t G_len )
2267{
Janos Follath865b3eb2019-12-16 11:46:15 +00002268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002269
Glenn Strausscee11292021-12-20 01:43:17 -05002270 mbedtls_mpi_free( &conf->dhm_P );
2271 mbedtls_mpi_free( &conf->dhm_G );
2272
Hanno Beckera90658f2017-10-04 15:29:08 +01002273 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2274 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2275 {
2276 mbedtls_mpi_free( &conf->dhm_P );
2277 mbedtls_mpi_free( &conf->dhm_G );
2278 return( ret );
2279 }
2280
2281 return( 0 );
2282}
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002284int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002285{
Janos Follath865b3eb2019-12-16 11:46:15 +00002286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002287
Glenn Strausscee11292021-12-20 01:43:17 -05002288 mbedtls_mpi_free( &conf->dhm_P );
2289 mbedtls_mpi_free( &conf->dhm_G );
2290
Gilles Peskinee5702482021-06-11 21:59:08 +02002291 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2292 &conf->dhm_P ) ) != 0 ||
2293 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2294 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002295 {
2296 mbedtls_mpi_free( &conf->dhm_P );
2297 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002298 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002299 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002300
2301 return( 0 );
2302}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002303#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002304
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002305#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2306/*
2307 * Set the minimum length for Diffie-Hellman parameters
2308 */
2309void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2310 unsigned int bitlen )
2311{
2312 conf->dhm_min_bitlen = bitlen;
2313}
2314#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2315
Gilles Peskineeccd8882020-03-10 12:19:08 +01002316#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002317#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002318/*
2319 * Set allowed/preferred hashes for handshake signatures
2320 */
2321void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2322 const int *hashes )
2323{
2324 conf->sig_hashes = hashes;
2325}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002326#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002327
Jerry Yuf017ee42022-01-12 15:49:48 +08002328/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002329void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2330 const uint16_t* sig_algs )
2331{
Jerry Yuf017ee42022-01-12 15:49:48 +08002332#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2333 conf->sig_hashes = NULL;
2334#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2335 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002336}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002337#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002338
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002339#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002340#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002341/*
2342 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002343 *
2344 * mbedtls_ssl_setup() takes the provided list
2345 * and translates it to a list of IANA TLS group identifiers,
2346 * stored in ssl->handshake->group_list.
2347 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002348 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002349void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002350 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002351{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002352 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002353 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002354}
Brett Warrene0edc842021-08-17 09:53:13 +01002355#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002356#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002357
Brett Warrene0edc842021-08-17 09:53:13 +01002358/*
2359 * Set the allowed groups
2360 */
2361void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2362 const uint16_t *group_list )
2363{
2364#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2365 conf->curve_list = NULL;
2366#endif
2367 conf->group_list = group_list;
2368}
2369
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002370#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002372{
Hanno Becker947194e2017-04-07 13:25:49 +01002373 /* Initialize to suppress unnecessary compiler warning */
2374 size_t hostname_len = 0;
2375
2376 /* Check if new hostname is valid before
2377 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002378 if( hostname != NULL )
2379 {
2380 hostname_len = strlen( hostname );
2381
2382 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2384 }
2385
2386 /* Now it's clear that we will overwrite the old hostname,
2387 * so we can free it safely */
2388
2389 if( ssl->hostname != NULL )
2390 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002391 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002392 mbedtls_free( ssl->hostname );
2393 }
2394
2395 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002396
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002398 {
2399 ssl->hostname = NULL;
2400 }
2401 else
2402 {
2403 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002404 if( ssl->hostname == NULL )
2405 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002406
Hanno Becker947194e2017-04-07 13:25:49 +01002407 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002408
Hanno Becker947194e2017-04-07 13:25:49 +01002409 ssl->hostname[hostname_len] = '\0';
2410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
2412 return( 0 );
2413}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002414#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002416#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002417void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002419 const unsigned char *, size_t),
2420 void *p_sni )
2421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002422 conf->f_sni = f_sni;
2423 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002429{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002430 size_t cur_len, tot_len;
2431 const char **p;
2432
2433 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002434 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2435 * MUST NOT be truncated."
2436 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002437 */
2438 tot_len = 0;
2439 for( p = protos; *p != NULL; p++ )
2440 {
2441 cur_len = strlen( *p );
2442 tot_len += cur_len;
2443
Ronald Cron8216dd32020-04-23 16:41:44 +02002444 if( ( cur_len == 0 ) ||
2445 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2446 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002448 }
2449
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002450 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002451
2452 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002453}
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002456{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002457 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002460
Johan Pascalb62bb512015-12-03 21:56:45 +01002461#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002462void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2463 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002464{
2465 conf->dtls_srtp_mki_support = support_mki_value;
2466}
2467
Ron Eldoref72faf2018-07-12 11:54:20 +03002468int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2469 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002470 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002471{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002472 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002473 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002475 }
Ron Eldor591f1622018-01-22 12:30:04 +02002476
2477 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002478 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002479 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002480 }
Ron Eldor591f1622018-01-22 12:30:04 +02002481
2482 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2483 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002484 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002485}
2486
Ron Eldoref72faf2018-07-12 11:54:20 +03002487int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002488 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002489{
Johan Pascal253d0262020-09-22 13:04:45 +02002490 const mbedtls_ssl_srtp_profile *p;
2491 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002492
Johan Pascal253d0262020-09-22 13:04:45 +02002493 /* check the profiles list: all entry must be valid,
2494 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002495 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2496 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2497 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002498 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002499 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002500 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002501 list_size++;
2502 }
2503 else
2504 {
2505 /* unsupported value, stop parsing and set the size to an error value */
2506 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002507 }
2508 }
2509
Johan Pascal5ef72d22020-10-28 17:05:47 +01002510 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002511 {
Johan Pascal253d0262020-09-22 13:04:45 +02002512 conf->dtls_srtp_profile_list = NULL;
2513 conf->dtls_srtp_profile_list_len = 0;
2514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2515 }
2516
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002517 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002518 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002519
2520 return( 0 );
2521}
2522
Johan Pascal5ef72d22020-10-28 17:05:47 +01002523void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2524 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002525{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002526 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2527 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002528 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002529 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002530 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002531 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002532 else
2533 {
2534 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002535 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2536 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002537 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002538}
Johan Pascalb62bb512015-12-03 21:56:45 +01002539#endif /* MBEDTLS_SSL_DTLS_SRTP */
2540
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302541#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002542void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002543{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002544 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002545}
2546
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002547void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002548{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002549 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002550}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302551#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002552
Janos Follath088ce432017-04-10 12:42:31 +01002553#if defined(MBEDTLS_SSL_SRV_C)
2554void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2555 char cert_req_ca_list )
2556{
2557 conf->cert_req_ca_list = cert_req_ca_list;
2558}
2559#endif
2560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002562void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002564 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002565}
2566#endif
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002569void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002571 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002572}
2573#endif
2574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002576int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002579 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002582 }
2583
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002584 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002585
2586 return( 0 );
2587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002590void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002592 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002593}
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002596void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002598 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002599}
2600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002601void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002603 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002604}
2605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002606void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002607 const unsigned char period[8] )
2608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002609 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002610}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002614#if defined(MBEDTLS_SSL_CLI_C)
2615void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002616{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002617 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002618}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002619#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002620
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002621#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002622
Jerry Yud0766ec2022-09-22 10:46:57 +08002623#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002624void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2625 uint16_t num_tickets )
2626{
Jerry Yud0766ec2022-09-22 10:46:57 +08002627 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002628}
2629#endif
2630
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002631void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2632 mbedtls_ssl_ticket_write_t *f_ticket_write,
2633 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2634 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002635{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002636 conf->f_ticket_write = f_ticket_write;
2637 conf->f_ticket_parse = f_ticket_parse;
2638 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002639}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002642
Hanno Becker7e6c1782021-06-08 09:24:55 +01002643void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2644 mbedtls_ssl_export_keys_t *f_export_keys,
2645 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002646{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002647 ssl->f_export_keys = f_export_keys;
2648 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002649}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002650
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002651#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002652void mbedtls_ssl_conf_async_private_cb(
2653 mbedtls_ssl_config *conf,
2654 mbedtls_ssl_async_sign_t *f_async_sign,
2655 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2656 mbedtls_ssl_async_resume_t *f_async_resume,
2657 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002658 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002659{
2660 conf->f_async_sign_start = f_async_sign;
2661 conf->f_async_decrypt_start = f_async_decrypt;
2662 conf->f_async_resume = f_async_resume;
2663 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002664 conf->p_async_config_data = async_config_data;
2665}
2666
Gilles Peskine8f97af72018-04-26 11:46:10 +02002667void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2668{
2669 return( conf->p_async_config_data );
2670}
2671
Gilles Peskine1febfef2018-04-30 11:54:39 +02002672void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002673{
2674 if( ssl->handshake == NULL )
2675 return( NULL );
2676 else
2677 return( ssl->handshake->user_async_ctx );
2678}
2679
Gilles Peskine1febfef2018-04-30 11:54:39 +02002680void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002681 void *ctx )
2682{
2683 if( ssl->handshake != NULL )
2684 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002685}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002686#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002687
Paul Bakker5121ce52009-01-03 21:22:43 +00002688/*
2689 * SSL get accessors
2690 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002691uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002693 if( ssl->session != NULL )
2694 return( ssl->session->verify_result );
2695
2696 if( ssl->session_negotiate != NULL )
2697 return( ssl->session_negotiate->verify_result );
2698
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002699 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700}
2701
Glenn Strauss8f526902022-01-13 00:04:49 -05002702int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2703{
2704 if( ssl == NULL || ssl->session == NULL )
2705 return( 0 );
2706
2707 return( ssl->session->ciphersuite );
2708}
2709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002711{
Paul Bakker926c8e42013-03-06 10:23:34 +01002712 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002713 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002716}
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002722 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002723 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002724 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002725 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002726 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002727 default:
2728 return( "unknown (DTLS)" );
2729 }
2730 }
2731#endif
2732
Glenn Strauss60bfe602022-03-14 19:04:24 -04002733 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002734 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002735 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002736 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002737 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002738 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002739 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002740 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002741 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002742}
2743
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002744#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002745size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2746{
David Horstmann95d516f2021-05-04 18:36:56 +01002747 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002748 size_t read_mfl;
2749
2750 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2752 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2753 {
2754 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2755 }
2756
2757 /* Check if a smaller max length was negotiated */
2758 if( ssl->session_out != NULL )
2759 {
2760 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2761 if( read_mfl < max_len )
2762 {
2763 max_len = read_mfl;
2764 }
2765 }
2766
2767 // During a handshake, use the value being negotiated
2768 if( ssl->session_negotiate != NULL )
2769 {
2770 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2771 if( read_mfl < max_len )
2772 {
2773 max_len = read_mfl;
2774 }
2775 }
2776
2777 return( max_len );
2778}
2779
2780size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002781{
2782 size_t max_len;
2783
2784 /*
2785 * Assume mfl_code is correct since it was checked when set
2786 */
Angus Grattond8213d02016-05-25 20:56:48 +10002787 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002788
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002789 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002790 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002791 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002792 {
Angus Grattond8213d02016-05-25 20:56:48 +10002793 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002794 }
2795
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002796 /* During a handshake, use the value being negotiated */
2797 if( ssl->session_negotiate != NULL &&
2798 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2799 {
2800 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2801 }
2802
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002803 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002804}
2805#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2806
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002808size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002809{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002810 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2811 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2812 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2813 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2814 return ( 0 );
2815
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002816 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2817 return( ssl->mtu );
2818
2819 if( ssl->mtu == 0 )
2820 return( ssl->handshake->mtu );
2821
2822 return( ssl->mtu < ssl->handshake->mtu ?
2823 ssl->mtu : ssl->handshake->mtu );
2824}
2825#endif /* MBEDTLS_SSL_PROTO_DTLS */
2826
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002827int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2828{
2829 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2830
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002831#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2832 !defined(MBEDTLS_SSL_PROTO_DTLS)
2833 (void) ssl;
2834#endif
2835
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002836#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002837 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002838
2839 if( max_len > mfl )
2840 max_len = mfl;
2841#endif
2842
2843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002844 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002845 {
Hanno Becker89490712020-02-05 10:50:12 +00002846 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002847 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2848 const size_t overhead = (size_t) ret;
2849
2850 if( ret < 0 )
2851 return( ret );
2852
2853 if( mtu <= overhead )
2854 {
2855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2856 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2857 }
2858
2859 if( max_len > mtu - overhead )
2860 max_len = mtu - overhead;
2861 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002862#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002863
Hanno Becker0defedb2018-08-10 12:35:02 +01002864#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2865 !defined(MBEDTLS_SSL_PROTO_DTLS)
2866 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002867#endif
2868
2869 return( (int) max_len );
2870}
2871
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002872int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2873{
2874 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2875
2876#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2877 (void) ssl;
2878#endif
2879
2880#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2881 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2882
2883 if( max_len > mfl )
2884 max_len = mfl;
2885#endif
2886
2887 return( (int) max_len );
2888}
2889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890#if defined(MBEDTLS_X509_CRT_PARSE_C)
2891const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002892{
2893 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002894 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002895
Hanno Beckere6824572019-02-07 13:18:46 +00002896#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002897 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002898#else
2899 return( NULL );
2900#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002905int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2906 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002907{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002908 int ret;
2909
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002910 if( ssl == NULL ||
2911 dst == NULL ||
2912 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002913 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002916 }
2917
Hanno Beckere810bbc2021-05-14 16:01:05 +01002918 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2919 * idempotent: Each session can only be exported once.
2920 *
2921 * (This is in preparation for TLS 1.3 support where we will
2922 * need the ability to export multiple sessions (aka tickets),
2923 * which will be achieved by calling mbedtls_ssl_get_session()
2924 * multiple times until it fails.)
2925 *
2926 * Check whether we have already exported the current session,
2927 * and fail if so.
2928 */
2929 if( ssl->session->exported == 1 )
2930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2931
2932 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2933 if( ret != 0 )
2934 return( ret );
2935
2936 /* Remember that we've exported the session. */
2937 ssl->session->exported = 1;
2938 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002941
Paul Bakker5121ce52009-01-03 21:22:43 +00002942/*
Hanno Beckera835da52019-05-16 12:39:07 +01002943 * Define ticket header determining Mbed TLS version
2944 * and structure of the ticket.
2945 */
2946
Hanno Becker94ef3b32019-05-16 12:50:45 +01002947/*
Hanno Becker50b59662019-05-28 14:30:45 +01002948 * Define bitflag determining compile-time settings influencing
2949 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002950 */
2951
Hanno Becker50b59662019-05-28 14:30:45 +01002952#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002953#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002954#else
Hanno Becker3e088662019-05-29 11:10:18 +01002955#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002956#endif /* MBEDTLS_HAVE_TIME */
2957
2958#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002959#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002960#else
Hanno Becker3e088662019-05-29 11:10:18 +01002961#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002962#endif /* MBEDTLS_X509_CRT_PARSE_C */
2963
2964#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002965#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002966#else
Hanno Becker3e088662019-05-29 11:10:18 +01002967#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002968#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2969
2970#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002971#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002972#else
Hanno Becker3e088662019-05-29 11:10:18 +01002973#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002974#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2975
Hanno Becker94ef3b32019-05-16 12:50:45 +01002976#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002977#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002978#else
Hanno Becker3e088662019-05-29 11:10:18 +01002979#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002980#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2981
Hanno Becker94ef3b32019-05-16 12:50:45 +01002982#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2983#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2984#else
2985#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2986#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2987
Hanno Becker3e088662019-05-29 11:10:18 +01002988#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2989#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2990#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2991#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002992#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2993#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002994
Hanno Becker50b59662019-05-28 14:30:45 +01002995#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002996 ( (uint16_t) ( \
2997 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2998 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2999 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3000 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003001 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003002 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003003
Hanno Beckerf8787072019-05-16 12:41:07 +01003004static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003005 MBEDTLS_VERSION_MAJOR,
3006 MBEDTLS_VERSION_MINOR,
3007 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003008 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3009 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003010};
Hanno Beckera835da52019-05-16 12:39:07 +01003011
3012/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003013 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003014 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003015 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003016 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003017 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003018 * opaque mbedtls_version[3]; // library version: major, minor, patch
3019 * opaque session_format[2]; // library-version specific 16-bit field
3020 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003021 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003022 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003023 * Note: When updating the format, remember to keep
3024 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003025 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003026 * // In this version, `session_format` determines
3027 * // the setting of those compile-time
3028 * // configuration options which influence
3029 * // the structure of mbedtls_ssl_session.
3030 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003031 * uint8_t minor_ver; // Protocol minor version. Possible values:
3032 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003033 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003034 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003035 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003036 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003037 * serialized_session_tls12 data;
3038 *
3039 * };
3040 *
3041 * } serialized_session;
3042 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003043 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003044
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003045MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003046static int ssl_session_save( const mbedtls_ssl_session *session,
3047 unsigned char omit_header,
3048 unsigned char *buf,
3049 size_t buf_len,
3050 size_t *olen )
3051{
3052 unsigned char *p = buf;
3053 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003054 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003055#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3056 size_t out_len;
3057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3058#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003059 if( session == NULL )
3060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3061
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003062 if( !omit_header )
3063 {
3064 /*
3065 * Add Mbed TLS version identifier
3066 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003067 used += sizeof( ssl_serialized_session_header );
3068
3069 if( used <= buf_len )
3070 {
3071 memcpy( p, ssl_serialized_session_header,
3072 sizeof( ssl_serialized_session_header ) );
3073 p += sizeof( ssl_serialized_session_header );
3074 }
3075 }
3076
3077 /*
3078 * TLS version identifier
3079 */
3080 used += 1;
3081 if( used <= buf_len )
3082 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003083 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003084 }
3085
3086 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003087 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003088 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003089 {
3090#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003091 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003092 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003094#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3095
Jerry Yu251a12e2022-07-13 15:15:48 +08003096#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3097 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003098 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3099 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003100 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003101 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003102 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003103#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3104
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 default:
3106 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3107 }
3108
3109 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003110 if( used > buf_len )
3111 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003112
3113 return( 0 );
3114}
3115
3116/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003117 * Public wrapper for ssl_session_save()
3118 */
3119int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3120 unsigned char *buf,
3121 size_t buf_len,
3122 size_t *olen )
3123{
3124 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3125}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003126
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003127/*
3128 * Deserialize session, see mbedtls_ssl_session_save() for format.
3129 *
3130 * This internal version is wrapped by a public function that cleans up in
3131 * case of error, and has an extra option omit_header.
3132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003133MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003134static int ssl_session_load( mbedtls_ssl_session *session,
3135 unsigned char omit_header,
3136 const unsigned char *buf,
3137 size_t len )
3138{
3139 const unsigned char *p = buf;
3140 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003141 size_t remaining_len;
3142
3143
3144 if( session == NULL )
3145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003146
3147 if( !omit_header )
3148 {
3149 /*
3150 * Check Mbed TLS version identifier
3151 */
3152
3153 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3155
3156 if( memcmp( p, ssl_serialized_session_header,
3157 sizeof( ssl_serialized_session_header ) ) != 0 )
3158 {
3159 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3160 }
3161 p += sizeof( ssl_serialized_session_header );
3162 }
3163
3164 /*
3165 * TLS version identifier
3166 */
3167 if( 1 > (size_t)( end - p ) )
3168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003169 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003170
3171 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003172 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003173 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003174 {
3175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003176 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003177 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3179
Jerry Yu438ddd82022-07-07 06:55:50 +00003180#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3181 case MBEDTLS_SSL_VERSION_TLS1_3:
3182 return( ssl_tls13_session_load( session, p, remaining_len ) );
3183#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3184
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003185 default:
3186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3187 }
3188}
3189
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003190/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003191 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003192 */
3193int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3194 const unsigned char *buf,
3195 size_t len )
3196{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003197 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003198
3199 if( ret != 0 )
3200 mbedtls_ssl_session_free( session );
3201
3202 return( ret );
3203}
3204
3205/*
Paul Bakker1961b702013-01-25 14:49:24 +01003206 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003208MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003209static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3210{
3211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3212
Ronald Cron66dbf912022-02-02 15:33:46 +01003213 /*
3214 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003215 * that were written into the output buffer by the previous handshake step,
3216 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003217 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3218 * We proceed to the next handshake step only when all data from the
3219 * previous one have been sent to the peer, thus we make sure that this is
3220 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3221 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3222 * we have to wait before to go ahead.
3223 * In the case of TLS 1.3, handshake step handlers do not send data to the
3224 * peer. Data are only sent here and through
3225 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003226 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003227 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003228 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3229 return( ret );
3230
3231#if defined(MBEDTLS_SSL_PROTO_DTLS)
3232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3233 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3234 {
3235 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3236 return( ret );
3237 }
3238#endif /* MBEDTLS_SSL_PROTO_DTLS */
3239
3240 return( ret );
3241}
3242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244{
Hanno Becker41934dd2021-08-07 19:13:43 +01003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Hanno Becker41934dd2021-08-07 19:13:43 +01003247 if( ssl == NULL ||
3248 ssl->conf == NULL ||
3249 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003250 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003251 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003253 }
3254
3255 ret = ssl_prepare_handshake_step( ssl );
3256 if( ret != 0 )
3257 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003258
Jerry Yue7047812021-09-13 19:26:39 +08003259 ret = mbedtls_ssl_handle_pending_alert( ssl );
3260 if( ret != 0 )
3261 goto cleanup;
3262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003264 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003265 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3267 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003268
Ronald Cron9f0fba32022-02-10 16:45:15 +01003269 switch( ssl->state )
3270 {
3271 case MBEDTLS_SSL_HELLO_REQUEST:
3272 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3273 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003274
Ronald Cron9f0fba32022-02-10 16:45:15 +01003275 case MBEDTLS_SSL_CLIENT_HELLO:
3276 ret = mbedtls_ssl_write_client_hello( ssl );
3277 break;
3278
3279 default:
3280#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003281 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003282 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3283 else
3284 ret = mbedtls_ssl_handshake_client_step( ssl );
3285#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3286 ret = mbedtls_ssl_handshake_client_step( ssl );
3287#else
3288 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3289#endif
3290 }
Jerry Yub9930e72021-08-06 17:11:51 +08003291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003295 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003296#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003297 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003298 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003299#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003300
3301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3302 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3303 ret = mbedtls_ssl_handshake_server_step( ssl );
3304#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003306#endif
3307
Jerry Yue7047812021-09-13 19:26:39 +08003308 if( ret != 0 )
3309 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003310 /* handshake_step return error. And it is same
3311 * with alert_reason.
3312 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003313 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003314 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003315 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003316 goto cleanup;
3317 }
3318 }
3319
3320cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003321 return( ret );
3322}
3323
3324/*
3325 * Perform the SSL handshake
3326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003328{
3329 int ret = 0;
3330
Hanno Beckera817ea42020-10-20 15:20:23 +01003331 /* Sanity checks */
3332
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003333 if( ssl == NULL || ssl->conf == NULL )
3334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3335
Hanno Beckera817ea42020-10-20 15:20:23 +01003336#if defined(MBEDTLS_SSL_PROTO_DTLS)
3337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3338 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3339 {
3340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3341 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3343 }
3344#endif /* MBEDTLS_SSL_PROTO_DTLS */
3345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003347
Hanno Beckera817ea42020-10-20 15:20:23 +01003348 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003349 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003352
3353 if( ret != 0 )
3354 break;
3355 }
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 return( ret );
3360}
3361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362#if defined(MBEDTLS_SSL_RENEGOTIATION)
3363#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003364/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003365 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003366 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003367MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003369{
Janos Follath865b3eb2019-12-16 11:46:15 +00003370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003373
3374 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3376 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003377
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003378 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003379 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003381 return( ret );
3382 }
3383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003385
3386 return( 0 );
3387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003389
3390/*
3391 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 * - any side: calling mbedtls_ssl_renegotiate(),
3393 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3394 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003395 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003396 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003398 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003399int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003400{
Janos Follath865b3eb2019-12-16 11:46:15 +00003401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003404
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003405 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3406 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003407
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003408 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3409 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003411 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003413 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003414 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003415 ssl->handshake->out_msg_seq = 1;
3416 else
3417 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003418 }
3419#endif
3420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3422 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003427 return( ret );
3428 }
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003431
3432 return( 0 );
3433}
3434
3435/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003436 * Renegotiate current connection on client,
3437 * or request renegotiation on server
3438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003442
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003443 if( ssl == NULL || ssl->conf == NULL )
3444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003447 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003448 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003449 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003450 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003454
3455 /* Did we already try/start sending HelloRequest? */
3456 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003458
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003464 /*
3465 * On client, either start the renegotiation process or,
3466 * if already in progress, continue the handshake
3467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003469 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003470 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003472
Hanno Becker40cdaa12020-02-05 10:48:27 +00003473 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003474 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003476 return( ret );
3477 }
3478 }
3479 else
3480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003484 return( ret );
3485 }
3486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003488
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003489 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003492
Gilles Peskine9b562d52018-04-25 20:32:43 +02003493void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003494{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003495 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3496
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003497 if( handshake == NULL )
3498 return;
3499
Brett Warrene0edc842021-08-17 09:53:13 +01003500#if defined(MBEDTLS_ECP_C)
3501#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3502 if ( ssl->handshake->group_list_heap_allocated )
3503 mbedtls_free( (void*) handshake->group_list );
3504 handshake->group_list = NULL;
3505#endif /* MBEDTLS_DEPRECATED_REMOVED */
3506#endif /* MBEDTLS_ECP_C */
3507
Jerry Yuf017ee42022-01-12 15:49:48 +08003508#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3509#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3510 if ( ssl->handshake->sig_algs_heap_allocated )
3511 mbedtls_free( (void*) handshake->sig_algs );
3512 handshake->sig_algs = NULL;
3513#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003514#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3515 if( ssl->handshake->certificate_request_context )
3516 {
3517 mbedtls_free( (void*) handshake->certificate_request_context );
3518 }
3519#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003520#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3521
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003522#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3523 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3524 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003525 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003526 handshake->async_in_progress = 0;
3527 }
3528#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3529
Andrzej Kurek25f27152022-08-17 16:09:31 -04003530#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003531#if defined(MBEDTLS_USE_PSA_CRYPTO)
3532 psa_hash_abort( &handshake->fin_sha256_psa );
3533#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003534 mbedtls_sha256_free( &handshake->fin_sha256 );
3535#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003536#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003537#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003538#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003539 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003540#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003541 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003542#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003543#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_DHM_C)
3546 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003547#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003548#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003550#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003551#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003552 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003553#if defined(MBEDTLS_SSL_CLI_C)
3554 mbedtls_free( handshake->ecjpake_cache );
3555 handshake->ecjpake_cache = NULL;
3556 handshake->ecjpake_cache_len = 0;
3557#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003558#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003559
Janos Follath4ae5c292016-02-10 11:27:43 +00003560#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3561 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003562 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003564#endif
3565
Gilles Peskineeccd8882020-03-10 12:19:08 +01003566#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003567#if defined(MBEDTLS_USE_PSA_CRYPTO)
3568 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3569 {
3570 /* The maintenance of the external PSK key slot is the
3571 * user's responsibility. */
3572 if( ssl->handshake->psk_opaque_is_internal )
3573 {
3574 psa_destroy_key( ssl->handshake->psk_opaque );
3575 ssl->handshake->psk_opaque_is_internal = 0;
3576 }
3577 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3578 }
Neil Armstronge952a302022-05-03 10:22:14 +02003579#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003580 if( handshake->psk != NULL )
3581 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003582 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003583 mbedtls_free( handshake->psk );
3584 }
Neil Armstronge952a302022-05-03 10:22:14 +02003585#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003586#endif
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3589 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003590 /*
3591 * Free only the linked list wrapper, not the keys themselves
3592 * since the belong to the SNI callback
3593 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003594 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003596
Gilles Peskineeccd8882020-03-10 12:19:08 +01003597#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003598 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003599 if( handshake->ecrs_peer_cert != NULL )
3600 {
3601 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3602 mbedtls_free( handshake->ecrs_peer_cert );
3603 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003604#endif
3605
Hanno Becker75173122019-02-06 16:18:31 +00003606#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3607 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3608 mbedtls_pk_free( &handshake->peer_pubkey );
3609#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3610
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003611#if defined(MBEDTLS_SSL_CLI_C) && \
3612 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3613 mbedtls_free( handshake->cookie );
3614#endif /* MBEDTLS_SSL_CLI_C &&
3615 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003616
3617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003618 mbedtls_ssl_flight_free( handshake->flight );
3619 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003620#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003621
Ronald Cronf12b81d2022-03-15 10:42:41 +01003622#if defined(MBEDTLS_ECDH_C) && \
3623 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003624 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003625 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003626#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3627
Ronald Cron6f135e12021-12-08 16:57:54 +01003628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003629 mbedtls_ssl_transform_free( handshake->transform_handshake );
3630 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003631 mbedtls_free( handshake->transform_earlydata );
3632 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003633#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003634
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003635
3636#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3637 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3638 * processes datagrams and the fact that a datagram is allowed to have
3639 * several records in it, it is possible that the I/O buffers are not
3640 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003641 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3642 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003643#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003644
Jerry Yuba9c7272021-10-30 11:54:10 +08003645 /* mbedtls_platform_zeroize MUST be last one in this function */
3646 mbedtls_platform_zeroize( handshake,
3647 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003648}
3649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003651{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003652 if( session == NULL )
3653 return;
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003656 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003657#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003658
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003659#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003661#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003662
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003663 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003664}
3665
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003666#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003667
3668#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3669#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3670#else
3671#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3672#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3673
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003674#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003675
3676#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3677#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3678#else
3679#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3680#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3681
3682#if defined(MBEDTLS_SSL_ALPN)
3683#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3684#else
3685#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3686#endif /* MBEDTLS_SSL_ALPN */
3687
3688#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3689#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3691#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3692
3693#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3694 ( (uint32_t) ( \
3695 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3696 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3697 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3698 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3699 0u ) )
3700
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003701static unsigned char ssl_serialized_context_header[] = {
3702 MBEDTLS_VERSION_MAJOR,
3703 MBEDTLS_VERSION_MINOR,
3704 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003705 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3706 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3707 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3708 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3709 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003710};
3711
Paul Bakker5121ce52009-01-03 21:22:43 +00003712/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003713 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003714 *
3715 * The format of the serialized data is:
3716 * (in the presentation language of TLS, RFC 8446 section 3)
3717 *
3718 * // header
3719 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003720 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003721 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003722 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003723 * Note: When updating the format, remember to keep these
3724 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003725 *
3726 * // session sub-structure
3727 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3728 * // transform sub-structure
3729 * uint8 random[64]; // ServerHello.random+ClientHello.random
3730 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3731 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3732 * // fields from ssl_context
3733 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3734 * uint64 in_window_top; // DTLS: last validated record seq_num
3735 * uint64 in_window; // DTLS: bitmask for replay protection
3736 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3737 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3738 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3739 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3740 *
3741 * Note that many fields of the ssl_context or sub-structures are not
3742 * serialized, as they fall in one of the following categories:
3743 *
3744 * 1. forced value (eg in_left must be 0)
3745 * 2. pointer to dynamically-allocated memory (eg session, transform)
3746 * 3. value can be re-derived from other data (eg session keys from MS)
3747 * 4. value was temporary (eg content of input buffer)
3748 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003749 */
3750int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3751 unsigned char *buf,
3752 size_t buf_len,
3753 size_t *olen )
3754{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003755 unsigned char *p = buf;
3756 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003757 size_t session_len;
3758 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003759
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003760 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003761 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3762 * this function's documentation.
3763 *
3764 * These are due to assumptions/limitations in the implementation. Some of
3765 * them are likely to stay (no handshake in progress) some might go away
3766 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003767 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003768 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003769 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003770 {
3771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003773 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003774 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003775 {
3776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003778 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003779 /* Double-check that sub-structures are indeed ready */
3780 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003781 {
3782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003784 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003785 /* There must be no pending incoming or outgoing data */
3786 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003790 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003791 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003795 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796 /* Protocol must be DLTS, not TLS */
3797 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003798 {
3799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003801 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003802 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003803 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003804 {
3805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003807 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003808 /* We must be using an AEAD ciphersuite */
3809 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003810 {
3811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003813 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003814 /* Renegotiation must not be enabled */
3815#if defined(MBEDTLS_SSL_RENEGOTIATION)
3816 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003820 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003821#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003822
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003823 /*
3824 * Version and format identifier
3825 */
3826 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003827
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003828 if( used <= buf_len )
3829 {
3830 memcpy( p, ssl_serialized_context_header,
3831 sizeof( ssl_serialized_context_header ) );
3832 p += sizeof( ssl_serialized_context_header );
3833 }
3834
3835 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003836 * Session (length + data)
3837 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003838 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003839 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3840 return( ret );
3841
3842 used += 4 + session_len;
3843 if( used <= buf_len )
3844 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003845 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3846 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003847
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003848 ret = ssl_session_save( ssl->session, 1,
3849 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003850 if( ret != 0 )
3851 return( ret );
3852
3853 p += session_len;
3854 }
3855
3856 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003857 * Transform
3858 */
3859 used += sizeof( ssl->transform->randbytes );
3860 if( used <= buf_len )
3861 {
3862 memcpy( p, ssl->transform->randbytes,
3863 sizeof( ssl->transform->randbytes ) );
3864 p += sizeof( ssl->transform->randbytes );
3865 }
3866
3867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3868 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3869 if( used <= buf_len )
3870 {
3871 *p++ = ssl->transform->in_cid_len;
3872 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3873 p += ssl->transform->in_cid_len;
3874
3875 *p++ = ssl->transform->out_cid_len;
3876 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3877 p += ssl->transform->out_cid_len;
3878 }
3879#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3880
3881 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003882 * Saved fields from top-level ssl_context structure
3883 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003884 used += 4;
3885 if( used <= buf_len )
3886 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003887 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3888 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003889 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003890
3891#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3892 used += 16;
3893 if( used <= buf_len )
3894 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003895 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3896 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003897
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003898 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3899 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003900 }
3901#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3902
3903#if defined(MBEDTLS_SSL_PROTO_DTLS)
3904 used += 1;
3905 if( used <= buf_len )
3906 {
3907 *p++ = ssl->disable_datagram_packing;
3908 }
3909#endif /* MBEDTLS_SSL_PROTO_DTLS */
3910
Jerry Yuae0b2e22021-10-08 15:21:19 +08003911 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003912 if( used <= buf_len )
3913 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003914 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3915 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003916 }
3917
3918#if defined(MBEDTLS_SSL_PROTO_DTLS)
3919 used += 2;
3920 if( used <= buf_len )
3921 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003922 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3923 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003924 }
3925#endif /* MBEDTLS_SSL_PROTO_DTLS */
3926
3927#if defined(MBEDTLS_SSL_ALPN)
3928 {
3929 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003930 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003931 : 0;
3932
3933 used += 1 + alpn_len;
3934 if( used <= buf_len )
3935 {
3936 *p++ = alpn_len;
3937
3938 if( ssl->alpn_chosen != NULL )
3939 {
3940 memcpy( p, ssl->alpn_chosen, alpn_len );
3941 p += alpn_len;
3942 }
3943 }
3944 }
3945#endif /* MBEDTLS_SSL_ALPN */
3946
3947 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003948 * Done
3949 */
3950 *olen = used;
3951
3952 if( used > buf_len )
3953 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003954
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003955 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3956
Hanno Becker43aefe22020-02-05 10:44:56 +00003957 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003958}
3959
3960/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003961 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003962 *
3963 * This internal version is wrapped by a public function that cleans up in
3964 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003965 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003966MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003967static int ssl_context_load( mbedtls_ssl_context *ssl,
3968 const unsigned char *buf,
3969 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003970{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003971 const unsigned char *p = buf;
3972 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003973 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003975
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003976 /*
3977 * The context should have been freshly setup or reset.
3978 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003979 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003980 * renegotiating, or if the user mistakenly loaded a session first.)
3981 */
3982 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3983 ssl->session != NULL )
3984 {
3985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3986 }
3987
3988 /*
3989 * We can't check that the config matches the initial one, but we can at
3990 * least check it matches the requirements for serializing.
3991 */
3992 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003993 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3994 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003995#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003996 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003997#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003998 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003999 {
4000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4001 }
4002
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004003 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4004
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004005 /*
4006 * Check version identifier
4007 */
4008 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4010
4011 if( memcmp( p, ssl_serialized_context_header,
4012 sizeof( ssl_serialized_context_header ) ) != 0 )
4013 {
4014 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4015 }
4016 p += sizeof( ssl_serialized_context_header );
4017
4018 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004019 * Session
4020 */
4021 if( (size_t)( end - p ) < 4 )
4022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4023
4024 session_len = ( (size_t) p[0] << 24 ) |
4025 ( (size_t) p[1] << 16 ) |
4026 ( (size_t) p[2] << 8 ) |
4027 ( (size_t) p[3] );
4028 p += 4;
4029
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004030 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004031 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004032 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004033 ssl->session_in = ssl->session;
4034 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004035 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004036
4037 if( (size_t)( end - p ) < session_len )
4038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4039
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004040 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004041 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004042 {
4043 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004044 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004045 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004046
4047 p += session_len;
4048
4049 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004050 * Transform
4051 */
4052
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004053 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004054 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004055 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004056 ssl->transform_in = ssl->transform;
4057 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004058 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004059
4060 /* Read random bytes and populate structure */
4061 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004063#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004064 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004065 ssl->session->ciphersuite,
4066 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004067#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004068 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004069#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004070 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4071 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004072 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004073 ssl->conf->endpoint,
4074 ssl );
4075 if( ret != 0 )
4076 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004077#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004078 p += sizeof( ssl->transform->randbytes );
4079
4080#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4081 /* Read connection IDs and store them */
4082 if( (size_t)( end - p ) < 1 )
4083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4084
4085 ssl->transform->in_cid_len = *p++;
4086
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004087 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4089
4090 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4091 p += ssl->transform->in_cid_len;
4092
4093 ssl->transform->out_cid_len = *p++;
4094
4095 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4097
4098 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4099 p += ssl->transform->out_cid_len;
4100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4101
4102 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004103 * Saved fields from top-level ssl_context structure
4104 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004105 if( (size_t)( end - p ) < 4 )
4106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4107
4108 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4109 ( (uint32_t) p[1] << 16 ) |
4110 ( (uint32_t) p[2] << 8 ) |
4111 ( (uint32_t) p[3] );
4112 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004113
4114#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4115 if( (size_t)( end - p ) < 16 )
4116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4117
4118 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4119 ( (uint64_t) p[1] << 48 ) |
4120 ( (uint64_t) p[2] << 40 ) |
4121 ( (uint64_t) p[3] << 32 ) |
4122 ( (uint64_t) p[4] << 24 ) |
4123 ( (uint64_t) p[5] << 16 ) |
4124 ( (uint64_t) p[6] << 8 ) |
4125 ( (uint64_t) p[7] );
4126 p += 8;
4127
4128 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4129 ( (uint64_t) p[1] << 48 ) |
4130 ( (uint64_t) p[2] << 40 ) |
4131 ( (uint64_t) p[3] << 32 ) |
4132 ( (uint64_t) p[4] << 24 ) |
4133 ( (uint64_t) p[5] << 16 ) |
4134 ( (uint64_t) p[6] << 8 ) |
4135 ( (uint64_t) p[7] );
4136 p += 8;
4137#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4138
4139#if defined(MBEDTLS_SSL_PROTO_DTLS)
4140 if( (size_t)( end - p ) < 1 )
4141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4142
4143 ssl->disable_datagram_packing = *p++;
4144#endif /* MBEDTLS_SSL_PROTO_DTLS */
4145
Jerry Yud9a94fe2021-09-28 18:58:59 +08004146 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004148 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4149 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004150
4151#if defined(MBEDTLS_SSL_PROTO_DTLS)
4152 if( (size_t)( end - p ) < 2 )
4153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4154
4155 ssl->mtu = ( p[0] << 8 ) | p[1];
4156 p += 2;
4157#endif /* MBEDTLS_SSL_PROTO_DTLS */
4158
4159#if defined(MBEDTLS_SSL_ALPN)
4160 {
4161 uint8_t alpn_len;
4162 const char **cur;
4163
4164 if( (size_t)( end - p ) < 1 )
4165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 alpn_len = *p++;
4168
4169 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4170 {
4171 /* alpn_chosen should point to an item in the configured list */
4172 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4173 {
4174 if( strlen( *cur ) == alpn_len &&
4175 memcmp( p, cur, alpn_len ) == 0 )
4176 {
4177 ssl->alpn_chosen = *cur;
4178 break;
4179 }
4180 }
4181 }
4182
4183 /* can only happen on conf mismatch */
4184 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4186
4187 p += alpn_len;
4188 }
4189#endif /* MBEDTLS_SSL_ALPN */
4190
4191 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004192 * Forced fields from top-level ssl_context structure
4193 *
4194 * Most of them already set to the correct value by mbedtls_ssl_init() and
4195 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4196 */
4197 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004198 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004199
Hanno Becker361b10d2019-08-30 10:42:49 +01004200 /* Adjust pointers for header fields of outgoing records to
4201 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004202 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004203
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004204#if defined(MBEDTLS_SSL_PROTO_DTLS)
4205 ssl->in_epoch = 1;
4206#endif
4207
4208 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4209 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004210 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4211 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004212 if( ssl->handshake != NULL )
4213 {
4214 mbedtls_ssl_handshake_free( ssl );
4215 mbedtls_free( ssl->handshake );
4216 ssl->handshake = NULL;
4217 }
4218
4219 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004220 * Done - should have consumed entire buffer
4221 */
4222 if( p != end )
4223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004224
4225 return( 0 );
4226}
4227
4228/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004229 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004230 */
4231int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4232 const unsigned char *buf,
4233 size_t len )
4234{
4235 int ret = ssl_context_load( context, buf, len );
4236
4237 if( ret != 0 )
4238 mbedtls_ssl_free( context );
4239
4240 return( ret );
4241}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004242#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004243
4244/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004245 * Free an SSL context
4246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004248{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004249 if( ssl == NULL )
4250 return;
4251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004254 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 {
sander-visserb8aa2072020-05-06 22:05:13 +02004256#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4257 size_t out_buf_len = ssl->out_buf_len;
4258#else
4259 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4260#endif
4261
Darryl Greenb33cc762019-11-28 14:29:44 +00004262 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004264 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 }
4266
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004267 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 {
sander-visserb8aa2072020-05-06 22:05:13 +02004269#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4270 size_t in_buf_len = ssl->in_buf_len;
4271#else
4272 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4273#endif
4274
Darryl Greenb33cc762019-11-28 14:29:44 +00004275 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004277 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004278 }
4279
Paul Bakker48916f92012-09-16 19:57:18 +00004280 if( ssl->transform )
4281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 mbedtls_ssl_transform_free( ssl->transform );
4283 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004284 }
4285
4286 if( ssl->handshake )
4287 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004288 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4290 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292 mbedtls_free( ssl->handshake );
4293 mbedtls_free( ssl->transform_negotiate );
4294 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004295 }
4296
Ronald Cron6f135e12021-12-08 16:57:54 +01004297#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004298 mbedtls_ssl_transform_free( ssl->transform_application );
4299 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004300#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004301
Paul Bakkerc0463502013-02-14 11:19:38 +01004302 if( ssl->session )
4303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 mbedtls_ssl_session_free( ssl->session );
4305 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004306 }
4307
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004308#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004309 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004310 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004311 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004316#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004318#endif
4319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004321
Paul Bakker86f04f42013-02-14 11:20:09 +01004322 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004323 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324}
4325
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004326/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004327 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004328 */
4329void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4330{
4331 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4332}
4333
Gilles Peskineae270bf2021-06-02 00:05:29 +02004334/* The selection should be the same as mbedtls_x509_crt_profile_default in
4335 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004336 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004337 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004338 * about this list.
4339 */
Brett Warrene0edc842021-08-17 09:53:13 +01004340static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004341#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004342 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004343#endif
4344#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004345 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004346#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004347#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004348 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004349#endif
4350#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004351 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004352#endif
4353#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004354 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004355#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004356#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004357 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004358#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004359#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004360 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004361#endif
4362#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004363 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004364#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004365 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004366};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004367
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004368static int ssl_preset_suiteb_ciphersuites[] = {
4369 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4370 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4371 0
4372};
4373
Gilles Peskineeccd8882020-03-10 12:19:08 +01004374#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004375
Jerry Yu909df7b2022-01-22 11:56:27 +08004376/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004377 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004378 * rules SHOULD be upheld.
4379 * - No duplicate entries.
4380 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004381 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004382 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004383 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004384static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004385
Andrzej Kurek25f27152022-08-17 16:09:31 -04004386#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 +08004387 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4388 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004389#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004390 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004391
Andrzej Kurek25f27152022-08-17 16:09:31 -04004392#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 +08004393 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4394 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004395#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004396 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4397
Andrzej Kurek25f27152022-08-17 16:09:31 -04004398#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 +08004399 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4400 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004401#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004402 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004403
Andrzej Kurek25f27152022-08-17 16:09:31 -04004404#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 +08004405 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004406#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 +08004407
Andrzej Kurek25f27152022-08-17 16:09:31 -04004408#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 +08004409 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004410#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 +08004411
Andrzej Kurek25f27152022-08-17 16:09:31 -04004412#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 +08004413 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004414#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 +08004415
Andrzej Kurek25f27152022-08-17 16:09:31 -04004416#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 +08004417 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4418#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4419
Andrzej Kurek25f27152022-08-17 16:09:31 -04004420#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 +08004421 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4422#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4423
Andrzej Kurek25f27152022-08-17 16:09:31 -04004424#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 +08004425 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4426#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4427
Gabor Mezei15b95a62022-05-09 16:37:58 +02004428 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004429};
4430
4431/* NOTICE: see above */
4432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4433static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004434#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004435#if defined(MBEDTLS_ECDSA_C)
4436 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004437#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004438#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4439 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4440#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004441#if defined(MBEDTLS_RSA_C)
4442 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4443#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004444#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004445#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004446#if defined(MBEDTLS_ECDSA_C)
4447 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004448#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004449#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4450 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4451#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004452#if defined(MBEDTLS_RSA_C)
4453 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4454#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004456#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004457#if defined(MBEDTLS_ECDSA_C)
4458 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004459#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004460#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4461 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4462#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004463#if defined(MBEDTLS_RSA_C)
4464 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4465#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004467 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004468};
4469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4470/* NOTICE: see above */
4471static uint16_t ssl_preset_suiteb_sig_algs[] = {
4472
Andrzej Kurek25f27152022-08-17 16:09:31 -04004473#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 +08004474 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4475 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004476#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004477 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4478
Andrzej Kurek25f27152022-08-17 16:09:31 -04004479#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 +08004480 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4481 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004482#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004483 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004484
Andrzej Kurek25f27152022-08-17 16:09:31 -04004485#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 +08004486 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004487#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 +08004488
Andrzej Kurek25f27152022-08-17 16:09:31 -04004489#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 +08004490 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004491#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004492
Gabor Mezei15b95a62022-05-09 16:37:58 +02004493 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004494};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004495
Jerry Yu909df7b2022-01-22 11:56:27 +08004496/* NOTICE: see above */
4497#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4498static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004499#if defined(MBEDTLS_HAS_ALG_SHA_256_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_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +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_SHA256 ),
4505#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004506#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004507#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004508#if defined(MBEDTLS_ECDSA_C)
4509 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004510#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004511#if defined(MBEDTLS_RSA_C)
4512 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4513#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004514#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004515 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004516};
Jerry Yu909df7b2022-01-22 11:56:27 +08004517#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4518
Jerry Yu1a8b4812022-01-20 17:56:50 +08004519#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004520
Brett Warrene0edc842021-08-17 09:53:13 +01004521static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004522#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004523 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004524#endif
4525#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004526 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004527#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004528 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004529};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004530
Jerry Yu1a8b4812022-01-20 17:56:50 +08004531#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004532/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004533 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004534MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004535static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004536{
4537 size_t i, j;
4538 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004539
Gabor Mezei15b95a62022-05-09 16:37:58 +02004540 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004541 {
Jerry Yuf377d642022-01-25 10:43:59 +08004542 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004543 {
Jerry Yuf377d642022-01-25 10:43:59 +08004544 if( sig_algs[i] != sig_algs[j] )
4545 continue;
4546 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4547 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4548 sig_algs[i], j, i );
4549 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004550 }
4551 }
4552 return( ret );
4553}
4554
4555#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4556
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004557/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004558 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004559 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004560int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004561 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004562{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004563#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004565#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004566
Jerry Yu1a8b4812022-01-20 17:56:50 +08004567#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004568 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004569 {
4570 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4571 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4572 }
4573
Jerry Yuf377d642022-01-25 10:43:59 +08004574 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004575 {
4576 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4577 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4578 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004579
4580#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004581 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004582 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004583 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004584 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4585 }
4586
Jerry Yuf377d642022-01-25 10:43:59 +08004587 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004588 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004589 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004590 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4591 }
4592#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004593#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4594
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004595 /* Use the functions here so that they are covered in tests,
4596 * but otherwise access member directly for efficiency */
4597 mbedtls_ssl_conf_endpoint( conf, endpoint );
4598 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004599
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004600 /*
4601 * Things that are common to all presets
4602 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004603#if defined(MBEDTLS_SSL_CLI_C)
4604 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4605 {
4606 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4607#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4608 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4609#endif
4610 }
4611#endif
4612
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004613#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4614 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4615#endif
4616
4617#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4618 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4619#endif
4620
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004621#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004622 conf->f_cookie_write = ssl_cookie_write_dummy;
4623 conf->f_cookie_check = ssl_cookie_check_dummy;
4624#endif
4625
4626#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4627 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4628#endif
4629
Janos Follath088ce432017-04-10 12:42:31 +01004630#if defined(MBEDTLS_SSL_SRV_C)
4631 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004632 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004633#endif
4634
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004635#if defined(MBEDTLS_SSL_PROTO_DTLS)
4636 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4637 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4638#endif
4639
4640#if defined(MBEDTLS_SSL_RENEGOTIATION)
4641 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004642 memset( conf->renego_period, 0x00, 2 );
4643 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004644#endif
4645
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004646#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004647 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4648 {
4649 const unsigned char dhm_p[] =
4650 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4651 const unsigned char dhm_g[] =
4652 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004653
Hanno Beckere2defad2021-07-24 05:59:17 +01004654 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4655 dhm_p, sizeof( dhm_p ),
4656 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4657 {
4658 return( ret );
4659 }
4660 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004661#endif
4662
Ronald Cron6f135e12021-12-08 16:57:54 +01004663#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004664#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004665 mbedtls_ssl_conf_new_session_tickets(
4666 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4667#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004668 /*
4669 * Allow all TLS 1.3 key exchange modes by default.
4670 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004671 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004673
XiaokangQian4d3a6042022-04-21 13:46:17 +00004674 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4675 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004676#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004677 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004678 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004679#else
XiaokangQian060d8672022-04-21 09:24:56 +00004680 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004681#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004682 }
4683 else
4684 {
4685#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4686 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4687 {
4688 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4689 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4690 }
4691 else
4692 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4693 {
4694 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4695 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4696 }
4697#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4698 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4699 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4700#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4701 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4702 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4703#else
4704 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4705#endif
4706 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004707
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004708 /*
4709 * Preset-specific defaults
4710 */
4711 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004712 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004713 /*
4714 * NSA Suite B
4715 */
4716 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004717
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004718 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004719
4720#if defined(MBEDTLS_X509_CRT_PARSE_C)
4721 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004722#endif
4723
Gilles Peskineeccd8882020-03-10 12:19:08 +01004724#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4726 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4727 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4728 else
4729#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4730 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004731#endif
4732
Brett Warrene0edc842021-08-17 09:53:13 +01004733#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4734 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004735#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004736 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004737 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004738
4739 /*
4740 * Default
4741 */
4742 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004743
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004744 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004745
4746#if defined(MBEDTLS_X509_CRT_PARSE_C)
4747 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4748#endif
4749
Gilles Peskineeccd8882020-03-10 12:19:08 +01004750#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004751#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4752 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4753 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4754 else
4755#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4756 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004757#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004758
Brett Warrene0edc842021-08-17 09:53:13 +01004759#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4760 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004761#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004762 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004763
4764#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4765 conf->dhm_min_bitlen = 1024;
4766#endif
4767 }
4768
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004769 return( 0 );
4770}
4771
4772/*
4773 * Free mbedtls_ssl_config
4774 */
4775void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4776{
4777#if defined(MBEDTLS_DHM_C)
4778 mbedtls_mpi_free( &conf->dhm_P );
4779 mbedtls_mpi_free( &conf->dhm_G );
4780#endif
4781
Gilles Peskineeccd8882020-03-10 12:19:08 +01004782#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004783#if defined(MBEDTLS_USE_PSA_CRYPTO)
4784 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4785 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004786 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4787 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004788#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004789 if( conf->psk != NULL )
4790 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004791 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004792 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004793 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004794 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004795 }
4796
4797 if( conf->psk_identity != NULL )
4798 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004799 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004800 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004801 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004802 conf->psk_identity_len = 0;
4803 }
4804#endif
4805
4806#if defined(MBEDTLS_X509_CRT_PARSE_C)
4807 ssl_key_cert_free( conf->key_cert );
4808#endif
4809
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004810 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004811}
4812
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004813#if defined(MBEDTLS_PK_C) && \
4814 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004815/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820#if defined(MBEDTLS_RSA_C)
4821 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4822 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004823#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824#if defined(MBEDTLS_ECDSA_C)
4825 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4826 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004829}
4830
Hanno Becker7e5437a2017-04-28 17:15:26 +01004831unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4832{
4833 switch( type ) {
4834 case MBEDTLS_PK_RSA:
4835 return( MBEDTLS_SSL_SIG_RSA );
4836 case MBEDTLS_PK_ECDSA:
4837 case MBEDTLS_PK_ECKEY:
4838 return( MBEDTLS_SSL_SIG_ECDSA );
4839 default:
4840 return( MBEDTLS_SSL_SIG_ANON );
4841 }
4842}
4843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004845{
4846 switch( sig )
4847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848#if defined(MBEDTLS_RSA_C)
4849 case MBEDTLS_SSL_SIG_RSA:
4850 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#if defined(MBEDTLS_ECDSA_C)
4853 case MBEDTLS_SSL_SIG_ECDSA:
4854 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004855#endif
4856 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004858 }
4859}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004860#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004861
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004862/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004863 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004866{
4867 switch( hash )
4868 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004869#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 case MBEDTLS_SSL_HASH_MD5:
4871 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004872#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004873#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 case MBEDTLS_SSL_HASH_SHA1:
4875 return( MBEDTLS_MD_SHA1 );
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_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878 case MBEDTLS_SSL_HASH_SHA224:
4879 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004880#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004881#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 case MBEDTLS_SSL_HASH_SHA256:
4883 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004884#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004885#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 case MBEDTLS_SSL_HASH_SHA384:
4887 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004888#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004889#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 case MBEDTLS_SSL_HASH_SHA512:
4891 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004892#endif
4893 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004895 }
4896}
4897
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004898/*
4899 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4900 */
4901unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4902{
4903 switch( md )
4904 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004905#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004906 case MBEDTLS_MD_MD5:
4907 return( MBEDTLS_SSL_HASH_MD5 );
4908#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004909#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004910 case MBEDTLS_MD_SHA1:
4911 return( MBEDTLS_SSL_HASH_SHA1 );
4912#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004913#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004914 case MBEDTLS_MD_SHA224:
4915 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004916#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004917#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004918 case MBEDTLS_MD_SHA256:
4919 return( MBEDTLS_SSL_HASH_SHA256 );
4920#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004921#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004922 case MBEDTLS_MD_SHA384:
4923 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004924#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004925#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004926 case MBEDTLS_MD_SHA512:
4927 return( MBEDTLS_SSL_HASH_SHA512 );
4928#endif
4929 default:
4930 return( MBEDTLS_SSL_HASH_NONE );
4931 }
4932}
4933
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004934/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004935 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004936 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004937 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004938int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004939{
Brett Warrene0edc842021-08-17 09:53:13 +01004940 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004941
Brett Warrene0edc842021-08-17 09:53:13 +01004942 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004943 return( -1 );
4944
Brett Warrene0edc842021-08-17 09:53:13 +01004945 for( ; *group_list != 0; group_list++ )
4946 {
4947 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004948 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004949 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004950
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004951 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004952}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004953
4954#if defined(MBEDTLS_ECP_C)
4955/*
4956 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4957 */
4958int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4959{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004960 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004961 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004962
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004963 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004964 return -1;
4965
4966 uint16_t tls_id = grp_info->tls_id;
4967
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004968 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4969}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004970#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972#if defined(MBEDTLS_X509_CRT_PARSE_C)
4973int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4974 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004975 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004976 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004977{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004978 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004979 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004980 const char *ext_oid;
4981 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004984 {
4985 /* Server part of the key exchange */
4986 switch( ciphersuite->key_exchange )
4987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 case MBEDTLS_KEY_EXCHANGE_RSA:
4989 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004990 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004991 break;
4992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4994 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4995 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4996 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004997 break;
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5000 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005001 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005002 break;
5003
5004 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 case MBEDTLS_KEY_EXCHANGE_NONE:
5006 case MBEDTLS_KEY_EXCHANGE_PSK:
5007 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5008 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005009 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005010 usage = 0;
5011 }
5012 }
5013 else
5014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5016 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005017 }
5018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005020 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005021 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005022 ret = -1;
5023 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5028 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005029 }
5030 else
5031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5033 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005034 }
5035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005037 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005038 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005039 ret = -1;
5040 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005041
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005042 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005045
Jerry Yu148165c2021-09-24 23:20:59 +08005046#if defined(MBEDTLS_USE_PSA_CRYPTO)
5047int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5048 const mbedtls_md_type_t md,
5049 unsigned char *dst,
5050 size_t dst_len,
5051 size_t *olen )
5052{
Ronald Cronf6893e12022-01-07 22:09:01 +01005053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5054 psa_hash_operation_t *hash_operation_to_clone;
5055 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5056
Jerry Yu148165c2021-09-24 23:20:59 +08005057 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005058
5059 switch( md )
5060 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005061#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005062 case MBEDTLS_MD_SHA384:
5063 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5064 break;
5065#endif
5066
Andrzej Kurek25f27152022-08-17 16:09:31 -04005067#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005068 case MBEDTLS_MD_SHA256:
5069 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5070 break;
5071#endif
5072
5073 default:
5074 goto exit;
5075 }
5076
5077 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5078 if( status != PSA_SUCCESS )
5079 goto exit;
5080
5081 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5082 if( status != PSA_SUCCESS )
5083 goto exit;
5084
5085exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005086 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005087}
5088#else /* MBEDTLS_USE_PSA_CRYPTO */
5089
Andrzej Kurek25f27152022-08-17 16:09:31 -04005090#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005091MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005092static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5093 unsigned char *dst,
5094 size_t dst_len,
5095 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005096{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005097 int ret;
5098 mbedtls_sha512_context sha512;
5099
5100 if( dst_len < 48 )
5101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5102
5103 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005104 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005105
5106 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5107 {
5108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5109 goto exit;
5110 }
5111
5112 *olen = 48;
5113
5114exit:
5115
5116 mbedtls_sha512_free( &sha512 );
5117 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005118}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005119#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120
Andrzej Kurek25f27152022-08-17 16:09:31 -04005121#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005122MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005123static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5124 unsigned char *dst,
5125 size_t dst_len,
5126 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005127{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005128 int ret;
5129 mbedtls_sha256_context sha256;
5130
5131 if( dst_len < 32 )
5132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5133
5134 mbedtls_sha256_init( &sha256 );
5135 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005136
Jerry Yu24c0ec32021-09-09 14:21:07 +08005137 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5138 {
5139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5140 goto exit;
5141 }
5142
5143 *olen = 32;
5144
5145exit:
5146
5147 mbedtls_sha256_free( &sha256 );
5148 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005149}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005150#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005151
Jerry Yu000f9762021-09-14 11:12:51 +08005152int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5153 const mbedtls_md_type_t md,
5154 unsigned char *dst,
5155 size_t dst_len,
5156 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005157{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005158 switch( md )
5159 {
5160
Andrzej Kurek25f27152022-08-17 16:09:31 -04005161#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005162 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005163 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005164#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005165
Andrzej Kurek25f27152022-08-17 16:09:31 -04005166#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005167 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005168 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005169#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005170
5171 default:
5172 break;
5173 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005175}
XiaokangQian647719a2021-12-07 09:16:29 +00005176
Jerry Yu148165c2021-09-24 23:20:59 +08005177#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005178
Gabor Mezei078e8032022-04-27 21:17:56 +02005179#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5180/* mbedtls_ssl_parse_sig_alg_ext()
5181 *
5182 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5183 * value (TLS 1.3 RFC8446):
5184 * enum {
5185 * ....
5186 * ecdsa_secp256r1_sha256( 0x0403 ),
5187 * ecdsa_secp384r1_sha384( 0x0503 ),
5188 * ecdsa_secp521r1_sha512( 0x0603 ),
5189 * ....
5190 * } SignatureScheme;
5191 *
5192 * struct {
5193 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5194 * } SignatureSchemeList;
5195 *
5196 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5197 * value (TLS 1.2 RFC5246):
5198 * enum {
5199 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5200 * sha512(6), (255)
5201 * } HashAlgorithm;
5202 *
5203 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5204 * SignatureAlgorithm;
5205 *
5206 * struct {
5207 * HashAlgorithm hash;
5208 * SignatureAlgorithm signature;
5209 * } SignatureAndHashAlgorithm;
5210 *
5211 * SignatureAndHashAlgorithm
5212 * supported_signature_algorithms<2..2^16-2>;
5213 *
5214 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5215 * generalization of the TLS 1.2 signature algorithm extension.
5216 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5217 * `SignatureScheme` field of TLS 1.3
5218 *
5219 */
5220int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5221 const unsigned char *buf,
5222 const unsigned char *end )
5223{
5224 const unsigned char *p = buf;
5225 size_t supported_sig_algs_len = 0;
5226 const unsigned char *supported_sig_algs_end;
5227 uint16_t sig_alg;
5228 uint32_t common_idx = 0;
5229
5230 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5231 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5232 p += 2;
5233
5234 memset( ssl->handshake->received_sig_algs, 0,
5235 sizeof(ssl->handshake->received_sig_algs) );
5236
5237 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5238 supported_sig_algs_end = p + supported_sig_algs_len;
5239 while( p < supported_sig_algs_end )
5240 {
5241 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5242 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5243 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005244 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5245 sig_alg,
5246 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005248 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005249 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5250 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5251 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005252 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005253 }
5254#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005255
Jerry Yuf3b46b52022-06-19 16:52:27 +08005256 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5257 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005258
5259 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5260 {
5261 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5262 common_idx += 1;
5263 }
5264 }
5265 /* Check that we consumed all the message. */
5266 if( p != end )
5267 {
5268 MBEDTLS_SSL_DEBUG_MSG( 1,
5269 ( "Signature algorithms extension length misaligned" ) );
5270 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5271 MBEDTLS_ERR_SSL_DECODE_ERROR );
5272 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5273 }
5274
5275 if( common_idx == 0 )
5276 {
5277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5278 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5279 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5280 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5281 }
5282
Gabor Mezei15b95a62022-05-09 16:37:58 +02005283 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005284 return( 0 );
5285}
5286
5287#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5288
Jerry Yudc7bd172022-02-17 13:44:15 +08005289#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5290
5291#if defined(MBEDTLS_USE_PSA_CRYPTO)
5292
5293static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5294 mbedtls_svc_key_id_t key,
5295 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005296 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005297 const unsigned char* seed, size_t seed_length,
5298 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005299 const unsigned char* other_secret,
5300 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005301 size_t capacity )
5302{
5303 psa_status_t status;
5304
5305 status = psa_key_derivation_setup( derivation, alg );
5306 if( status != PSA_SUCCESS )
5307 return( status );
5308
5309 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5310 {
5311 status = psa_key_derivation_input_bytes( derivation,
5312 PSA_KEY_DERIVATION_INPUT_SEED,
5313 seed, seed_length );
5314 if( status != PSA_SUCCESS )
5315 return( status );
5316
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005317 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005318 {
5319 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005320 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5321 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005322 if( status != PSA_SUCCESS )
5323 return( status );
5324 }
5325
Jerry Yudc7bd172022-02-17 13:44:15 +08005326 if( mbedtls_svc_key_id_is_null( key ) )
5327 {
5328 status = psa_key_derivation_input_bytes(
5329 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005330 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005331 }
5332 else
5333 {
5334 status = psa_key_derivation_input_key(
5335 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5336 }
5337 if( status != PSA_SUCCESS )
5338 return( status );
5339
5340 status = psa_key_derivation_input_bytes( derivation,
5341 PSA_KEY_DERIVATION_INPUT_LABEL,
5342 label, label_length );
5343 if( status != PSA_SUCCESS )
5344 return( status );
5345 }
5346 else
5347 {
5348 return( PSA_ERROR_NOT_SUPPORTED );
5349 }
5350
5351 status = psa_key_derivation_set_capacity( derivation, capacity );
5352 if( status != PSA_SUCCESS )
5353 return( status );
5354
5355 return( PSA_SUCCESS );
5356}
5357
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005358MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005359static int tls_prf_generic( mbedtls_md_type_t md_type,
5360 const unsigned char *secret, size_t slen,
5361 const char *label,
5362 const unsigned char *random, size_t rlen,
5363 unsigned char *dstbuf, size_t dlen )
5364{
5365 psa_status_t status;
5366 psa_algorithm_t alg;
5367 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5368 psa_key_derivation_operation_t derivation =
5369 PSA_KEY_DERIVATION_OPERATION_INIT;
5370
5371 if( md_type == MBEDTLS_MD_SHA384 )
5372 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5373 else
5374 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5375
5376 /* Normally a "secret" should be long enough to be impossible to
5377 * find by brute force, and in particular should not be empty. But
5378 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5379 * and for this use case it makes sense to have a 0-length "secret".
5380 * Since the key API doesn't allow importing a key of length 0,
5381 * keep master_key=0, which setup_psa_key_derivation() understands
5382 * to mean a 0-length "secret" input. */
5383 if( slen != 0 )
5384 {
5385 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5386 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5387 psa_set_key_algorithm( &key_attributes, alg );
5388 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5389
5390 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5391 if( status != PSA_SUCCESS )
5392 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5393 }
5394
5395 status = setup_psa_key_derivation( &derivation,
5396 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005397 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005398 random, rlen,
5399 (unsigned char const *) label,
5400 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005401 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005402 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_output_bytes( &derivation, dstbuf, dlen );
5411 if( status != PSA_SUCCESS )
5412 {
5413 psa_key_derivation_abort( &derivation );
5414 psa_destroy_key( master_key );
5415 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5416 }
5417
5418 status = psa_key_derivation_abort( &derivation );
5419 if( status != PSA_SUCCESS )
5420 {
5421 psa_destroy_key( master_key );
5422 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5423 }
5424
5425 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5426 status = psa_destroy_key( master_key );
5427 if( status != PSA_SUCCESS )
5428 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5429
5430 return( 0 );
5431}
5432
5433#else /* MBEDTLS_USE_PSA_CRYPTO */
5434
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005436static int tls_prf_generic( mbedtls_md_type_t md_type,
5437 const unsigned char *secret, size_t slen,
5438 const char *label,
5439 const unsigned char *random, size_t rlen,
5440 unsigned char *dstbuf, size_t dlen )
5441{
5442 size_t nb;
5443 size_t i, j, k, md_len;
5444 unsigned char *tmp;
5445 size_t tmp_len = 0;
5446 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5447 const mbedtls_md_info_t *md_info;
5448 mbedtls_md_context_t md_ctx;
5449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5450
5451 mbedtls_md_init( &md_ctx );
5452
5453 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5455
5456 md_len = mbedtls_md_get_size( md_info );
5457
5458 tmp_len = md_len + strlen( label ) + rlen;
5459 tmp = mbedtls_calloc( 1, tmp_len );
5460 if( tmp == NULL )
5461 {
5462 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5463 goto exit;
5464 }
5465
5466 nb = strlen( label );
5467 memcpy( tmp + md_len, label, nb );
5468 memcpy( tmp + md_len + nb, random, rlen );
5469 nb += rlen;
5470
5471 /*
5472 * Compute P_<hash>(secret, label + random)[0..dlen]
5473 */
5474 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5475 goto exit;
5476
5477 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5478 if( ret != 0 )
5479 goto exit;
5480 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5481 if( ret != 0 )
5482 goto exit;
5483 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5484 if( ret != 0 )
5485 goto exit;
5486
5487 for( i = 0; i < dlen; i += md_len )
5488 {
5489 ret = mbedtls_md_hmac_reset ( &md_ctx );
5490 if( ret != 0 )
5491 goto exit;
5492 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5493 if( ret != 0 )
5494 goto exit;
5495 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5496 if( ret != 0 )
5497 goto exit;
5498
5499 ret = mbedtls_md_hmac_reset ( &md_ctx );
5500 if( ret != 0 )
5501 goto exit;
5502 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5503 if( ret != 0 )
5504 goto exit;
5505 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5506 if( ret != 0 )
5507 goto exit;
5508
5509 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5510
5511 for( j = 0; j < k; j++ )
5512 dstbuf[i + j] = h_i[j];
5513 }
5514
5515exit:
5516 mbedtls_md_free( &md_ctx );
5517
5518 mbedtls_platform_zeroize( tmp, tmp_len );
5519 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5520
5521 mbedtls_free( tmp );
5522
5523 return( ret );
5524}
5525#endif /* MBEDTLS_USE_PSA_CRYPTO */
5526
Andrzej Kurek25f27152022-08-17 16:09:31 -04005527#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005528MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005529static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5530 const char *label,
5531 const unsigned char *random, size_t rlen,
5532 unsigned char *dstbuf, size_t dlen )
5533{
5534 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5535 label, random, rlen, dstbuf, dlen ) );
5536}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005537#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005538
Andrzej Kurek25f27152022-08-17 16:09:31 -04005539#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005540MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005541static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5542 const char *label,
5543 const unsigned char *random, size_t rlen,
5544 unsigned char *dstbuf, size_t dlen )
5545{
5546 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5547 label, random, rlen, dstbuf, dlen ) );
5548}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005549#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005550
Jerry Yuf009d862022-02-17 14:01:37 +08005551/*
5552 * Set appropriate PRF function and other SSL / TLS1.2 functions
5553 *
5554 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005555 * - hash associated with the ciphersuite (only used by TLS 1.2)
5556 *
5557 * Outputs:
5558 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5559 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005560MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005561static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005562 mbedtls_md_type_t hash )
5563{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005564#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005565 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005566 {
5567 handshake->tls_prf = tls_prf_sha384;
5568 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5569 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5570 }
5571 else
5572#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005573#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005574 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005575 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005576 handshake->tls_prf = tls_prf_sha256;
5577 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5578 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5579 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005580#else
Jerry Yuf009d862022-02-17 14:01:37 +08005581 {
Jerry Yuf009d862022-02-17 14:01:37 +08005582 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005583 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5585 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005586#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005587
5588 return( 0 );
5589}
Jerry Yud6ab2352022-02-17 14:03:43 +08005590
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005591/*
5592 * Compute master secret if needed
5593 *
5594 * Parameters:
5595 * [in/out] handshake
5596 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5597 * (PSA-PSK) ciphersuite_info, psk_opaque
5598 * [out] premaster (cleared)
5599 * [out] master
5600 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5601 * debug: conf->f_dbg, conf->p_dbg
5602 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005603 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005604 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005605MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005606static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5607 unsigned char *master,
5608 const mbedtls_ssl_context *ssl )
5609{
5610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5611
5612 /* cf. RFC 5246, Section 8.1:
5613 * "The master secret is always exactly 48 bytes in length." */
5614 size_t const master_secret_len = 48;
5615
5616#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5617 unsigned char session_hash[48];
5618#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5619
5620 /* The label for the KDF used for key expansion.
5621 * This is either "master secret" or "extended master secret"
5622 * depending on whether the Extended Master Secret extension
5623 * is used. */
5624 char const *lbl = "master secret";
5625
Przemek Stekielae4ed302022-04-05 17:15:55 +02005626 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005627 * - If the Extended Master Secret extension is not used,
5628 * this is ClientHello.Random + ServerHello.Random
5629 * (see Sect. 8.1 in RFC 5246).
5630 * - If the Extended Master Secret extension is used,
5631 * this is the transcript of the handshake so far.
5632 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005633 unsigned char const *seed = handshake->randbytes;
5634 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005635
5636#if !defined(MBEDTLS_DEBUG_C) && \
5637 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5638 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5639 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5640 ssl = NULL; /* make sure we don't use it except for those cases */
5641 (void) ssl;
5642#endif
5643
5644 if( handshake->resume != 0 )
5645 {
5646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5647 return( 0 );
5648 }
5649
5650#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5651 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5652 {
5653 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005654 seed = session_hash;
5655 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005656
5657 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005658 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005659 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005660#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005661
Przemek Stekiel99114f32022-04-22 11:20:09 +02005662#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005663 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005664 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005665 {
5666 /* Perform PSK-to-MS expansion in a single step. */
5667 psa_status_t status;
5668 psa_algorithm_t alg;
5669 mbedtls_svc_key_id_t psk;
5670 psa_key_derivation_operation_t derivation =
5671 PSA_KEY_DERIVATION_OPERATION_INIT;
5672 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5673
5674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5675
5676 psk = mbedtls_ssl_get_opaque_psk( ssl );
5677
5678 if( hash_alg == MBEDTLS_MD_SHA384 )
5679 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5680 else
5681 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5682
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005683 size_t other_secret_len = 0;
5684 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005685
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005686 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005687 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005688 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005689 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005690 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005691 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005692 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005693 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005694 other_secret_len = 48;
5695 other_secret = handshake->premaster + 2;
5696 break;
5697 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005698 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005699 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5700 other_secret = handshake->premaster + 2;
5701 break;
5702 default:
5703 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005704 }
5705
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005706 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005707 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005708 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005709 (unsigned char const *) lbl,
5710 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005711 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005712 master_secret_len );
5713 if( status != PSA_SUCCESS )
5714 {
5715 psa_key_derivation_abort( &derivation );
5716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5717 }
5718
5719 status = psa_key_derivation_output_bytes( &derivation,
5720 master,
5721 master_secret_len );
5722 if( status != PSA_SUCCESS )
5723 {
5724 psa_key_derivation_abort( &derivation );
5725 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5726 }
5727
5728 status = psa_key_derivation_abort( &derivation );
5729 if( status != PSA_SUCCESS )
5730 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5731 }
5732 else
5733#endif
5734 {
5735 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005736 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005737 master,
5738 master_secret_len );
5739 if( ret != 0 )
5740 {
5741 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5742 return( ret );
5743 }
5744
5745 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5746 handshake->premaster,
5747 handshake->pmslen );
5748
5749 mbedtls_platform_zeroize( handshake->premaster,
5750 sizeof(handshake->premaster) );
5751 }
5752
5753 return( 0 );
5754}
5755
Jerry Yud62f87e2022-02-17 14:09:02 +08005756int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5757{
5758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5759 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5760 ssl->handshake->ciphersuite_info;
5761
5762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5763
5764 /* Set PRF, calc_verify and calc_finished function pointers */
5765 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005766 ciphersuite_info->mac );
5767 if( ret != 0 )
5768 {
5769 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5770 return( ret );
5771 }
5772
5773 /* Compute master secret if needed */
5774 ret = ssl_compute_master( ssl->handshake,
5775 ssl->session_negotiate->master,
5776 ssl );
5777 if( ret != 0 )
5778 {
5779 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5780 return( ret );
5781 }
5782
5783 /* Swap the client and server random values:
5784 * - MS derivation wanted client+server (RFC 5246 8.1)
5785 * - key derivation wants server+client (RFC 5246 6.3) */
5786 {
5787 unsigned char tmp[64];
5788 memcpy( tmp, ssl->handshake->randbytes, 64 );
5789 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5790 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5791 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5792 }
5793
5794 /* Populate transform structure */
5795 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5796 ssl->session_negotiate->ciphersuite,
5797 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005798#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005799 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005800#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005801 ssl->handshake->tls_prf,
5802 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005803 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005804 ssl->conf->endpoint,
5805 ssl );
5806 if( ret != 0 )
5807 {
5808 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5809 return( ret );
5810 }
5811
5812 /* We no longer need Server/ClientHello.random values */
5813 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5814 sizeof( ssl->handshake->randbytes ) );
5815
5816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5817
5818 return( 0 );
5819}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005820
Ronald Cron4dcbca92022-03-07 10:21:40 +01005821int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5822{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005823 switch( md )
5824 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005825#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005826 case MBEDTLS_SSL_HASH_SHA384:
5827 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5828 break;
5829#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005830#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005831 case MBEDTLS_SSL_HASH_SHA256:
5832 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5833 break;
5834#endif
5835 default:
5836 return( -1 );
5837 }
5838
Ronald Cronc2f13a02022-03-07 10:25:24 +01005839 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005840}
5841
Andrzej Kurek25f27152022-08-17 16:09:31 -04005842#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005843void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5844 unsigned char *hash,
5845 size_t *hlen )
5846{
5847#if defined(MBEDTLS_USE_PSA_CRYPTO)
5848 size_t hash_size;
5849 psa_status_t status;
5850 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5851
5852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5853 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5854 if( status != PSA_SUCCESS )
5855 {
5856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5857 return;
5858 }
5859
5860 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5861 if( status != PSA_SUCCESS )
5862 {
5863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5864 return;
5865 }
5866
5867 *hlen = 32;
5868 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5870#else
5871 mbedtls_sha256_context sha256;
5872
5873 mbedtls_sha256_init( &sha256 );
5874
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5876
5877 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5878 mbedtls_sha256_finish( &sha256, hash );
5879
5880 *hlen = 32;
5881
5882 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5884
5885 mbedtls_sha256_free( &sha256 );
5886#endif /* MBEDTLS_USE_PSA_CRYPTO */
5887 return;
5888}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005889#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005890
Andrzej Kurek25f27152022-08-17 16:09:31 -04005891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005892void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5893 unsigned char *hash,
5894 size_t *hlen )
5895{
5896#if defined(MBEDTLS_USE_PSA_CRYPTO)
5897 size_t hash_size;
5898 psa_status_t status;
5899 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5900
5901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5902 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5903 if( status != PSA_SUCCESS )
5904 {
5905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5906 return;
5907 }
5908
5909 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5910 if( status != PSA_SUCCESS )
5911 {
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5913 return;
5914 }
5915
5916 *hlen = 48;
5917 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5919#else
5920 mbedtls_sha512_context sha512;
5921
5922 mbedtls_sha512_init( &sha512 );
5923
5924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5925
Andrzej Kureka242e832022-08-11 10:03:14 -04005926 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005927 mbedtls_sha512_finish( &sha512, hash );
5928
5929 *hlen = 48;
5930
5931 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5933
5934 mbedtls_sha512_free( &sha512 );
5935#endif /* MBEDTLS_USE_PSA_CRYPTO */
5936 return;
5937}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005938#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005939
Neil Armstrong80f6f322022-05-03 17:56:38 +02005940#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5941 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005942int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5943{
5944 unsigned char *p = ssl->handshake->premaster;
5945 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5946 const unsigned char *psk = NULL;
5947 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005948 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005949
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005950 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005951 {
5952 /*
5953 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005954 * checked before calling this function.
5955 *
5956 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005957 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005958 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005959 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5960 {
5961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5963 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005964 }
5965
5966 /*
5967 * PMS = struct {
5968 * opaque other_secret<0..2^16-1>;
5969 * opaque psk<0..2^16-1>;
5970 * };
5971 * with "other_secret" depending on the particular key exchange
5972 */
5973#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5974 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5975 {
5976 if( end - p < 2 )
5977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5978
5979 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5980 p += 2;
5981
5982 if( end < p || (size_t)( end - p ) < psk_len )
5983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5984
5985 memset( p, 0, psk_len );
5986 p += psk_len;
5987 }
5988 else
5989#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5990#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5991 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5992 {
5993 /*
5994 * other_secret already set by the ClientKeyExchange message,
5995 * and is 48 bytes long
5996 */
5997 if( end - p < 2 )
5998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5999
6000 *p++ = 0;
6001 *p++ = 48;
6002 p += 48;
6003 }
6004 else
6005#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6006#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6007 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6008 {
6009 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6010 size_t len;
6011
6012 /* Write length only when we know the actual value */
6013 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6014 p + 2, end - ( p + 2 ), &len,
6015 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6016 {
6017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6018 return( ret );
6019 }
6020 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6021 p += 2 + len;
6022
6023 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6024 }
6025 else
6026#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006027#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006028 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6029 {
6030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6031 size_t zlen;
6032
6033 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6034 p + 2, end - ( p + 2 ),
6035 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6036 {
6037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6038 return( ret );
6039 }
6040
6041 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6042 p += 2 + zlen;
6043
6044 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6045 MBEDTLS_DEBUG_ECDH_Z );
6046 }
6047 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006048#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006049 {
6050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6052 }
6053
6054 /* opaque psk<0..2^16-1>; */
6055 if( end - p < 2 )
6056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6057
6058 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6059 p += 2;
6060
6061 if( end < p || (size_t)( end - p ) < psk_len )
6062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6063
6064 memcpy( p, psk, psk_len );
6065 p += psk_len;
6066
6067 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6068
6069 return( 0 );
6070}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006071#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006072
6073#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006074MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006075static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6076
6077#if defined(MBEDTLS_SSL_PROTO_DTLS)
6078int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6079{
6080 /* If renegotiation is not enforced, retransmit until we would reach max
6081 * timeout if we were using the usual handshake doubling scheme */
6082 if( ssl->conf->renego_max_records < 0 )
6083 {
6084 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6085 unsigned char doublings = 1;
6086
6087 while( ratio != 0 )
6088 {
6089 ++doublings;
6090 ratio >>= 1;
6091 }
6092
6093 if( ++ssl->renego_records_seen > doublings )
6094 {
6095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6096 return( 0 );
6097 }
6098 }
6099
6100 return( ssl_write_hello_request( ssl ) );
6101}
6102#endif
6103#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006104
Jerry Yud9526692022-02-17 14:23:47 +08006105/*
6106 * Handshake functions
6107 */
6108#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6109/* No certificate support -> dummy functions */
6110int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6111{
6112 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6113 ssl->handshake->ciphersuite_info;
6114
6115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6116
6117 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6118 {
6119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6120 ssl->state++;
6121 return( 0 );
6122 }
6123
6124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6126}
6127
6128int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6129{
6130 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6131 ssl->handshake->ciphersuite_info;
6132
6133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6134
6135 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6136 {
6137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6138 ssl->state++;
6139 return( 0 );
6140 }
6141
6142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6144}
6145
6146#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6147/* Some certificate support -> implement write and parse */
6148
6149int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6150{
6151 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6152 size_t i, n;
6153 const mbedtls_x509_crt *crt;
6154 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6155 ssl->handshake->ciphersuite_info;
6156
6157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6158
6159 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6160 {
6161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6162 ssl->state++;
6163 return( 0 );
6164 }
6165
6166#if defined(MBEDTLS_SSL_CLI_C)
6167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6168 {
6169 if( ssl->handshake->client_auth == 0 )
6170 {
6171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6172 ssl->state++;
6173 return( 0 );
6174 }
6175 }
6176#endif /* MBEDTLS_SSL_CLI_C */
6177#if defined(MBEDTLS_SSL_SRV_C)
6178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6179 {
6180 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6181 {
6182 /* Should never happen because we shouldn't have picked the
6183 * ciphersuite if we don't have a certificate. */
6184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6185 }
6186 }
6187#endif
6188
6189 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6190
6191 /*
6192 * 0 . 0 handshake type
6193 * 1 . 3 handshake length
6194 * 4 . 6 length of all certs
6195 * 7 . 9 length of cert. 1
6196 * 10 . n-1 peer certificate
6197 * n . n+2 length of cert. 2
6198 * n+3 . ... upper level cert, etc.
6199 */
6200 i = 7;
6201 crt = mbedtls_ssl_own_cert( ssl );
6202
6203 while( crt != NULL )
6204 {
6205 n = crt->raw.len;
6206 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6207 {
6208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6209 " > %" MBEDTLS_PRINTF_SIZET,
6210 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6211 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6212 }
6213
6214 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6215 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6216 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6217
6218 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6219 i += n; crt = crt->next;
6220 }
6221
6222 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6223 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6224 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6225
6226 ssl->out_msglen = i;
6227 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6228 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6229
6230 ssl->state++;
6231
6232 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6233 {
6234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6235 return( ret );
6236 }
6237
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6239
6240 return( ret );
6241}
6242
6243#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6244
6245#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006246MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006247static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6248 unsigned char *crt_buf,
6249 size_t crt_buf_len )
6250{
6251 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6252
6253 if( peer_crt == NULL )
6254 return( -1 );
6255
6256 if( peer_crt->raw.len != crt_buf_len )
6257 return( -1 );
6258
6259 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6260}
6261#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006262MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006263static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6264 unsigned char *crt_buf,
6265 size_t crt_buf_len )
6266{
6267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6268 unsigned char const * const peer_cert_digest =
6269 ssl->session->peer_cert_digest;
6270 mbedtls_md_type_t const peer_cert_digest_type =
6271 ssl->session->peer_cert_digest_type;
6272 mbedtls_md_info_t const * const digest_info =
6273 mbedtls_md_info_from_type( peer_cert_digest_type );
6274 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6275 size_t digest_len;
6276
6277 if( peer_cert_digest == NULL || digest_info == NULL )
6278 return( -1 );
6279
6280 digest_len = mbedtls_md_get_size( digest_info );
6281 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6282 return( -1 );
6283
6284 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6285 if( ret != 0 )
6286 return( -1 );
6287
6288 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6289}
6290#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6291#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6292
6293/*
6294 * Once the certificate message is read, parse it into a cert chain and
6295 * perform basic checks, but leave actual verification to the caller
6296 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006297MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006298static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6299 mbedtls_x509_crt *chain )
6300{
6301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6302#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6303 int crt_cnt=0;
6304#endif
6305 size_t i, n;
6306 uint8_t alert;
6307
6308 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6309 {
6310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6311 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6312 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6313 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6314 }
6315
6316 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6317 {
6318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6320 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6321 }
6322
6323 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6324 {
6325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6326 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6327 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6328 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6329 }
6330
6331 i = mbedtls_ssl_hs_hdr_len( ssl );
6332
6333 /*
6334 * Same message structure as in mbedtls_ssl_write_certificate()
6335 */
6336 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6337
6338 if( ssl->in_msg[i] != 0 ||
6339 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6340 {
6341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6342 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6343 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6344 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6345 }
6346
6347 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6348 i += 3;
6349
6350 /* Iterate through and parse the CRTs in the provided chain. */
6351 while( i < ssl->in_hslen )
6352 {
6353 /* Check that there's room for the next CRT's length fields. */
6354 if ( i + 3 > ssl->in_hslen ) {
6355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6356 mbedtls_ssl_send_alert_message( ssl,
6357 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6358 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6359 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6360 }
6361 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6362 * anything beyond 2**16 ~ 64K. */
6363 if( ssl->in_msg[i] != 0 )
6364 {
6365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6366 mbedtls_ssl_send_alert_message( ssl,
6367 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6368 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6369 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6370 }
6371
6372 /* Read length of the next CRT in the chain. */
6373 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6374 | (unsigned int) ssl->in_msg[i + 2];
6375 i += 3;
6376
6377 if( n < 128 || i + n > ssl->in_hslen )
6378 {
6379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6380 mbedtls_ssl_send_alert_message( ssl,
6381 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6382 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6383 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6384 }
6385
6386 /* Check if we're handling the first CRT in the chain. */
6387#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6388 if( crt_cnt++ == 0 &&
6389 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6390 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6391 {
6392 /* During client-side renegotiation, check that the server's
6393 * end-CRTs hasn't changed compared to the initial handshake,
6394 * mitigating the triple handshake attack. On success, reuse
6395 * the original end-CRT instead of parsing it again. */
6396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6397 if( ssl_check_peer_crt_unchanged( ssl,
6398 &ssl->in_msg[i],
6399 n ) != 0 )
6400 {
6401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6402 mbedtls_ssl_send_alert_message( ssl,
6403 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6404 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6405 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6406 }
6407
6408 /* Now we can safely free the original chain. */
6409 ssl_clear_peer_cert( ssl->session );
6410 }
6411#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6412
6413 /* Parse the next certificate in the chain. */
6414#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6415 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6416#else
6417 /* If we don't need to store the CRT chain permanently, parse
6418 * it in-place from the input buffer instead of making a copy. */
6419 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6420#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6421 switch( ret )
6422 {
6423 case 0: /*ok*/
6424 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6425 /* Ignore certificate with an unknown algorithm: maybe a
6426 prior certificate was already trusted. */
6427 break;
6428
6429 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6430 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6431 goto crt_parse_der_failed;
6432
6433 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6434 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6435 goto crt_parse_der_failed;
6436
6437 default:
6438 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6439 crt_parse_der_failed:
6440 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6441 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6442 return( ret );
6443 }
6444
6445 i += n;
6446 }
6447
6448 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6449 return( 0 );
6450}
6451
6452#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006453MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006454static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6455{
6456 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6457 return( -1 );
6458
6459 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6460 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6461 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6462 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6463 {
Ronald Cron19385882022-06-15 16:26:13 +02006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006465 return( 0 );
6466 }
6467 return( -1 );
6468}
6469#endif /* MBEDTLS_SSL_SRV_C */
6470
6471/* Check if a certificate message is expected.
6472 * Return either
6473 * - SSL_CERTIFICATE_EXPECTED, or
6474 * - SSL_CERTIFICATE_SKIP
6475 * indicating whether a Certificate message is expected or not.
6476 */
6477#define SSL_CERTIFICATE_EXPECTED 0
6478#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006479MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006480static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6481 int authmode )
6482{
6483 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6484 ssl->handshake->ciphersuite_info;
6485
6486 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6487 return( SSL_CERTIFICATE_SKIP );
6488
6489#if defined(MBEDTLS_SSL_SRV_C)
6490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6491 {
6492 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6493 return( SSL_CERTIFICATE_SKIP );
6494
6495 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6496 {
6497 ssl->session_negotiate->verify_result =
6498 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6499 return( SSL_CERTIFICATE_SKIP );
6500 }
6501 }
6502#else
6503 ((void) authmode);
6504#endif /* MBEDTLS_SSL_SRV_C */
6505
6506 return( SSL_CERTIFICATE_EXPECTED );
6507}
6508
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006509MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006510static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6511 int authmode,
6512 mbedtls_x509_crt *chain,
6513 void *rs_ctx )
6514{
6515 int ret = 0;
6516 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6517 ssl->handshake->ciphersuite_info;
6518 int have_ca_chain = 0;
6519
6520 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6521 void *p_vrfy;
6522
6523 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6524 return( 0 );
6525
6526 if( ssl->f_vrfy != NULL )
6527 {
6528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6529 f_vrfy = ssl->f_vrfy;
6530 p_vrfy = ssl->p_vrfy;
6531 }
6532 else
6533 {
6534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6535 f_vrfy = ssl->conf->f_vrfy;
6536 p_vrfy = ssl->conf->p_vrfy;
6537 }
6538
6539 /*
6540 * Main check: verify certificate
6541 */
6542#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6543 if( ssl->conf->f_ca_cb != NULL )
6544 {
6545 ((void) rs_ctx);
6546 have_ca_chain = 1;
6547
6548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6549 ret = mbedtls_x509_crt_verify_with_ca_cb(
6550 chain,
6551 ssl->conf->f_ca_cb,
6552 ssl->conf->p_ca_cb,
6553 ssl->conf->cert_profile,
6554 ssl->hostname,
6555 &ssl->session_negotiate->verify_result,
6556 f_vrfy, p_vrfy );
6557 }
6558 else
6559#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6560 {
6561 mbedtls_x509_crt *ca_chain;
6562 mbedtls_x509_crl *ca_crl;
6563
6564#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6565 if( ssl->handshake->sni_ca_chain != NULL )
6566 {
6567 ca_chain = ssl->handshake->sni_ca_chain;
6568 ca_crl = ssl->handshake->sni_ca_crl;
6569 }
6570 else
6571#endif
6572 {
6573 ca_chain = ssl->conf->ca_chain;
6574 ca_crl = ssl->conf->ca_crl;
6575 }
6576
6577 if( ca_chain != NULL )
6578 have_ca_chain = 1;
6579
6580 ret = mbedtls_x509_crt_verify_restartable(
6581 chain,
6582 ca_chain, ca_crl,
6583 ssl->conf->cert_profile,
6584 ssl->hostname,
6585 &ssl->session_negotiate->verify_result,
6586 f_vrfy, p_vrfy, rs_ctx );
6587 }
6588
6589 if( ret != 0 )
6590 {
6591 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6592 }
6593
6594#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6595 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6596 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6597#endif
6598
6599 /*
6600 * Secondary checks: always done, but change 'ret' only if it was 0
6601 */
6602
6603#if defined(MBEDTLS_ECP_C)
6604 {
6605 const mbedtls_pk_context *pk = &chain->pk;
6606
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006607 /* If certificate uses an EC key, make sure the curve is OK.
6608 * This is a public key, so it can't be opaque, so can_do() is a good
6609 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006610 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006611 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006612 /* and in the unlikely case the above assumption no longer holds
6613 * we are making sure that pk_ec() here does not return a NULL
6614 */
6615 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6616 if( ec == NULL )
6617 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6620 }
Jerry Yud9526692022-02-17 14:23:47 +08006621
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006622 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6623 {
6624 ssl->session_negotiate->verify_result |=
6625 MBEDTLS_X509_BADCERT_BAD_KEY;
6626
6627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6628 if( ret == 0 )
6629 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6630 }
Jerry Yud9526692022-02-17 14:23:47 +08006631 }
6632 }
6633#endif /* MBEDTLS_ECP_C */
6634
6635 if( mbedtls_ssl_check_cert_usage( chain,
6636 ciphersuite_info,
6637 ! ssl->conf->endpoint,
6638 &ssl->session_negotiate->verify_result ) != 0 )
6639 {
6640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6641 if( ret == 0 )
6642 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6643 }
6644
6645 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6646 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6647 * with details encoded in the verification flags. All other kinds
6648 * of error codes, including those from the user provided f_vrfy
6649 * functions, are treated as fatal and lead to a failure of
6650 * ssl_parse_certificate even if verification was optional. */
6651 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6652 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6653 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6654 {
6655 ret = 0;
6656 }
6657
6658 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6659 {
6660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6661 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6662 }
6663
6664 if( ret != 0 )
6665 {
6666 uint8_t alert;
6667
6668 /* The certificate may have been rejected for several reasons.
6669 Pick one and send the corresponding alert. Which alert to send
6670 may be a subject of debate in some cases. */
6671 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6672 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6673 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6674 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6675 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6676 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6677 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6678 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6679 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6680 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6681 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6682 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6683 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6684 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6685 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6686 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6687 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6688 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6689 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6690 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6691 else
6692 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6693 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6694 alert );
6695 }
6696
6697#if defined(MBEDTLS_DEBUG_C)
6698 if( ssl->session_negotiate->verify_result != 0 )
6699 {
6700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6701 (unsigned int) ssl->session_negotiate->verify_result ) );
6702 }
6703 else
6704 {
6705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6706 }
6707#endif /* MBEDTLS_DEBUG_C */
6708
6709 return( ret );
6710}
6711
6712#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006713MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006714static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6715 unsigned char *start, size_t len )
6716{
6717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6718 /* Remember digest of the peer's end-CRT. */
6719 ssl->session_negotiate->peer_cert_digest =
6720 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6721 if( ssl->session_negotiate->peer_cert_digest == NULL )
6722 {
6723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6724 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6725 mbedtls_ssl_send_alert_message( ssl,
6726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6727 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6728
6729 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6730 }
6731
6732 ret = mbedtls_md( mbedtls_md_info_from_type(
6733 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6734 start, len,
6735 ssl->session_negotiate->peer_cert_digest );
6736
6737 ssl->session_negotiate->peer_cert_digest_type =
6738 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6739 ssl->session_negotiate->peer_cert_digest_len =
6740 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6741
6742 return( ret );
6743}
6744
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006745MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006746static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6747 unsigned char *start, size_t len )
6748{
6749 unsigned char *end = start + len;
6750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6751
6752 /* Make a copy of the peer's raw public key. */
6753 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6754 ret = mbedtls_pk_parse_subpubkey( &start, end,
6755 &ssl->handshake->peer_pubkey );
6756 if( ret != 0 )
6757 {
6758 /* We should have parsed the public key before. */
6759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6760 }
6761
6762 return( 0 );
6763}
6764#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6765
6766int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6767{
6768 int ret = 0;
6769 int crt_expected;
6770#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6771 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6772 ? ssl->handshake->sni_authmode
6773 : ssl->conf->authmode;
6774#else
6775 const int authmode = ssl->conf->authmode;
6776#endif
6777 void *rs_ctx = NULL;
6778 mbedtls_x509_crt *chain = NULL;
6779
6780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6781
6782 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6783 if( crt_expected == SSL_CERTIFICATE_SKIP )
6784 {
6785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6786 goto exit;
6787 }
6788
6789#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6790 if( ssl->handshake->ecrs_enabled &&
6791 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6792 {
6793 chain = ssl->handshake->ecrs_peer_cert;
6794 ssl->handshake->ecrs_peer_cert = NULL;
6795 goto crt_verify;
6796 }
6797#endif
6798
6799 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6800 {
6801 /* mbedtls_ssl_read_record may have sent an alert already. We
6802 let it decide whether to alert. */
6803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6804 goto exit;
6805 }
6806
6807#if defined(MBEDTLS_SSL_SRV_C)
6808 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6809 {
6810 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6811
6812 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6813 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6814
6815 goto exit;
6816 }
6817#endif /* MBEDTLS_SSL_SRV_C */
6818
6819 /* Clear existing peer CRT structure in case we tried to
6820 * reuse a session but it failed, and allocate a new one. */
6821 ssl_clear_peer_cert( ssl->session_negotiate );
6822
6823 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6824 if( chain == NULL )
6825 {
6826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6827 sizeof( mbedtls_x509_crt ) ) );
6828 mbedtls_ssl_send_alert_message( ssl,
6829 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6830 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6831
6832 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6833 goto exit;
6834 }
6835 mbedtls_x509_crt_init( chain );
6836
6837 ret = ssl_parse_certificate_chain( ssl, chain );
6838 if( ret != 0 )
6839 goto exit;
6840
6841#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6842 if( ssl->handshake->ecrs_enabled)
6843 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6844
6845crt_verify:
6846 if( ssl->handshake->ecrs_enabled)
6847 rs_ctx = &ssl->handshake->ecrs_ctx;
6848#endif
6849
6850 ret = ssl_parse_certificate_verify( ssl, authmode,
6851 chain, rs_ctx );
6852 if( ret != 0 )
6853 goto exit;
6854
6855#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6856 {
6857 unsigned char *crt_start, *pk_start;
6858 size_t crt_len, pk_len;
6859
6860 /* We parse the CRT chain without copying, so
6861 * these pointers point into the input buffer,
6862 * and are hence still valid after freeing the
6863 * CRT chain. */
6864
6865 crt_start = chain->raw.p;
6866 crt_len = chain->raw.len;
6867
6868 pk_start = chain->pk_raw.p;
6869 pk_len = chain->pk_raw.len;
6870
6871 /* Free the CRT structures before computing
6872 * digest and copying the peer's public key. */
6873 mbedtls_x509_crt_free( chain );
6874 mbedtls_free( chain );
6875 chain = NULL;
6876
6877 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6878 if( ret != 0 )
6879 goto exit;
6880
6881 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6882 if( ret != 0 )
6883 goto exit;
6884 }
6885#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6886 /* Pass ownership to session structure. */
6887 ssl->session_negotiate->peer_cert = chain;
6888 chain = NULL;
6889#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6890
6891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6892
6893exit:
6894
6895 if( ret == 0 )
6896 ssl->state++;
6897
6898#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6899 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6900 {
6901 ssl->handshake->ecrs_peer_cert = chain;
6902 chain = NULL;
6903 }
6904#endif
6905
6906 if( chain != NULL )
6907 {
6908 mbedtls_x509_crt_free( chain );
6909 mbedtls_free( chain );
6910 }
6911
6912 return( ret );
6913}
6914#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6915
Andrzej Kurek25f27152022-08-17 16:09:31 -04006916#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006917static void ssl_calc_finished_tls_sha256(
6918 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6919{
6920 int len = 12;
6921 const char *sender;
6922 unsigned char padbuf[32];
6923#if defined(MBEDTLS_USE_PSA_CRYPTO)
6924 size_t hash_size;
6925 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6926 psa_status_t status;
6927#else
6928 mbedtls_sha256_context sha256;
6929#endif
6930
6931 mbedtls_ssl_session *session = ssl->session_negotiate;
6932 if( !session )
6933 session = ssl->session;
6934
6935 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6936 ? "client finished"
6937 : "server finished";
6938
6939#if defined(MBEDTLS_USE_PSA_CRYPTO)
6940 sha256_psa = psa_hash_operation_init();
6941
6942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6943
6944 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6945 if( status != PSA_SUCCESS )
6946 {
6947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6948 return;
6949 }
6950
6951 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6952 if( status != PSA_SUCCESS )
6953 {
6954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6955 return;
6956 }
6957 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6958#else
6959
6960 mbedtls_sha256_init( &sha256 );
6961
6962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6963
6964 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6965
6966 /*
6967 * TLSv1.2:
6968 * hash = PRF( master, finished_label,
6969 * Hash( handshake ) )[0.11]
6970 */
6971
6972#if !defined(MBEDTLS_SHA256_ALT)
6973 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6974 sha256.state, sizeof( sha256.state ) );
6975#endif
6976
6977 mbedtls_sha256_finish( &sha256, padbuf );
6978 mbedtls_sha256_free( &sha256 );
6979#endif /* MBEDTLS_USE_PSA_CRYPTO */
6980
6981 ssl->handshake->tls_prf( session->master, 48, sender,
6982 padbuf, 32, buf, len );
6983
6984 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6985
6986 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6987
6988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6989}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006990#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006991
Jerry Yub7ba49e2022-02-17 14:25:53 +08006992
Andrzej Kurek25f27152022-08-17 16:09:31 -04006993#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006994static void ssl_calc_finished_tls_sha384(
6995 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6996{
6997 int len = 12;
6998 const char *sender;
6999 unsigned char padbuf[48];
7000#if defined(MBEDTLS_USE_PSA_CRYPTO)
7001 size_t hash_size;
7002 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7003 psa_status_t status;
7004#else
7005 mbedtls_sha512_context sha512;
7006#endif
7007
7008 mbedtls_ssl_session *session = ssl->session_negotiate;
7009 if( !session )
7010 session = ssl->session;
7011
7012 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7013 ? "client finished"
7014 : "server finished";
7015
7016#if defined(MBEDTLS_USE_PSA_CRYPTO)
7017 sha384_psa = psa_hash_operation_init();
7018
7019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7020
7021 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7022 if( status != PSA_SUCCESS )
7023 {
7024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7025 return;
7026 }
7027
7028 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7029 if( status != PSA_SUCCESS )
7030 {
7031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7032 return;
7033 }
7034 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7035#else
7036 mbedtls_sha512_init( &sha512 );
7037
7038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7039
Andrzej Kureka242e832022-08-11 10:03:14 -04007040 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007041
7042 /*
7043 * TLSv1.2:
7044 * hash = PRF( master, finished_label,
7045 * Hash( handshake ) )[0.11]
7046 */
7047
7048#if !defined(MBEDTLS_SHA512_ALT)
7049 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7050 sha512.state, sizeof( sha512.state ) );
7051#endif
7052 mbedtls_sha512_finish( &sha512, padbuf );
7053
7054 mbedtls_sha512_free( &sha512 );
7055#endif
7056
7057 ssl->handshake->tls_prf( session->master, 48, sender,
7058 padbuf, 48, buf, len );
7059
7060 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7061
7062 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7063
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7065}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007066#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007067
Jerry Yuaef00152022-02-17 14:27:31 +08007068void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7069{
7070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7071
7072 /*
7073 * Free our handshake params
7074 */
7075 mbedtls_ssl_handshake_free( ssl );
7076 mbedtls_free( ssl->handshake );
7077 ssl->handshake = NULL;
7078
7079 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007080 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007081 */
7082 if( ssl->transform )
7083 {
7084 mbedtls_ssl_transform_free( ssl->transform );
7085 mbedtls_free( ssl->transform );
7086 }
7087 ssl->transform = ssl->transform_negotiate;
7088 ssl->transform_negotiate = NULL;
7089
7090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7091}
7092
Jerry Yu2a9fff52022-02-17 14:28:51 +08007093void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7094{
7095 int resume = ssl->handshake->resume;
7096
7097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7098
7099#if defined(MBEDTLS_SSL_RENEGOTIATION)
7100 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7101 {
7102 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7103 ssl->renego_records_seen = 0;
7104 }
7105#endif
7106
7107 /*
7108 * Free the previous session and switch in the current one
7109 */
7110 if( ssl->session )
7111 {
7112#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7113 /* RFC 7366 3.1: keep the EtM state */
7114 ssl->session_negotiate->encrypt_then_mac =
7115 ssl->session->encrypt_then_mac;
7116#endif
7117
7118 mbedtls_ssl_session_free( ssl->session );
7119 mbedtls_free( ssl->session );
7120 }
7121 ssl->session = ssl->session_negotiate;
7122 ssl->session_negotiate = NULL;
7123
7124 /*
7125 * Add cache entry
7126 */
7127 if( ssl->conf->f_set_cache != NULL &&
7128 ssl->session->id_len != 0 &&
7129 resume == 0 )
7130 {
7131 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7132 ssl->session->id,
7133 ssl->session->id_len,
7134 ssl->session ) != 0 )
7135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7136 }
7137
7138#if defined(MBEDTLS_SSL_PROTO_DTLS)
7139 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7140 ssl->handshake->flight != NULL )
7141 {
7142 /* Cancel handshake timer */
7143 mbedtls_ssl_set_timer( ssl, 0 );
7144
7145 /* Keep last flight around in case we need to resend it:
7146 * we need the handshake and transform structures for that */
7147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7148 }
7149 else
7150#endif
7151 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7152
7153 ssl->state++;
7154
7155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7156}
7157
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007158int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7159{
7160 int ret, hash_len;
7161
7162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7163
7164 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7165
7166 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7167
7168 /*
7169 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7170 * may define some other value. Currently (early 2016), no defined
7171 * ciphersuite does this (and this is unlikely to change as activity has
7172 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7173 */
7174 hash_len = 12;
7175
7176#if defined(MBEDTLS_SSL_RENEGOTIATION)
7177 ssl->verify_data_len = hash_len;
7178 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7179#endif
7180
7181 ssl->out_msglen = 4 + hash_len;
7182 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7183 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7184
7185 /*
7186 * In case of session resuming, invert the client and server
7187 * ChangeCipherSpec messages order.
7188 */
7189 if( ssl->handshake->resume != 0 )
7190 {
7191#if defined(MBEDTLS_SSL_CLI_C)
7192 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7193 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7194#endif
7195#if defined(MBEDTLS_SSL_SRV_C)
7196 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7197 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7198#endif
7199 }
7200 else
7201 ssl->state++;
7202
7203 /*
7204 * Switch to our negotiated transform and session parameters for outbound
7205 * data.
7206 */
7207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7208
7209#if defined(MBEDTLS_SSL_PROTO_DTLS)
7210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7211 {
7212 unsigned char i;
7213
7214 /* Remember current epoch settings for resending */
7215 ssl->handshake->alt_transform_out = ssl->transform_out;
7216 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7217 sizeof( ssl->handshake->alt_out_ctr ) );
7218
7219 /* Set sequence_number to zero */
7220 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7221
7222
7223 /* Increment epoch */
7224 for( i = 2; i > 0; i-- )
7225 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7226 break;
7227
7228 /* The loop goes to its end iff the counter is wrapping */
7229 if( i == 0 )
7230 {
7231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7232 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7233 }
7234 }
7235 else
7236#endif /* MBEDTLS_SSL_PROTO_DTLS */
7237 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7238
7239 ssl->transform_out = ssl->transform_negotiate;
7240 ssl->session_out = ssl->session_negotiate;
7241
7242#if defined(MBEDTLS_SSL_PROTO_DTLS)
7243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7244 mbedtls_ssl_send_flight_completed( ssl );
7245#endif
7246
7247 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7248 {
7249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7250 return( ret );
7251 }
7252
7253#if defined(MBEDTLS_SSL_PROTO_DTLS)
7254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7255 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7256 {
7257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7258 return( ret );
7259 }
7260#endif
7261
7262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7263
7264 return( 0 );
7265}
7266
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007267#define SSL_MAX_HASH_LEN 12
7268
7269int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7270{
7271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7272 unsigned int hash_len = 12;
7273 unsigned char buf[SSL_MAX_HASH_LEN];
7274
7275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7276
7277 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7278
7279 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7280 {
7281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7282 goto exit;
7283 }
7284
7285 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7286 {
7287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
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_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7295 {
7296 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7297 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7298 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7299 goto exit;
7300 }
7301
7302 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7303 {
7304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7306 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7307 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7308 goto exit;
7309 }
7310
7311 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7312 buf, hash_len ) != 0 )
7313 {
7314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7315 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7316 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7317 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7318 goto exit;
7319 }
7320
7321#if defined(MBEDTLS_SSL_RENEGOTIATION)
7322 ssl->verify_data_len = hash_len;
7323 memcpy( ssl->peer_verify_data, buf, hash_len );
7324#endif
7325
7326 if( ssl->handshake->resume != 0 )
7327 {
7328#if defined(MBEDTLS_SSL_CLI_C)
7329 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7330 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7331#endif
7332#if defined(MBEDTLS_SSL_SRV_C)
7333 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7334 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7335#endif
7336 }
7337 else
7338 ssl->state++;
7339
7340#if defined(MBEDTLS_SSL_PROTO_DTLS)
7341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7342 mbedtls_ssl_recv_flight_completed( ssl );
7343#endif
7344
7345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7346
7347exit:
7348 mbedtls_platform_zeroize( buf, hash_len );
7349 return( ret );
7350}
7351
Jerry Yu392112c2022-02-17 14:34:10 +08007352#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7353/*
7354 * Helper to get TLS 1.2 PRF from ciphersuite
7355 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7356 */
7357static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7358{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007359#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007360 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7361 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7362
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007363 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007364 return( tls_prf_sha384 );
7365#else
7366 (void) ciphersuite_id;
7367#endif
7368 return( tls_prf_sha256 );
7369}
7370#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007371
7372static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7373{
7374 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007375#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007376 if( tls_prf == tls_prf_sha384 )
7377 {
7378 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7379 }
7380 else
7381#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007382#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007383 if( tls_prf == tls_prf_sha256 )
7384 {
7385 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7386 }
7387 else
7388#endif
7389 return( MBEDTLS_SSL_TLS_PRF_NONE );
7390}
7391
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007392/*
7393 * Populate a transform structure with session keys and all the other
7394 * necessary information.
7395 *
7396 * Parameters:
7397 * - [in/out]: transform: structure to populate
7398 * [in] must be just initialised with mbedtls_ssl_transform_init()
7399 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7400 * - [in] ciphersuite
7401 * - [in] master
7402 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007403 * - [in] tls_prf: pointer to PRF to use for key derivation
7404 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007405 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007406 * - [in] endpoint: client or server
7407 * - [in] ssl: used for:
7408 * - ssl->conf->{f,p}_export_keys
7409 * [in] optionally used for:
7410 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7411 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007412MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007413static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7414 int ciphersuite,
7415 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007416#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007417 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007418#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007419 ssl_tls_prf_t tls_prf,
7420 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007421 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007422 unsigned endpoint,
7423 const mbedtls_ssl_context *ssl )
7424{
7425 int ret = 0;
7426 unsigned char keyblk[256];
7427 unsigned char *key1;
7428 unsigned char *key2;
7429 unsigned char *mac_enc;
7430 unsigned char *mac_dec;
7431 size_t mac_key_len = 0;
7432 size_t iv_copy_len;
7433 size_t keylen;
7434 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007435 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007436#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007437 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007438 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007439#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007440
7441#if defined(MBEDTLS_USE_PSA_CRYPTO)
7442 psa_key_type_t key_type;
7443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7444 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007445 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007446 size_t key_bits;
7447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7448#endif
7449
7450#if !defined(MBEDTLS_DEBUG_C) && \
7451 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7452 if( ssl->f_export_keys == NULL )
7453 {
7454 ssl = NULL; /* make sure we don't use it except for these cases */
7455 (void) ssl;
7456 }
7457#endif
7458
7459 /*
7460 * Some data just needs copying into the structure
7461 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007462#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007463 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007464#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007465 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007466
7467#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7468 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7469#endif
7470
7471#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007472 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007473 {
7474 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7475 * generation separate. This should never happen. */
7476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7477 }
7478#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7479
7480 /*
7481 * Get various info structures
7482 */
7483 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7484 if( ciphersuite_info == NULL )
7485 {
7486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7487 ciphersuite ) );
7488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7489 }
7490
Neil Armstrongab555e02022-04-04 11:07:59 +02007491 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007492#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007493 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007494#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007495 ciphersuite_info );
7496
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007497 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7498 transform->taglen =
7499 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7500
7501#if defined(MBEDTLS_USE_PSA_CRYPTO)
7502 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7503 transform->taglen,
7504 &alg,
7505 &key_type,
7506 &key_bits ) ) != PSA_SUCCESS )
7507 {
7508 ret = psa_ssl_status_to_mbedtls( status );
7509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7510 goto end;
7511 }
7512#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007513 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7514 if( cipher_info == NULL )
7515 {
7516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7517 ciphersuite_info->cipher ) );
7518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7519 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007520#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007521
Neil Armstronge4512952022-03-08 09:08:22 +01007522#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007523 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007524 if( mac_alg == 0 )
7525 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007527 (unsigned) ciphersuite_info->mac ) );
7528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7529 }
7530#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007531 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7532 if( md_info == NULL )
7533 {
7534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7535 (unsigned) ciphersuite_info->mac ) );
7536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7537 }
Neil Armstronge4512952022-03-08 09:08:22 +01007538#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007539
7540#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7541 /* Copy own and peer's CID if the use of the CID
7542 * extension has been negotiated. */
7543 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7544 {
7545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7546
7547 transform->in_cid_len = ssl->own_cid_len;
7548 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7549 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7550 transform->in_cid_len );
7551
7552 transform->out_cid_len = ssl->handshake->peer_cid_len;
7553 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7554 ssl->handshake->peer_cid_len );
7555 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7556 transform->out_cid_len );
7557 }
7558#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7559
7560 /*
7561 * Compute key block using the PRF
7562 */
7563 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7564 if( ret != 0 )
7565 {
7566 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7567 return( ret );
7568 }
7569
7570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7571 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7572 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7573 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7574 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7575
7576 /*
7577 * Determine the appropriate key, IV and MAC length.
7578 */
7579
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007580#if defined(MBEDTLS_USE_PSA_CRYPTO)
7581 keylen = PSA_BITS_TO_BYTES(key_bits);
7582#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007583 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007584#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007585
7586#if defined(MBEDTLS_GCM_C) || \
7587 defined(MBEDTLS_CCM_C) || \
7588 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007589 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007590 {
7591 size_t explicit_ivlen;
7592
7593 transform->maclen = 0;
7594 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007595
7596 /* All modes haves 96-bit IVs, but the length of the static parts vary
7597 * with mode and version:
7598 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7599 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7600 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7601 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7602 * sequence number).
7603 */
7604 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007605#if defined(MBEDTLS_USE_PSA_CRYPTO)
7606 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7607#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007608 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007609#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007610 transform->fixed_ivlen = 12;
7611 else
7612 transform->fixed_ivlen = 4;
7613
7614 /* Minimum length of encrypted record */
7615 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7616 transform->minlen = explicit_ivlen + transform->taglen;
7617 }
7618 else
7619#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7620#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007621 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7622 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7623 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007624 {
Neil Armstronge4512952022-03-08 09:08:22 +01007625#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007626 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007627#else
7628 size_t block_size = cipher_info->block_size;
7629#endif /* MBEDTLS_USE_PSA_CRYPTO */
7630
7631#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007632 /* Get MAC length */
7633 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7634#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007635 /* Initialize HMAC contexts */
7636 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7637 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7638 {
7639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7640 goto end;
7641 }
7642
7643 /* Get MAC length */
7644 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007645#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007646 transform->maclen = mac_key_len;
7647
7648 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007649#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007650 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007651#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007652 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007653#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007654
7655 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007656 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007657 transform->minlen = transform->maclen;
7658 else
7659 {
7660 /*
7661 * GenericBlockCipher:
7662 * 1. if EtM is in use: one block plus MAC
7663 * otherwise: * first multiple of blocklen greater than maclen
7664 * 2. IV
7665 */
7666#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007667 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007668 {
7669 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007670 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007671 }
7672 else
7673#endif
7674 {
7675 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007676 + block_size
7677 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007678 }
7679
Glenn Strauss07c64162022-03-14 12:34:51 -04007680 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007681 {
7682 transform->minlen += transform->ivlen;
7683 }
7684 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007685 {
7686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7687 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7688 goto end;
7689 }
7690 }
7691 }
7692 else
7693#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7694 {
7695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7697 }
7698
7699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7700 (unsigned) keylen,
7701 (unsigned) transform->minlen,
7702 (unsigned) transform->ivlen,
7703 (unsigned) transform->maclen ) );
7704
7705 /*
7706 * Finally setup the cipher contexts, IVs and MAC secrets.
7707 */
7708#if defined(MBEDTLS_SSL_CLI_C)
7709 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7710 {
7711 key1 = keyblk + mac_key_len * 2;
7712 key2 = keyblk + mac_key_len * 2 + keylen;
7713
7714 mac_enc = keyblk;
7715 mac_dec = keyblk + mac_key_len;
7716
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007717 iv_copy_len = ( transform->fixed_ivlen ) ?
7718 transform->fixed_ivlen : transform->ivlen;
7719 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7720 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7721 iv_copy_len );
7722 }
7723 else
7724#endif /* MBEDTLS_SSL_CLI_C */
7725#if defined(MBEDTLS_SSL_SRV_C)
7726 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7727 {
7728 key1 = keyblk + mac_key_len * 2 + keylen;
7729 key2 = keyblk + mac_key_len * 2;
7730
7731 mac_enc = keyblk + mac_key_len;
7732 mac_dec = keyblk;
7733
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 iv_copy_len = ( transform->fixed_ivlen ) ?
7735 transform->fixed_ivlen : transform->ivlen;
7736 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7737 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7738 iv_copy_len );
7739 }
7740 else
7741#endif /* MBEDTLS_SSL_SRV_C */
7742 {
7743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7744 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7745 goto end;
7746 }
7747
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007748 if( ssl != NULL && ssl->f_export_keys != NULL )
7749 {
7750 ssl->f_export_keys( ssl->p_export_keys,
7751 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7752 master, 48,
7753 randbytes + 32,
7754 randbytes,
7755 tls_prf_get_type( tls_prf ) );
7756 }
7757
7758#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759 transform->psa_alg = alg;
7760
7761 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7762 {
7763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7764 psa_set_key_algorithm( &attributes, alg );
7765 psa_set_key_type( &attributes, key_type );
7766
7767 if( ( status = psa_import_key( &attributes,
7768 key1,
7769 PSA_BITS_TO_BYTES( key_bits ),
7770 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7771 {
7772 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7773 ret = psa_ssl_status_to_mbedtls( status );
7774 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7775 goto end;
7776 }
7777
7778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7779
7780 if( ( status = psa_import_key( &attributes,
7781 key2,
7782 PSA_BITS_TO_BYTES( key_bits ),
7783 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7784 {
7785 ret = psa_ssl_status_to_mbedtls( status );
7786 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7787 goto end;
7788 }
7789 }
7790#else
7791 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7792 cipher_info ) ) != 0 )
7793 {
7794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7795 goto end;
7796 }
7797
7798 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7799 cipher_info ) ) != 0 )
7800 {
7801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7802 goto end;
7803 }
7804
7805 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7806 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7807 MBEDTLS_ENCRYPT ) ) != 0 )
7808 {
7809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7810 goto end;
7811 }
7812
7813 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7814 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7815 MBEDTLS_DECRYPT ) ) != 0 )
7816 {
7817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7818 goto end;
7819 }
7820
7821#if defined(MBEDTLS_CIPHER_MODE_CBC)
7822 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7823 {
7824 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7825 MBEDTLS_PADDING_NONE ) ) != 0 )
7826 {
7827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7828 goto end;
7829 }
7830
7831 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7832 MBEDTLS_PADDING_NONE ) ) != 0 )
7833 {
7834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7835 goto end;
7836 }
7837 }
7838#endif /* MBEDTLS_CIPHER_MODE_CBC */
7839#endif /* MBEDTLS_USE_PSA_CRYPTO */
7840
Neil Armstrong29c0c042022-03-17 17:47:28 +01007841#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7842 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7843 For AEAD-based ciphersuites, there is nothing to do here. */
7844 if( mac_key_len != 0 )
7845 {
7846#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007847 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007848
7849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007850 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007851 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7852
7853 if( ( status = psa_import_key( &attributes,
7854 mac_enc, mac_key_len,
7855 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7856 {
7857 ret = psa_ssl_status_to_mbedtls( status );
7858 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7859 goto end;
7860 }
7861
Ronald Cronfb39f152022-03-25 14:36:28 +01007862 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007863 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7864#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7865 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7866#endif
7867 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007868 /* mbedtls_ct_hmac() requires the key to be exportable */
7869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7870 PSA_KEY_USAGE_VERIFY_HASH );
7871 else
7872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7873
7874 if( ( status = psa_import_key( &attributes,
7875 mac_dec, mac_key_len,
7876 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7877 {
7878 ret = psa_ssl_status_to_mbedtls( status );
7879 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7880 goto end;
7881 }
7882#else
7883 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7884 if( ret != 0 )
7885 goto end;
7886 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7887 if( ret != 0 )
7888 goto end;
7889#endif /* MBEDTLS_USE_PSA_CRYPTO */
7890 }
7891#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7892
7893 ((void) mac_dec);
7894 ((void) mac_enc);
7895
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007896end:
7897 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7898 return( ret );
7899}
7900
Jerry Yuee40f9d2022-02-17 14:55:16 +08007901#if defined(MBEDTLS_USE_PSA_CRYPTO)
7902int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7903 unsigned char *hash, size_t *hashlen,
7904 unsigned char *data, size_t data_len,
7905 mbedtls_md_type_t md_alg )
7906{
7907 psa_status_t status;
7908 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007909 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007910
7911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7912
7913 if( ( status = psa_hash_setup( &hash_operation,
7914 hash_alg ) ) != PSA_SUCCESS )
7915 {
7916 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7917 goto exit;
7918 }
7919
7920 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7921 64 ) ) != PSA_SUCCESS )
7922 {
7923 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7924 goto exit;
7925 }
7926
7927 if( ( status = psa_hash_update( &hash_operation,
7928 data, data_len ) ) != PSA_SUCCESS )
7929 {
7930 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7931 goto exit;
7932 }
7933
7934 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7935 hashlen ) ) != PSA_SUCCESS )
7936 {
7937 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7938 goto exit;
7939 }
7940
7941exit:
7942 if( status != PSA_SUCCESS )
7943 {
7944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7945 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7946 switch( status )
7947 {
7948 case PSA_ERROR_NOT_SUPPORTED:
7949 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7950 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7951 case PSA_ERROR_BUFFER_TOO_SMALL:
7952 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7953 case PSA_ERROR_INSUFFICIENT_MEMORY:
7954 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7955 default:
7956 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7957 }
7958 }
7959 return( 0 );
7960}
7961
7962#else
7963
7964int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7965 unsigned char *hash, size_t *hashlen,
7966 unsigned char *data, size_t data_len,
7967 mbedtls_md_type_t md_alg )
7968{
7969 int ret = 0;
7970 mbedtls_md_context_t ctx;
7971 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7972 *hashlen = mbedtls_md_get_size( md_info );
7973
7974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7975
7976 mbedtls_md_init( &ctx );
7977
7978 /*
7979 * digitally-signed struct {
7980 * opaque client_random[32];
7981 * opaque server_random[32];
7982 * ServerDHParams params;
7983 * };
7984 */
7985 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7986 {
7987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7988 goto exit;
7989 }
7990 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7991 {
7992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7993 goto exit;
7994 }
7995 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7996 {
7997 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7998 goto exit;
7999 }
8000 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8001 {
8002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8003 goto exit;
8004 }
8005 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8006 {
8007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8008 goto exit;
8009 }
8010
8011exit:
8012 mbedtls_md_free( &ctx );
8013
8014 if( ret != 0 )
8015 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8016 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8017
8018 return( ret );
8019}
8020#endif /* MBEDTLS_USE_PSA_CRYPTO */
8021
Jerry Yud9d91da2022-02-17 14:57:06 +08008022#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8023
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008024/* Find the preferred hash for a given signature algorithm. */
8025unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8026 mbedtls_ssl_context *ssl,
8027 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008028{
Gabor Mezei078e8032022-04-27 21:17:56 +02008029 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008030 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008031
8032 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008033 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008034
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008035 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008036 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008037 unsigned int hash_alg_received =
8038 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8039 received_sig_algs[i] );
8040 unsigned int sig_alg_received =
8041 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8042 received_sig_algs[i] );
8043
8044 if( sig_alg == sig_alg_received )
8045 {
8046#if defined(MBEDTLS_USE_PSA_CRYPTO)
8047 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8048 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008049 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008050 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008051
Neil Armstrong96eceb82022-06-30 18:05:05 +02008052 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8053 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8054 PSA_ALG_ECDSA( psa_hash_alg ),
8055 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008056 continue;
8057
Neil Armstrong96eceb82022-06-30 18:05:05 +02008058 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008059 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8060 PSA_ALG_RSA_PKCS1V15_SIGN(
8061 psa_hash_alg ),
8062 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008063 continue;
8064 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008066
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008067 return( hash_alg_received );
8068 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008069 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008070
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008071 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008072}
8073
8074#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8075
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008076/* Serialization of TLS 1.2 sessions:
8077 *
8078 * struct {
8079 * uint64 start_time;
8080 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008081 * uint8 session_id_len; // at most 32
8082 * opaque session_id[32];
8083 * opaque master[48]; // fixed length in the standard
8084 * uint32 verify_result;
8085 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8086 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8087 * uint32 ticket_lifetime;
8088 * uint8 mfl_code; // up to 255 according to standard
8089 * uint8 encrypt_then_mac; // 0 or 1
8090 * } serialized_session_tls12;
8091 *
8092 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008093static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008094 unsigned char *buf,
8095 size_t buf_len )
8096{
8097 unsigned char *p = buf;
8098 size_t used = 0;
8099
8100#if defined(MBEDTLS_HAVE_TIME)
8101 uint64_t start;
8102#endif
8103#if defined(MBEDTLS_X509_CRT_PARSE_C)
8104#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8105 size_t cert_len;
8106#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8107#endif /* MBEDTLS_X509_CRT_PARSE_C */
8108
8109 /*
8110 * Time
8111 */
8112#if defined(MBEDTLS_HAVE_TIME)
8113 used += 8;
8114
8115 if( used <= buf_len )
8116 {
8117 start = (uint64_t) session->start;
8118
8119 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8120 p += 8;
8121 }
8122#endif /* MBEDTLS_HAVE_TIME */
8123
8124 /*
8125 * Basic mandatory fields
8126 */
8127 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008128 + 1 /* id_len */
8129 + sizeof( session->id )
8130 + sizeof( session->master )
8131 + 4; /* verify_result */
8132
8133 if( used <= buf_len )
8134 {
8135 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8136 p += 2;
8137
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008138 *p++ = MBEDTLS_BYTE_0( session->id_len );
8139 memcpy( p, session->id, 32 );
8140 p += 32;
8141
8142 memcpy( p, session->master, 48 );
8143 p += 48;
8144
8145 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8146 p += 4;
8147 }
8148
8149 /*
8150 * Peer's end-entity certificate
8151 */
8152#if defined(MBEDTLS_X509_CRT_PARSE_C)
8153#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8154 if( session->peer_cert == NULL )
8155 cert_len = 0;
8156 else
8157 cert_len = session->peer_cert->raw.len;
8158
8159 used += 3 + cert_len;
8160
8161 if( used <= buf_len )
8162 {
8163 *p++ = MBEDTLS_BYTE_2( cert_len );
8164 *p++ = MBEDTLS_BYTE_1( cert_len );
8165 *p++ = MBEDTLS_BYTE_0( cert_len );
8166
8167 if( session->peer_cert != NULL )
8168 {
8169 memcpy( p, session->peer_cert->raw.p, cert_len );
8170 p += cert_len;
8171 }
8172 }
8173#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8174 if( session->peer_cert_digest != NULL )
8175 {
8176 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8177 if( used <= buf_len )
8178 {
8179 *p++ = (unsigned char) session->peer_cert_digest_type;
8180 *p++ = (unsigned char) session->peer_cert_digest_len;
8181 memcpy( p, session->peer_cert_digest,
8182 session->peer_cert_digest_len );
8183 p += session->peer_cert_digest_len;
8184 }
8185 }
8186 else
8187 {
8188 used += 2;
8189 if( used <= buf_len )
8190 {
8191 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8192 *p++ = 0;
8193 }
8194 }
8195#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8196#endif /* MBEDTLS_X509_CRT_PARSE_C */
8197
8198 /*
8199 * Session ticket if any, plus associated data
8200 */
8201#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8202 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8203
8204 if( used <= buf_len )
8205 {
8206 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8207 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8208 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8209
8210 if( session->ticket != NULL )
8211 {
8212 memcpy( p, session->ticket, session->ticket_len );
8213 p += session->ticket_len;
8214 }
8215
8216 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8217 p += 4;
8218 }
8219#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8220
8221 /*
8222 * Misc extension-related info
8223 */
8224#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8225 used += 1;
8226
8227 if( used <= buf_len )
8228 *p++ = session->mfl_code;
8229#endif
8230
8231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8232 used += 1;
8233
8234 if( used <= buf_len )
8235 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8236#endif
8237
8238 return( used );
8239}
8240
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008241MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008242static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008243 const unsigned char *buf,
8244 size_t len )
8245{
8246#if defined(MBEDTLS_HAVE_TIME)
8247 uint64_t start;
8248#endif
8249#if defined(MBEDTLS_X509_CRT_PARSE_C)
8250#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8251 size_t cert_len;
8252#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8253#endif /* MBEDTLS_X509_CRT_PARSE_C */
8254
8255 const unsigned char *p = buf;
8256 const unsigned char * const end = buf + len;
8257
8258 /*
8259 * Time
8260 */
8261#if defined(MBEDTLS_HAVE_TIME)
8262 if( 8 > (size_t)( end - p ) )
8263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8264
8265 start = ( (uint64_t) p[0] << 56 ) |
8266 ( (uint64_t) p[1] << 48 ) |
8267 ( (uint64_t) p[2] << 40 ) |
8268 ( (uint64_t) p[3] << 32 ) |
8269 ( (uint64_t) p[4] << 24 ) |
8270 ( (uint64_t) p[5] << 16 ) |
8271 ( (uint64_t) p[6] << 8 ) |
8272 ( (uint64_t) p[7] );
8273 p += 8;
8274
8275 session->start = (time_t) start;
8276#endif /* MBEDTLS_HAVE_TIME */
8277
8278 /*
8279 * Basic mandatory fields
8280 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008281 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8283
8284 session->ciphersuite = ( p[0] << 8 ) | p[1];
8285 p += 2;
8286
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008287 session->id_len = *p++;
8288 memcpy( session->id, p, 32 );
8289 p += 32;
8290
8291 memcpy( session->master, p, 48 );
8292 p += 48;
8293
8294 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8295 ( (uint32_t) p[1] << 16 ) |
8296 ( (uint32_t) p[2] << 8 ) |
8297 ( (uint32_t) p[3] );
8298 p += 4;
8299
8300 /* Immediately clear invalid pointer values that have been read, in case
8301 * we exit early before we replaced them with valid ones. */
8302#if defined(MBEDTLS_X509_CRT_PARSE_C)
8303#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8304 session->peer_cert = NULL;
8305#else
8306 session->peer_cert_digest = NULL;
8307#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8308#endif /* MBEDTLS_X509_CRT_PARSE_C */
8309#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8310 session->ticket = NULL;
8311#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8312
8313 /*
8314 * Peer certificate
8315 */
8316#if defined(MBEDTLS_X509_CRT_PARSE_C)
8317#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8318 /* Deserialize CRT from the end of the ticket. */
8319 if( 3 > (size_t)( end - p ) )
8320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8321
8322 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8323 p += 3;
8324
8325 if( cert_len != 0 )
8326 {
8327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8328
8329 if( cert_len > (size_t)( end - p ) )
8330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8331
8332 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8333
8334 if( session->peer_cert == NULL )
8335 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8336
8337 mbedtls_x509_crt_init( session->peer_cert );
8338
8339 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8340 p, cert_len ) ) != 0 )
8341 {
8342 mbedtls_x509_crt_free( session->peer_cert );
8343 mbedtls_free( session->peer_cert );
8344 session->peer_cert = NULL;
8345 return( ret );
8346 }
8347
8348 p += cert_len;
8349 }
8350#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8351 /* Deserialize CRT digest from the end of the ticket. */
8352 if( 2 > (size_t)( end - p ) )
8353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8354
8355 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8356 session->peer_cert_digest_len = (size_t) *p++;
8357
8358 if( session->peer_cert_digest_len != 0 )
8359 {
8360 const mbedtls_md_info_t *md_info =
8361 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8362 if( md_info == NULL )
8363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8364 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8366
8367 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8369
8370 session->peer_cert_digest =
8371 mbedtls_calloc( 1, session->peer_cert_digest_len );
8372 if( session->peer_cert_digest == NULL )
8373 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8374
8375 memcpy( session->peer_cert_digest, p,
8376 session->peer_cert_digest_len );
8377 p += session->peer_cert_digest_len;
8378 }
8379#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8380#endif /* MBEDTLS_X509_CRT_PARSE_C */
8381
8382 /*
8383 * Session ticket and associated data
8384 */
8385#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8386 if( 3 > (size_t)( end - p ) )
8387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8388
8389 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8390 p += 3;
8391
8392 if( session->ticket_len != 0 )
8393 {
8394 if( session->ticket_len > (size_t)( end - p ) )
8395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8396
8397 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8398 if( session->ticket == NULL )
8399 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8400
8401 memcpy( session->ticket, p, session->ticket_len );
8402 p += session->ticket_len;
8403 }
8404
8405 if( 4 > (size_t)( end - p ) )
8406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8407
8408 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8409 ( (uint32_t) p[1] << 16 ) |
8410 ( (uint32_t) p[2] << 8 ) |
8411 ( (uint32_t) p[3] );
8412 p += 4;
8413#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8414
8415 /*
8416 * Misc extension-related info
8417 */
8418#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8419 if( 1 > (size_t)( end - p ) )
8420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8421
8422 session->mfl_code = *p++;
8423#endif
8424
8425#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8426 if( 1 > (size_t)( end - p ) )
8427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8428
8429 session->encrypt_then_mac = *p++;
8430#endif
8431
8432 /* Done, should have consumed entire buffer */
8433 if( p != end )
8434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8435
8436 return( 0 );
8437}
Jerry Yudc7bd172022-02-17 13:44:15 +08008438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8439
XiaokangQian75d40ef2022-04-20 11:05:24 +00008440int mbedtls_ssl_validate_ciphersuite(
8441 const mbedtls_ssl_context *ssl,
8442 const mbedtls_ssl_ciphersuite_t *suite_info,
8443 mbedtls_ssl_protocol_version min_tls_version,
8444 mbedtls_ssl_protocol_version max_tls_version )
8445{
8446 (void) ssl;
8447
8448 if( suite_info == NULL )
8449 return( -1 );
8450
8451 if( ( suite_info->min_tls_version > max_tls_version ) ||
8452 ( suite_info->max_tls_version < min_tls_version ) )
8453 {
8454 return( -1 );
8455 }
8456
XiaokangQian060d8672022-04-21 09:24:56 +00008457#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008458#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8459 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8460 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8461 {
8462 return( -1 );
8463 }
8464#endif
8465
8466 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8467#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8468 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8469 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8470 {
8471 return( -1 );
8472 }
8473#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8474#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8475
8476 return( 0 );
8477}
8478
XiaokangQianeaf36512022-04-24 09:07:44 +00008479#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8480/*
8481 * Function for writing a signature algorithm extension.
8482 *
8483 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8484 * value (TLS 1.3 RFC8446):
8485 * enum {
8486 * ....
8487 * ecdsa_secp256r1_sha256( 0x0403 ),
8488 * ecdsa_secp384r1_sha384( 0x0503 ),
8489 * ecdsa_secp521r1_sha512( 0x0603 ),
8490 * ....
8491 * } SignatureScheme;
8492 *
8493 * struct {
8494 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8495 * } SignatureSchemeList;
8496 *
8497 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8498 * value (TLS 1.2 RFC5246):
8499 * enum {
8500 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8501 * sha512(6), (255)
8502 * } HashAlgorithm;
8503 *
8504 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8505 * SignatureAlgorithm;
8506 *
8507 * struct {
8508 * HashAlgorithm hash;
8509 * SignatureAlgorithm signature;
8510 * } SignatureAndHashAlgorithm;
8511 *
8512 * SignatureAndHashAlgorithm
8513 * supported_signature_algorithms<2..2^16-2>;
8514 *
8515 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8516 * generalization of the TLS 1.2 signature algorithm extension.
8517 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8518 * `SignatureScheme` field of TLS 1.3
8519 *
8520 */
8521int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8522 const unsigned char *end, size_t *out_len )
8523{
8524 unsigned char *p = buf;
8525 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8526 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8527
8528 *out_len = 0;
8529
8530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8531
8532 /* Check if we have space for header and length field:
8533 * - extension_type (2 bytes)
8534 * - extension_data_length (2 bytes)
8535 * - supported_signature_algorithms_length (2 bytes)
8536 */
8537 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8538 p += 6;
8539
8540 /*
8541 * Write supported_signature_algorithms
8542 */
8543 supported_sig_alg = p;
8544 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8545 if( sig_alg == NULL )
8546 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8547
8548 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8549 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8551 *sig_alg,
8552 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008553 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8554 continue;
8555 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8556 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8557 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008559 *sig_alg,
8560 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008561 }
8562
8563 /* Length of supported_signature_algorithms */
8564 supported_sig_alg_len = p - supported_sig_alg;
8565 if( supported_sig_alg_len == 0 )
8566 {
8567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8568 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8569 }
8570
XiaokangQianeaf36512022-04-24 09:07:44 +00008571 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008572 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008573 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8574
XiaokangQianeaf36512022-04-24 09:07:44 +00008575 *out_len = p - buf;
8576
8577#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8578 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8579#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8580 return( 0 );
8581}
8582#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8583
XiaokangQian40a35232022-05-07 09:02:40 +00008584#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008585/*
8586 * mbedtls_ssl_parse_server_name_ext
8587 *
8588 * Structure of server_name extension:
8589 *
8590 * enum {
8591 * host_name(0), (255)
8592 * } NameType;
8593 * opaque HostName<1..2^16-1>;
8594 *
8595 * struct {
8596 * NameType name_type;
8597 * select (name_type) {
8598 * case host_name: HostName;
8599 * } name;
8600 * } ServerName;
8601 * struct {
8602 * ServerName server_name_list<1..2^16-1>
8603 * } ServerNameList;
8604 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008605MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008606int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8607 const unsigned char *buf,
8608 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008609{
8610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8611 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008612 size_t server_name_list_len, hostname_len;
8613 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008614
XiaokangQianf2a94202022-05-20 06:44:24 +00008615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008616
8617 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008618 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008619 p += 2;
8620
XiaokangQian9b2b7712022-05-17 02:57:00 +00008621 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8622 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008623 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008624 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008625 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008626 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8628 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008629
8630 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8631 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008632 /* sni_name is intended to be used only during the parsing of the
8633 * ClientHello message (it is reset to NULL before the end of
8634 * the message parsing). Thus it is ok to just point to the
8635 * reception buffer and not make a copy of it.
8636 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008637 ssl->handshake->sni_name = p + 3;
8638 ssl->handshake->sni_name_len = hostname_len;
8639 if( ssl->conf->f_sni == NULL )
8640 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008641 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008642 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008643 if( ret != 0 )
8644 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008646 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8647 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008648 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8649 }
8650 return( 0 );
8651 }
8652
8653 p += hostname_len + 3;
8654 }
8655
8656 return( 0 );
8657}
8658#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8659
XiaokangQianacb39922022-06-17 10:18:48 +00008660#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008661MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008662int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8663 const unsigned char *buf,
8664 const unsigned char *end )
8665{
8666 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008667 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008668 const unsigned char *protocol_name_list;
8669 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008670 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008671
8672 /* If ALPN not configured, just ignore the extension */
8673 if( ssl->conf->alpn_list == NULL )
8674 return( 0 );
8675
8676 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008677 * RFC7301, section 3.1
8678 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008679 *
XiaokangQianc7403452022-06-23 03:24:12 +00008680 * struct {
8681 * ProtocolName protocol_name_list<2..2^16-1>
8682 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008683 */
8684
XiaokangQianc7403452022-06-23 03:24:12 +00008685 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008686 * protocol_name_list_len 2 bytes
8687 * protocol_name_len 1 bytes
8688 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008689 */
XiaokangQianacb39922022-06-17 10:18:48 +00008690 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8691
XiaokangQianc7403452022-06-23 03:24:12 +00008692 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008693 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008694 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008695 protocol_name_list = p;
8696 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008697
8698 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008699 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008700 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008701 protocol_name_len = *p++;
8702 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8703 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008704 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008705 {
8706 MBEDTLS_SSL_PEND_FATAL_ALERT(
8707 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8708 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008709 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008710 }
8711
8712 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008713 }
8714
8715 /* Use our order of preference */
8716 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8717 {
8718 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008719 p = protocol_name_list;
8720 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008721 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008722 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008723 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008724 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008725 {
8726 ssl->alpn_chosen = *alpn;
8727 return( 0 );
8728 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008729
8730 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008731 }
8732 }
8733
XiaokangQian95d5f542022-06-24 02:29:26 +00008734 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008735 MBEDTLS_SSL_PEND_FATAL_ALERT(
8736 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8737 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8738 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8739}
8740
8741int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8742 unsigned char *buf,
8743 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008744 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008745{
8746 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008747 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008748 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008749
8750 if( ssl->alpn_chosen == NULL )
8751 {
8752 return( 0 );
8753 }
8754
XiaokangQian95d5f542022-06-24 02:29:26 +00008755 protocol_name_len = strlen( ssl->alpn_chosen );
8756 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008757
8758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8759 /*
8760 * 0 . 1 ext identifier
8761 * 2 . 3 ext length
8762 * 4 . 5 protocol list length
8763 * 6 . 6 protocol name length
8764 * 7 . 7+n protocol name
8765 */
8766 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8767
XiaokangQian95d5f542022-06-24 02:29:26 +00008768 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008769
XiaokangQian95d5f542022-06-24 02:29:26 +00008770 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8771 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008772 /* Note: the length of the chosen protocol has been checked to be less
8773 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8774 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008775 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008776
XiaokangQian95d5f542022-06-24 02:29:26 +00008777 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008778 return ( 0 );
8779}
8780#endif /* MBEDTLS_SSL_ALPN */
8781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#endif /* MBEDTLS_SSL_TLS_C */