blob: d32d58e2df85af132ac4ab76e3556b542e5fafe4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
300 return( 0 );
301}
302
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500303#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
306{
307 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
308 if( resized_buffer == NULL )
309 return -1;
310
311 /* We want to copy len_new bytes when downsizing the buffer, and
312 * len_old bytes when upsizing, so we choose the smaller of two sizes,
313 * to fit one buffer into another. Size checks, ensuring that no data is
314 * lost, are done outside of this function. */
315 memcpy( resized_buffer, *buffer,
316 ( len_new < *len_old ) ? len_new : *len_old );
317 mbedtls_platform_zeroize( *buffer, *len_old );
318 mbedtls_free( *buffer );
319
320 *buffer = resized_buffer;
321 *len_old = len_new;
322
323 return 0;
324}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325
326static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500327 size_t in_buf_new_len,
328 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200329{
330 int modified = 0;
331 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
332 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
333 if( ssl->in_buf != NULL )
334 {
335 written_in = ssl->in_msg - ssl->in_buf;
336 iv_offset_in = ssl->in_iv - ssl->in_buf;
337 len_offset_in = ssl->in_len - ssl->in_buf;
338 if( downsizing ?
339 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
340 ssl->in_buf_len < in_buf_new_len )
341 {
342 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
345 }
346 else
347 {
Paul Elliottb7449902021-03-10 18:14:58 +0000348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
349 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 modified = 1;
351 }
352 }
353 }
354
355 if( ssl->out_buf != NULL )
356 {
357 written_out = ssl->out_msg - ssl->out_buf;
358 iv_offset_out = ssl->out_iv - ssl->out_buf;
359 len_offset_out = ssl->out_len - ssl->out_buf;
360 if( downsizing ?
361 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
362 ssl->out_buf_len < out_buf_new_len )
363 {
364 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
365 {
366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
367 }
368 else
369 {
Paul Elliottb7449902021-03-10 18:14:58 +0000370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
371 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 modified = 1;
373 }
374 }
375 }
376 if( modified )
377 {
378 /* Update pointers here to avoid doing it twice. */
379 mbedtls_ssl_reset_in_out_pointers( ssl );
380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Jerry Yudb8c48a2022-01-27 14:54:54 +0800393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
396typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
397 const char *label,
398 const unsigned char *random, size_t rlen,
399 unsigned char *dstbuf, size_t dlen );
400
401static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
402
403#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
404
405/* Type for the TLS PRF */
406typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
407 const unsigned char *, size_t,
408 unsigned char *, size_t);
409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800411static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
412 int ciphersuite,
413 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800415 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200416#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800417 ssl_tls_prf_t tls_prf,
418 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400419 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800420 unsigned endpoint,
421 const mbedtls_ssl_context *ssl );
422
Andrzej Kurek25f27152022-08-17 16:09:31 -0400423#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800425static int tls_prf_sha256( const unsigned char *secret, size_t slen,
426 const char *label,
427 const unsigned char *random, size_t rlen,
428 unsigned char *dstbuf, size_t dlen );
429static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
430static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
431
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400432#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800433
Andrzej Kurek25f27152022-08-17 16:09:31 -0400434#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800436static int tls_prf_sha384( const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen );
440
441static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
442static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800444
Jerry Yu438ddd82022-07-07 06:55:50 +0000445static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800446 unsigned char *buf,
447 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800448
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000450static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800451 const unsigned char *buf,
452 size_t len );
453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
454
Jerry Yu53d23e22022-02-09 16:25:09 +0800455static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
456
Andrzej Kurek25f27152022-08-17 16:09:31 -0400457#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800458static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400459#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800460
Andrzej Kurek25f27152022-08-17 16:09:31 -0400461#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800462static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400463#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000464
Ron Eldor51d3ab52019-05-12 14:54:30 +0300465int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
466 const unsigned char *secret, size_t slen,
467 const char *label,
468 const unsigned char *random, size_t rlen,
469 unsigned char *dstbuf, size_t dlen )
470{
471 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
472
473 switch( prf )
474 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400476#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA384:
478 tls_prf = tls_prf_sha384;
479 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA256:
483 tls_prf = tls_prf_sha256;
484 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 default:
488 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
489 }
490
491 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
495static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
498 if( session->peer_cert != NULL )
499 {
500 mbedtls_x509_crt_free( session->peer_cert );
501 mbedtls_free( session->peer_cert );
502 session->peer_cert = NULL;
503 }
504#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
505 if( session->peer_cert_digest != NULL )
506 {
507 /* Zeroization is not necessary. */
508 mbedtls_free( session->peer_cert_digest );
509 session->peer_cert_digest = NULL;
510 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
511 session->peer_cert_digest_len = 0;
512 }
513#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
514}
515#endif /* MBEDTLS_X509_CRT_PARSE_C */
516
517void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
519{
520 ((void) ciphersuite_info);
521
Andrzej Kurek25f27152022-08-17 16:09:31 -0400522#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800523 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
524 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
525 else
526#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400527#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800528 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
529 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
530 else
531#endif
532 {
533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
534 return;
535 }
536}
537
XiaokangQianadab9a62022-07-18 07:41:26 +0000538void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
539 unsigned hs_type,
540 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100541{
542 unsigned char hs_hdr[4];
543
544 /* Build HS header for checksum update. */
545 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
546 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
547 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
548 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
549
550 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
551}
552
553void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
554 unsigned hs_type,
555 unsigned char const *msg,
556 size_t msg_len )
557{
558 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
559 ssl->handshake->update_checksum( ssl, msg, msg_len );
560}
561
Jerry Yuc73c6182022-02-08 20:29:25 +0800562void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
563{
564 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400565#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800566#if defined(MBEDTLS_USE_PSA_CRYPTO)
567 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
568 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
569#else
570 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
571#endif
572#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800574#if defined(MBEDTLS_USE_PSA_CRYPTO)
575 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
576 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
577#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400578 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800579#endif
580#endif
581}
582
583static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
584 const unsigned char *buf, size_t len )
585{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800587#if defined(MBEDTLS_USE_PSA_CRYPTO)
588 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
589#else
590 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
591#endif
592#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
596#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400597 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#endif
599#endif
600}
601
Andrzej Kurek25f27152022-08-17 16:09:31 -0400602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800603static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
604 const unsigned char *buf, size_t len )
605{
606#if defined(MBEDTLS_USE_PSA_CRYPTO)
607 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
608#else
609 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
610#endif
611}
612#endif
613
Andrzej Kurek25f27152022-08-17 16:09:31 -0400614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800615static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
616 const unsigned char *buf, size_t len )
617{
618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
620#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400621 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800622#endif
623}
624#endif
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200629
Andrzej Kurek25f27152022-08-17 16:09:31 -0400630#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 handshake->fin_sha256_psa = psa_hash_operation_init();
633 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
634#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200636 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500641 handshake->fin_sha384_psa = psa_hash_operation_init();
642 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500643#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400644 mbedtls_sha512_init( &handshake->fin_sha384 );
645 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648
649 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_DHM_C)
652 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200653#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200654#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200658 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200659#if defined(MBEDTLS_SSL_CLI_C)
660 handshake->ecjpake_cache = NULL;
661 handshake->ecjpake_cache_len = 0;
662#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200663#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200664
Gilles Peskineeccd8882020-03-10 12:19:08 +0100665#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200666 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200667#endif
668
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
670 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
671#endif
Hanno Becker75173122019-02-06 16:18:31 +0000672
673#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
674 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
675 mbedtls_pk_init( &handshake->peer_pubkey );
676#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200677}
678
Hanno Beckera18d1322018-01-03 14:27:32 +0000679void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200682
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100683#if defined(MBEDTLS_USE_PSA_CRYPTO)
684 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
685 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100686#else
687 mbedtls_cipher_init( &transform->cipher_ctx_enc );
688 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100689#endif
690
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000691#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100692#if defined(MBEDTLS_USE_PSA_CRYPTO)
693 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
694 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100695#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_init( &transform->md_ctx_enc );
697 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000698#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100699#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200700}
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705}
706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200707MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000709{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000711 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200716 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717
718 /*
719 * Either the pointers are now NULL or cleared properly and can be freed.
720 * Now allocate missing structures.
721 */
722 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200724 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200725 }
Paul Bakker48916f92012-09-16 19:57:18 +0000726
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200728 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200729 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 }
Paul Bakker48916f92012-09-16 19:57:18 +0000731
Paul Bakker82788fb2014-10-20 13:59:19 +0200732 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500736#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
737 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400738
Andrzej Kurek4a063792020-10-21 15:08:44 +0200739 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
740 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500741#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200743 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000744 if( ssl->handshake == NULL ||
745 ssl->transform_negotiate == NULL ||
746 ssl->session_negotiate == NULL )
747 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_free( ssl->handshake );
751 mbedtls_free( ssl->transform_negotiate );
752 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753
754 ssl->handshake = NULL;
755 ssl->transform_negotiate = NULL;
756 ssl->session_negotiate = NULL;
757
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 }
760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000763 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200764 ssl_handshake_params_init( ssl->handshake );
765
Jerry Yud0766ec2022-09-22 10:46:57 +0800766#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
767 defined(MBEDTLS_SSL_SRV_C) && \
768 defined(MBEDTLS_SSL_SESSION_TICKETS)
769 ssl->handshake->new_session_tickets_count =
770 ssl->conf->new_session_tickets_count ;
771#endif
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
775 {
776 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200777
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200778 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
779 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
780 else
781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200782
Hanno Becker0f57a652020-02-05 10:37:26 +0000783 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200784 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200785#endif
786
Brett Warrene0edc842021-08-17 09:53:13 +0100787/*
788 * curve_list is translated to IANA TLS group identifiers here because
789 * mbedtls_ssl_conf_curves returns void and so can't return
790 * any error codes.
791 */
792#if defined(MBEDTLS_ECP_C)
793#if !defined(MBEDTLS_DEPRECATED_REMOVED)
794 /* Heap allocate and translate curve_list from internal to IANA group ids */
795 if ( ssl->conf->curve_list != NULL )
796 {
797 size_t length;
798 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
799
800 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
801 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
802
803 /* Leave room for zero termination */
804 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
805 if ( group_list == NULL )
806 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
807
808 for( size_t i = 0; i < length; i++ )
809 {
810 const mbedtls_ecp_curve_info *info =
811 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
812 if ( info == NULL )
813 {
814 mbedtls_free( group_list );
815 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
816 }
817 group_list[i] = info->tls_id;
818 }
819
820 group_list[length] = 0;
821
822 ssl->handshake->group_list = group_list;
823 ssl->handshake->group_list_heap_allocated = 1;
824 }
825 else
826 {
827 ssl->handshake->group_list = ssl->conf->group_list;
828 ssl->handshake->group_list_heap_allocated = 0;
829 }
830#endif /* MBEDTLS_DEPRECATED_REMOVED */
831#endif /* MBEDTLS_ECP_C */
832
Jerry Yuf017ee42022-01-12 15:49:48 +0800833#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
834#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800836 /* Heap allocate and translate sig_hashes from internal hash identifiers to
837 signature algorithms IANA identifiers. */
838 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800839 ssl->conf->sig_hashes != NULL )
840 {
841 const int *md;
842 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800843 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800844 uint16_t *p;
845
Jerry Yub476a442022-01-21 18:14:45 +0800846#if defined(static_assert)
847 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
848 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
849 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800850#endif
851
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
853 {
854 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
855 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800856#if defined(MBEDTLS_ECDSA_C)
857 sig_algs_len += sizeof( uint16_t );
858#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800859
Jerry Yub476a442022-01-21 18:14:45 +0800860#if defined(MBEDTLS_RSA_C)
861 sig_algs_len += sizeof( uint16_t );
862#endif
863 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
864 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 }
866
Jerry Yub476a442022-01-21 18:14:45 +0800867 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800868 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
869
Jerry Yub476a442022-01-21 18:14:45 +0800870 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
871 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800872 if( ssl->handshake->sig_algs == NULL )
873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
874
875 p = (uint16_t *)ssl->handshake->sig_algs;
876 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
877 {
878 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
879 if( hash == MBEDTLS_SSL_HASH_NONE )
880 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800881#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
883 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800884#endif
885#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
887 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800888#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200890 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800891 ssl->handshake->sig_algs_heap_allocated = 1;
892 }
893 else
Jerry Yua69269a2022-01-17 21:06:01 +0800894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 ssl->handshake->sig_algs_heap_allocated = 0;
897 }
Jerry Yucc539102022-06-27 16:27:35 +0800898#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800899#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000900 return( 0 );
901}
902
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200903#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200904/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200905MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200906static int ssl_cookie_write_dummy( void *ctx,
907 unsigned char **p, unsigned char *end,
908 const unsigned char *cli_id, size_t cli_id_len )
909{
910 ((void) ctx);
911 ((void) p);
912 ((void) end);
913 ((void) cli_id);
914 ((void) cli_id_len);
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917}
918
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200919MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200920static int ssl_cookie_check_dummy( void *ctx,
921 const unsigned char *cookie, size_t cookie_len,
922 const unsigned char *cli_id, size_t cli_id_len )
923{
924 ((void) ctx);
925 ((void) cookie);
926 ((void) cookie_len);
927 ((void) cli_id);
928 ((void) cli_id_len);
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200932#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * Initialize an SSL context
936 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200937void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
938{
939 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
940}
941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200942MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800943static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
944{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100945 const mbedtls_ssl_config *conf = ssl->conf;
946
Ronald Cron6f135e12021-12-08 16:57:54 +0100947#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100948 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
949 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000950 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
953 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
954 }
955
Jerry Yu60835a82021-08-04 10:13:52 +0800956 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
957 return( 0 );
958 }
959#endif
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100962 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800963 {
964 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
965 return( 0 );
966 }
967#endif
968
Ronald Cron6f135e12021-12-08 16:57:54 +0100969#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100970 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800971 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
973 {
974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
975 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
976 }
977
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000978 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
981 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
982 }
983
984
Ronald Crone1d3f062022-02-10 14:50:54 +0100985 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
986 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800987 }
988#endif
989
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
991 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
992}
993
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800995static int ssl_conf_check(const mbedtls_ssl_context *ssl)
996{
997 int ret;
998 ret = ssl_conf_version_check( ssl );
999 if( ret != 0 )
1000 return( ret );
1001
1002 /* Space for further checks */
1003
1004 return( 0 );
1005}
1006
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001007/*
1008 * Setup an SSL context
1009 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001010
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001011int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001012 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013{
Janos Follath865b3eb2019-12-16 11:46:15 +00001014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001015 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1016 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001018 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001019
Jerry Yu60835a82021-08-04 10:13:52 +08001020 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1021 return( ret );
1022
Paul Bakker62f2dee2012-09-28 07:31:51 +00001023 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001024 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001025 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001026
1027 /* Set to NULL in case of an error condition */
1028 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001029
Darryl Greenb33cc762019-11-28 14:29:44 +00001030#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1031 ssl->in_buf_len = in_buf_len;
1032#endif
1033 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001034 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001037 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001038 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001039 }
1040
Darryl Greenb33cc762019-11-28 14:29:44 +00001041#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1042 ssl->out_buf_len = out_buf_len;
1043#endif
1044 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001045 if( ssl->out_buf == NULL )
1046 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001048 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001049 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
1051
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001052 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001053
Johan Pascalb62bb512015-12-03 21:56:45 +01001054#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001055 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001056#endif
1057
Paul Bakker48916f92012-09-16 19:57:18 +00001058 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001059 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001062
1063error:
1064 mbedtls_free( ssl->in_buf );
1065 mbedtls_free( ssl->out_buf );
1066
1067 ssl->conf = NULL;
1068
Darryl Greenb33cc762019-11-28 14:29:44 +00001069#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1070 ssl->in_buf_len = 0;
1071 ssl->out_buf_len = 0;
1072#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001073 ssl->in_buf = NULL;
1074 ssl->out_buf = NULL;
1075
1076 ssl->in_hdr = NULL;
1077 ssl->in_ctr = NULL;
1078 ssl->in_len = NULL;
1079 ssl->in_iv = NULL;
1080 ssl->in_msg = NULL;
1081
1082 ssl->out_hdr = NULL;
1083 ssl->out_ctr = NULL;
1084 ssl->out_len = NULL;
1085 ssl->out_iv = NULL;
1086 ssl->out_msg = NULL;
1087
k-stachowiak9f7798e2018-07-31 16:52:32 +02001088 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089}
1090
1091/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001092 * Reset an initialized and used SSL context for re-use while retaining
1093 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001094 *
1095 * If partial is non-zero, keep data in the input buffer and client ID.
1096 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001097 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001098void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1099 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001100{
Darryl Greenb33cc762019-11-28 14:29:44 +00001101#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1102 size_t in_buf_len = ssl->in_buf_len;
1103 size_t out_buf_len = ssl->out_buf_len;
1104#else
1105 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1106 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1107#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001108
Hanno Beckerb0302c42021-08-03 09:39:42 +01001109#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1110 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001111#endif
1112
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001113 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001114 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001115
Hanno Beckerb0302c42021-08-03 09:39:42 +01001116 mbedtls_ssl_reset_in_out_pointers( ssl );
1117
1118 /* Reset incoming message parsing */
1119 ssl->in_offt = NULL;
1120 ssl->nb_zero = 0;
1121 ssl->in_msgtype = 0;
1122 ssl->in_msglen = 0;
1123 ssl->in_hslen = 0;
1124 ssl->keep_current_message = 0;
1125 ssl->transform_in = NULL;
1126
1127#if defined(MBEDTLS_SSL_PROTO_DTLS)
1128 ssl->next_record_offset = 0;
1129 ssl->in_epoch = 0;
1130#endif
1131
1132 /* Keep current datagram if partial == 1 */
1133 if( partial == 0 )
1134 {
1135 ssl->in_left = 0;
1136 memset( ssl->in_buf, 0, in_buf_len );
1137 }
1138
Ronald Cronad8c17b2022-06-10 17:18:09 +02001139 ssl->send_alert = 0;
1140
Hanno Beckerb0302c42021-08-03 09:39:42 +01001141 /* Reset outgoing message writing */
1142 ssl->out_msgtype = 0;
1143 ssl->out_msglen = 0;
1144 ssl->out_left = 0;
1145 memset( ssl->out_buf, 0, out_buf_len );
1146 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1147 ssl->transform_out = NULL;
1148
1149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1150 mbedtls_ssl_dtls_replay_reset( ssl );
1151#endif
1152
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001154 if( ssl->transform )
1155 {
1156 mbedtls_ssl_transform_free( ssl->transform );
1157 mbedtls_free( ssl->transform );
1158 ssl->transform = NULL;
1159 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001160#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1161
XiaokangQian2b01dc32022-01-21 02:53:13 +00001162#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1163 mbedtls_ssl_transform_free( ssl->transform_application );
1164 mbedtls_free( ssl->transform_application );
1165 ssl->transform_application = NULL;
1166
1167 if( ssl->handshake != NULL )
1168 {
1169 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1170 mbedtls_free( ssl->handshake->transform_earlydata );
1171 ssl->handshake->transform_earlydata = NULL;
1172
1173 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1174 mbedtls_free( ssl->handshake->transform_handshake );
1175 ssl->handshake->transform_handshake = NULL;
1176 }
1177
XiaokangQian2b01dc32022-01-21 02:53:13 +00001178#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001179}
1180
1181int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1182{
1183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1184
1185 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1186
XiaokangQian78b1fa72022-01-19 06:56:30 +00001187 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001188
1189 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_RENEGOTIATION)
1191 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001192 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001193
1194 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1196 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001199
Hanno Beckerb0302c42021-08-03 09:39:42 +01001200 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001201 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001202 if( ssl->session )
1203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_ssl_session_free( ssl->session );
1205 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001206 ssl->session = NULL;
1207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001210 ssl->alpn_chosen = NULL;
1211#endif
1212
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001213#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001214#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001215 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001216#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001217 {
1218 mbedtls_free( ssl->cli_id );
1219 ssl->cli_id = NULL;
1220 ssl->cli_id_len = 0;
1221 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001222#endif
1223
Paul Bakker48916f92012-09-16 19:57:18 +00001224 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1225 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001226
1227 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001228}
1229
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001230/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001231 * Reset an initialized and used SSL context for re-use while retaining
1232 * all application-set variables, function pointers and data.
1233 */
1234int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1235{
Hanno Becker43aefe22020-02-05 10:44:56 +00001236 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001237}
1238
1239/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 * SSL set accessors
1241 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001242void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001244 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245}
1246
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001247void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001248{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001249 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001250}
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001253void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001256}
1257#endif
1258
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001259void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001261 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001262}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001265
Hanno Becker1841b0a2018-08-24 11:13:57 +01001266void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1267 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001268{
1269 ssl->disable_datagram_packing = !allow_packing;
1270}
1271
1272void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1273 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->hs_timeout_min = min;
1276 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001277}
1278#endif
1279
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001280void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001282 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283}
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001287 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001288 void *p_vrfy )
1289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001290 conf->f_vrfy = f_vrfy;
1291 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001296 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 void *p_rng )
1298{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001299 conf->f_rng = f_rng;
1300 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001301}
1302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001304 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 void *p_dbg )
1306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001307 conf->f_dbg = f_dbg;
1308 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309}
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001312 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001313 mbedtls_ssl_send_t *f_send,
1314 mbedtls_ssl_recv_t *f_recv,
1315 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001316{
1317 ssl->p_bio = p_bio;
1318 ssl->f_send = f_send;
1319 ssl->f_recv = f_recv;
1320 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001321}
1322
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001323#if defined(MBEDTLS_SSL_PROTO_DTLS)
1324void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1325{
1326 ssl->mtu = mtu;
1327}
1328#endif
1329
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001330void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001331{
1332 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001333}
1334
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001335void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1336 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001337 mbedtls_ssl_set_timer_t *f_set_timer,
1338 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001339{
1340 ssl->p_timer = p_timer;
1341 ssl->f_set_timer = f_set_timer;
1342 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001343
1344 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001345 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001349void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001350 void *p_cache,
1351 mbedtls_ssl_cache_get_t *f_get_cache,
1352 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001354 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001355 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001356 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_SSL_CLI_C)
1361int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Janos Follath865b3eb2019-12-16 11:46:15 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu40afab62022-10-08 10:42:13 +08001364 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
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
Jerry Yu40afab62022-10-08 10:42:13 +08001377 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1378 if( mbedtls_ssl_validate_ciphersuite( ssl, ciphersuite_info,
1379 session->tls_version,
1380 session->tls_version ) != 0 )
1381 {
1382 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid ciphersuite.",
1383 session->ciphersuite ) );
1384 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1385 }
1386
Hanno Becker52055ae2019-02-06 14:30:46 +00001387 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1388 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001389 return( ret );
1390
Paul Bakker0a597072012-09-25 21:55:46 +00001391 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001392
1393 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001397void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1398 const int *ciphersuites )
1399{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001400 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401}
1402
Ronald Cron6f135e12021-12-08 16:57:54 +01001403#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001404void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001405 const int kex_modes )
1406{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001407 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001408}
Ronald Cron6f135e12021-12-08 16:57:54 +01001409#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001412void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001413 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001414{
1415 conf->cert_profile = profile;
1416}
1417
Glenn Strauss36872db2022-01-22 05:06:31 -05001418static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1419{
1420 mbedtls_ssl_key_cert *cur = key_cert, *next;
1421
1422 while( cur != NULL )
1423 {
1424 next = cur->next;
1425 mbedtls_free( cur );
1426 cur = next;
1427 }
1428}
1429
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001430/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001431MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001432static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1433 mbedtls_x509_crt *cert,
1434 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001435{
niisato8ee24222018-06-25 19:05:48 +09001436 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001437
Glenn Strauss36872db2022-01-22 05:06:31 -05001438 if( cert == NULL )
1439 {
1440 /* Free list if cert is null */
1441 ssl_key_cert_free( *head );
1442 *head = NULL;
1443 return( 0 );
1444 }
1445
niisato8ee24222018-06-25 19:05:48 +09001446 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1447 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001448 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001449
niisato8ee24222018-06-25 19:05:48 +09001450 new_cert->cert = cert;
1451 new_cert->key = key;
1452 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001453
Glenn Strauss36872db2022-01-22 05:06:31 -05001454 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001455 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001456 {
niisato8ee24222018-06-25 19:05:48 +09001457 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001458 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001459 else
1460 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001461 mbedtls_ssl_key_cert *cur = *head;
1462 while( cur->next != NULL )
1463 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001464 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001465 }
1466
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001467 return( 0 );
1468}
1469
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001470int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001471 mbedtls_x509_crt *own_cert,
1472 mbedtls_pk_context *pk_key )
1473{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001474 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001475}
1476
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001477void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001478 mbedtls_x509_crt *ca_chain,
1479 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001480{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001481 conf->ca_chain = ca_chain;
1482 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001483
1484#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1485 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1486 * cannot be used together. */
1487 conf->f_ca_cb = NULL;
1488 conf->p_ca_cb = NULL;
1489#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001490}
Hanno Becker5adaad92019-03-27 16:54:37 +00001491
1492#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1493void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001494 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001495 void *p_ca_cb )
1496{
1497 conf->f_ca_cb = f_ca_cb;
1498 conf->p_ca_cb = p_ca_cb;
1499
1500 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1501 * cannot be used together. */
1502 conf->ca_chain = NULL;
1503 conf->ca_crl = NULL;
1504}
1505#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001507
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001508#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001509const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1510 size_t *name_len )
1511{
1512 *name_len = ssl->handshake->sni_name_len;
1513 return( ssl->handshake->sni_name );
1514}
1515
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001516int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1517 mbedtls_x509_crt *own_cert,
1518 mbedtls_pk_context *pk_key )
1519{
1520 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1521 own_cert, pk_key ) );
1522}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001523
1524void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1525 mbedtls_x509_crt *ca_chain,
1526 mbedtls_x509_crl *ca_crl )
1527{
1528 ssl->handshake->sni_ca_chain = ca_chain;
1529 ssl->handshake->sni_ca_crl = ca_crl;
1530}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001531
Glenn Strauss999ef702022-03-11 01:37:23 -05001532#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1533void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1534 const mbedtls_x509_crt *crt)
1535{
1536 ssl->handshake->dn_hints = crt;
1537}
1538#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1539
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001540void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1541 int authmode )
1542{
1543 ssl->handshake->sni_authmode = authmode;
1544}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001545#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1546
Hanno Becker8927c832019-04-03 12:52:50 +01001547#if defined(MBEDTLS_X509_CRT_PARSE_C)
1548void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1549 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1550 void *p_vrfy )
1551{
1552 ssl->f_vrfy = f_vrfy;
1553 ssl->p_vrfy = p_vrfy;
1554}
1555#endif
1556
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001557#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001558/*
1559 * Set EC J-PAKE password for current handshake
1560 */
1561int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1562 const unsigned char *pw,
1563 size_t pw_len )
1564{
1565 mbedtls_ecjpake_role role;
1566
Janos Follath8eb64132016-06-03 15:40:57 +01001567 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1569
1570 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1571 role = MBEDTLS_ECJPAKE_SERVER;
1572 else
1573 role = MBEDTLS_ECJPAKE_CLIENT;
1574
1575 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1576 role,
1577 MBEDTLS_MD_SHA256,
1578 MBEDTLS_ECP_DP_SECP256R1,
1579 pw, pw_len ) );
1580}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001581#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001582
Gilles Peskineeccd8882020-03-10 12:19:08 +01001583#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001584
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001585MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001586static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1587{
1588#if defined(MBEDTLS_USE_PSA_CRYPTO)
1589 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1590 return( 1 );
1591#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001592 if( conf->psk != NULL )
1593 return( 1 );
1594
1595 return( 0 );
1596}
1597
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001598static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1599{
1600 /* Remove reference to existing PSK, if any. */
1601#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001602 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001603 {
1604 /* The maintenance of the PSK key slot is the
1605 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001606 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001607 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001608#endif /* MBEDTLS_USE_PSA_CRYPTO */
1609 if( conf->psk != NULL )
1610 {
1611 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1612
1613 mbedtls_free( conf->psk );
1614 conf->psk = NULL;
1615 conf->psk_len = 0;
1616 }
1617
1618 /* Remove reference to PSK identity, if any. */
1619 if( conf->psk_identity != NULL )
1620 {
1621 mbedtls_free( conf->psk_identity );
1622 conf->psk_identity = NULL;
1623 conf->psk_identity_len = 0;
1624 }
1625}
1626
Hanno Becker7390c712018-11-15 13:33:04 +00001627/* This function assumes that PSK identity in the SSL config is unset.
1628 * It checks that the provided identity is well-formed and attempts
1629 * to make a copy of it in the SSL config.
1630 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001631MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001632static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1633 unsigned char const *psk_identity,
1634 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001635{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001636 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001637 if( psk_identity == NULL ||
1638 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001639 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001640 {
1641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1642 }
1643
Hanno Becker7390c712018-11-15 13:33:04 +00001644 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1645 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001646 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001647
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001648 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001649 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001650
1651 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001652}
1653
Hanno Becker7390c712018-11-15 13:33:04 +00001654int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1655 const unsigned char *psk, size_t psk_len,
1656 const unsigned char *psk_identity, size_t psk_identity_len )
1657{
Janos Follath865b3eb2019-12-16 11:46:15 +00001658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001659
1660 /* We currently only support one PSK, raw or opaque. */
1661 if( ssl_conf_psk_is_configured( conf ) )
1662 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001663
1664 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001665 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001667 if( psk_len == 0 )
1668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1669 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1671
Hanno Becker7390c712018-11-15 13:33:04 +00001672 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1673 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1674 conf->psk_len = psk_len;
1675 memcpy( conf->psk, psk, conf->psk_len );
1676
1677 /* Check and set PSK Identity */
1678 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1679 if( ret != 0 )
1680 ssl_conf_remove_psk( conf );
1681
1682 return( ret );
1683}
1684
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001685static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1686{
1687#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001688 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001689 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001690 /* The maintenance of the external PSK key slot is the
1691 * user's responsibility. */
1692 if( ssl->handshake->psk_opaque_is_internal )
1693 {
1694 psa_destroy_key( ssl->handshake->psk_opaque );
1695 ssl->handshake->psk_opaque_is_internal = 0;
1696 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001697 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001698 }
Neil Armstronge952a302022-05-03 10:22:14 +02001699#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001700 if( ssl->handshake->psk != NULL )
1701 {
1702 mbedtls_platform_zeroize( ssl->handshake->psk,
1703 ssl->handshake->psk_len );
1704 mbedtls_free( ssl->handshake->psk );
1705 ssl->handshake->psk_len = 0;
1706 }
Neil Armstronge952a302022-05-03 10:22:14 +02001707#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001708}
1709
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001710int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1711 const unsigned char *psk, size_t psk_len )
1712{
Neil Armstrong501c9322022-05-03 09:35:09 +02001713#if defined(MBEDTLS_USE_PSA_CRYPTO)
1714 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001716 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001718#endif /* MBEDTLS_USE_PSA_CRYPTO */
1719
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001720 if( psk == NULL || ssl->handshake == NULL )
1721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1722
1723 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1725
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001726 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001727
Neil Armstrong501c9322022-05-03 09:35:09 +02001728#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1730 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1731 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001732 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001733 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1734 else
1735 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1736 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1737 }
1738#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001739
Jerry Yu568ec252022-07-22 21:27:34 +08001740#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001741 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1742 {
1743 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1744 psa_set_key_usage_flags( &key_attributes,
1745 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1746 }
1747#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1748
Neil Armstrong501c9322022-05-03 09:35:09 +02001749 psa_set_key_algorithm( &key_attributes, alg );
1750 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1751
1752 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1753 if( status != PSA_SUCCESS )
1754 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1755
1756 /* Allow calling psa_destroy_key() on psk remove */
1757 ssl->handshake->psk_opaque_is_internal = 1;
1758 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1759#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001760 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001761 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001762
1763 ssl->handshake->psk_len = psk_len;
1764 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1765
1766 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001767#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001768}
1769
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001770#if defined(MBEDTLS_USE_PSA_CRYPTO)
1771int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001772 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001773 const unsigned char *psk_identity,
1774 size_t psk_identity_len )
1775{
Janos Follath865b3eb2019-12-16 11:46:15 +00001776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001777
1778 /* We currently only support one PSK, raw or opaque. */
1779 if( ssl_conf_psk_is_configured( conf ) )
1780 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001781
Hanno Becker7390c712018-11-15 13:33:04 +00001782 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001783 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001785 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001786
1787 /* Check and set PSK Identity */
1788 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1789 psk_identity_len );
1790 if( ret != 0 )
1791 ssl_conf_remove_psk( conf );
1792
1793 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001794}
1795
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001796int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1797 mbedtls_svc_key_id_t psk )
1798{
1799 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1800 ( ssl->handshake == NULL ) )
1801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1802
1803 ssl_remove_psk( ssl );
1804 ssl->handshake->psk_opaque = psk;
1805 return( 0 );
1806}
1807#endif /* MBEDTLS_USE_PSA_CRYPTO */
1808
Jerry Yu8897c072022-08-12 13:56:53 +08001809#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001810void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1811 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1812 size_t),
1813 void *p_psk )
1814{
1815 conf->f_psk = f_psk;
1816 conf->p_psk = p_psk;
1817}
Jerry Yu8897c072022-08-12 13:56:53 +08001818#endif /* MBEDTLS_SSL_SRV_C */
1819
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001820#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1821
1822#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001823static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1824 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001825{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001826#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001827 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001828 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001829#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001830 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001831 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001832 return( MBEDTLS_SSL_MODE_STREAM );
1833}
1834
1835#else /* MBEDTLS_USE_PSA_CRYPTO */
1836
1837static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1838 mbedtls_cipher_mode_t mode )
1839{
1840#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1841 if( mode == MBEDTLS_MODE_CBC )
1842 return( MBEDTLS_SSL_MODE_CBC );
1843#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1844
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845#if defined(MBEDTLS_GCM_C) || \
1846 defined(MBEDTLS_CCM_C) || \
1847 defined(MBEDTLS_CHACHAPOLY_C)
1848 if( mode == MBEDTLS_MODE_GCM ||
1849 mode == MBEDTLS_MODE_CCM ||
1850 mode == MBEDTLS_MODE_CHACHAPOLY )
1851 return( MBEDTLS_SSL_MODE_AEAD );
1852#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001853
1854 return( MBEDTLS_SSL_MODE_STREAM );
1855}
Gilles Peskine301711e2022-04-26 16:57:05 +02001856#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001857
Gilles Peskinee108d982022-04-26 16:50:40 +02001858static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1859 mbedtls_ssl_mode_t base_mode,
1860 int encrypt_then_mac )
1861{
1862#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1863 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1864 base_mode == MBEDTLS_SSL_MODE_CBC )
1865 {
1866 return( MBEDTLS_SSL_MODE_CBC_ETM );
1867 }
1868#else
1869 (void) encrypt_then_mac;
1870#endif
1871 return( base_mode );
1872}
1873
Neil Armstrongab555e02022-04-04 11:07:59 +02001874mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001875 const mbedtls_ssl_transform *transform )
1876{
Gilles Peskinee108d982022-04-26 16:50:40 +02001877 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001878#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001879 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001880#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001881 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1882#endif
1883 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001884
Gilles Peskinee108d982022-04-26 16:50:40 +02001885 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001886#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001887 encrypt_then_mac = transform->encrypt_then_mac;
1888#endif
1889 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001890}
1891
Neil Armstrongab555e02022-04-04 11:07:59 +02001892mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001893#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001895#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896 const mbedtls_ssl_ciphersuite_t *suite )
1897{
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1899
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900#if defined(MBEDTLS_USE_PSA_CRYPTO)
1901 psa_status_t status;
1902 psa_algorithm_t alg;
1903 psa_key_type_t type;
1904 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001905 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1906 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001907 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001908#else
1909 const mbedtls_cipher_info_t *cipher =
1910 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001912 {
1913 base_mode =
1914 mbedtls_ssl_get_base_mode(
1915 mbedtls_cipher_info_get_mode( cipher ) );
1916 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917#endif /* MBEDTLS_USE_PSA_CRYPTO */
1918
Gilles Peskinee108d982022-04-26 16:50:40 +02001919#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1920 int encrypt_then_mac = 0;
1921#endif
1922 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923}
1924
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001925#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001926
1927#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001928/* Serialization of TLS 1.3 sessions:
1929 *
1930 * struct {
1931 * uint64 ticket_received;
1932 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001933 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001934 * } ClientOnlyData;
1935 *
1936 * struct {
1937 * uint8 endpoint;
1938 * uint8 ciphersuite[2];
1939 * uint32 ticket_age_add;
1940 * uint8 ticket_flags;
1941 * opaque resumption_key<0..255>;
1942 * select ( endpoint ) {
1943 * case client: ClientOnlyData;
1944 * case server: uint64 start_time;
1945 * };
1946 * } serialized_session_tls13;
1947 *
1948 */
1949#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001950MBEDTLS_CHECK_RETURN_CRITICAL
1951static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1952 unsigned char *buf,
1953 size_t buf_len,
1954 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001955{
1956 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001957 size_t needed = 1 /* endpoint */
1958 + 2 /* ciphersuite */
1959 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001960 + 1 /* ticket_flags */
1961 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001962 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001963
1964 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1966 needed += session->resumption_key_len; /* resumption_key */
1967
Jerry Yu438ddd82022-07-07 06:55:50 +00001968#if defined(MBEDTLS_HAVE_TIME)
1969 needed += 8; /* start_time or ticket_received */
1970#endif
1971
1972#if defined(MBEDTLS_SSL_CLI_C)
1973 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1974 {
1975 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001976 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001977
1978 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001979 if( session->ticket_len > SIZE_MAX - needed )
1980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001981
Jerry Yue28d9742022-08-18 15:44:03 +08001982 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001983 }
1984#endif /* MBEDTLS_SSL_CLI_C */
1985
Jerry Yue36fdd62022-08-17 21:31:36 +08001986 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001987 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001988 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001989
1990 p[0] = session->endpoint;
1991 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1992 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1993 p[7] = session->ticket_flags;
1994
1995 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001996 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001997 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001998 memcpy( p, session->resumption_key, session->resumption_key_len );
1999 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002000
2001#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2002 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2003 {
2004 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2005 p += 8;
2006 }
2007#endif /* MBEDTLS_HAVE_TIME */
2008
2009#if defined(MBEDTLS_SSL_CLI_C)
2010 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2011 {
2012#if defined(MBEDTLS_HAVE_TIME)
2013 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2014 p += 8;
2015#endif
2016 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2017 p += 4;
2018
2019 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2020 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002021
2022 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002023 {
2024 memcpy( p, session->ticket, session->ticket_len );
2025 p += session->ticket_len;
2026 }
2027 }
2028#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002029 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002030}
2031
2032MBEDTLS_CHECK_RETURN_CRITICAL
2033static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2034 const unsigned char *buf,
2035 size_t len )
2036{
2037 const unsigned char *p = buf;
2038 const unsigned char *end = buf + len;
2039
2040 if( end - p < 9 )
2041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2042 session->endpoint = p[0];
2043 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2044 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2045 session->ticket_flags = p[7];
2046
2047 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002048 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002049 p += 9;
2050
Jerry Yubc7c1a42022-07-21 22:57:37 +08002051 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053
Jerry Yubc7c1a42022-07-21 22:57:37 +08002054 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002056 memcpy( session->resumption_key, p, session->resumption_key_len );
2057 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002058
2059#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2060 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2061 {
2062 if( end - p < 8 )
2063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2064 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2065 p += 8;
2066 }
2067#endif /* MBEDTLS_HAVE_TIME */
2068
2069#if defined(MBEDTLS_SSL_CLI_C)
2070 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2071 {
2072#if defined(MBEDTLS_HAVE_TIME)
2073 if( end - p < 8 )
2074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2075 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2076 p += 8;
2077#endif
2078 if( end - p < 4 )
2079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2080 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2081 p += 4;
2082
2083 if( end - p < 2 )
2084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2085 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2086 p += 2;
2087
2088 if( end - p < ( long int )session->ticket_len )
2089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2090 if( session->ticket_len > 0 )
2091 {
2092 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2093 if( session->ticket == NULL )
2094 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2095 memcpy( session->ticket, p, session->ticket_len );
2096 p += session->ticket_len;
2097 }
2098 }
2099#endif /* MBEDTLS_SSL_CLI_C */
2100
2101 return( 0 );
2102
2103}
2104#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002105MBEDTLS_CHECK_RETURN_CRITICAL
2106static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2107 unsigned char *buf,
2108 size_t buf_len,
2109 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002110{
2111 ((void) session);
2112 ((void) buf);
2113 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002114 *olen = 0;
2115 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002116}
Jerry Yu438ddd82022-07-07 06:55:50 +00002117
2118static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2119 unsigned char *buf,
2120 size_t buf_len )
2121{
2122 ((void) session);
2123 ((void) buf);
2124 ((void) buf_len);
2125 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2126}
2127#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002128#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2129
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002130psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002131 size_t taglen,
2132 psa_algorithm_t *alg,
2133 psa_key_type_t *key_type,
2134 size_t *key_size )
2135{
2136 switch ( mbedtls_cipher_type )
2137 {
2138 case MBEDTLS_CIPHER_AES_128_CBC:
2139 *alg = PSA_ALG_CBC_NO_PADDING;
2140 *key_type = PSA_KEY_TYPE_AES;
2141 *key_size = 128;
2142 break;
2143 case MBEDTLS_CIPHER_AES_128_CCM:
2144 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2145 *key_type = PSA_KEY_TYPE_AES;
2146 *key_size = 128;
2147 break;
2148 case MBEDTLS_CIPHER_AES_128_GCM:
2149 *alg = PSA_ALG_GCM;
2150 *key_type = PSA_KEY_TYPE_AES;
2151 *key_size = 128;
2152 break;
2153 case MBEDTLS_CIPHER_AES_192_CCM:
2154 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2155 *key_type = PSA_KEY_TYPE_AES;
2156 *key_size = 192;
2157 break;
2158 case MBEDTLS_CIPHER_AES_192_GCM:
2159 *alg = PSA_ALG_GCM;
2160 *key_type = PSA_KEY_TYPE_AES;
2161 *key_size = 192;
2162 break;
2163 case MBEDTLS_CIPHER_AES_256_CBC:
2164 *alg = PSA_ALG_CBC_NO_PADDING;
2165 *key_type = PSA_KEY_TYPE_AES;
2166 *key_size = 256;
2167 break;
2168 case MBEDTLS_CIPHER_AES_256_CCM:
2169 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2170 *key_type = PSA_KEY_TYPE_AES;
2171 *key_size = 256;
2172 break;
2173 case MBEDTLS_CIPHER_AES_256_GCM:
2174 *alg = PSA_ALG_GCM;
2175 *key_type = PSA_KEY_TYPE_AES;
2176 *key_size = 256;
2177 break;
2178 case MBEDTLS_CIPHER_ARIA_128_CBC:
2179 *alg = PSA_ALG_CBC_NO_PADDING;
2180 *key_type = PSA_KEY_TYPE_ARIA;
2181 *key_size = 128;
2182 break;
2183 case MBEDTLS_CIPHER_ARIA_128_CCM:
2184 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2185 *key_type = PSA_KEY_TYPE_ARIA;
2186 *key_size = 128;
2187 break;
2188 case MBEDTLS_CIPHER_ARIA_128_GCM:
2189 *alg = PSA_ALG_GCM;
2190 *key_type = PSA_KEY_TYPE_ARIA;
2191 *key_size = 128;
2192 break;
2193 case MBEDTLS_CIPHER_ARIA_192_CCM:
2194 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2195 *key_type = PSA_KEY_TYPE_ARIA;
2196 *key_size = 192;
2197 break;
2198 case MBEDTLS_CIPHER_ARIA_192_GCM:
2199 *alg = PSA_ALG_GCM;
2200 *key_type = PSA_KEY_TYPE_ARIA;
2201 *key_size = 192;
2202 break;
2203 case MBEDTLS_CIPHER_ARIA_256_CBC:
2204 *alg = PSA_ALG_CBC_NO_PADDING;
2205 *key_type = PSA_KEY_TYPE_ARIA;
2206 *key_size = 256;
2207 break;
2208 case MBEDTLS_CIPHER_ARIA_256_CCM:
2209 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2210 *key_type = PSA_KEY_TYPE_ARIA;
2211 *key_size = 256;
2212 break;
2213 case MBEDTLS_CIPHER_ARIA_256_GCM:
2214 *alg = PSA_ALG_GCM;
2215 *key_type = PSA_KEY_TYPE_ARIA;
2216 *key_size = 256;
2217 break;
2218 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2219 *alg = PSA_ALG_CBC_NO_PADDING;
2220 *key_type = PSA_KEY_TYPE_CAMELLIA;
2221 *key_size = 128;
2222 break;
2223 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2224 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2225 *key_type = PSA_KEY_TYPE_CAMELLIA;
2226 *key_size = 128;
2227 break;
2228 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2229 *alg = PSA_ALG_GCM;
2230 *key_type = PSA_KEY_TYPE_CAMELLIA;
2231 *key_size = 128;
2232 break;
2233 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2234 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2235 *key_type = PSA_KEY_TYPE_CAMELLIA;
2236 *key_size = 192;
2237 break;
2238 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2239 *alg = PSA_ALG_GCM;
2240 *key_type = PSA_KEY_TYPE_CAMELLIA;
2241 *key_size = 192;
2242 break;
2243 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2244 *alg = PSA_ALG_CBC_NO_PADDING;
2245 *key_type = PSA_KEY_TYPE_CAMELLIA;
2246 *key_size = 256;
2247 break;
2248 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2249 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2250 *key_type = PSA_KEY_TYPE_CAMELLIA;
2251 *key_size = 256;
2252 break;
2253 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2254 *alg = PSA_ALG_GCM;
2255 *key_type = PSA_KEY_TYPE_CAMELLIA;
2256 *key_size = 256;
2257 break;
2258 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2259 *alg = PSA_ALG_CHACHA20_POLY1305;
2260 *key_type = PSA_KEY_TYPE_CHACHA20;
2261 *key_size = 256;
2262 break;
2263 case MBEDTLS_CIPHER_NULL:
2264 *alg = MBEDTLS_SSL_NULL_CIPHER;
2265 *key_type = 0;
2266 *key_size = 0;
2267 break;
2268 default:
2269 return PSA_ERROR_NOT_SUPPORTED;
2270 }
2271
2272 return PSA_SUCCESS;
2273}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002274#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002275
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002276#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002277int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2278 const unsigned char *dhm_P, size_t P_len,
2279 const unsigned char *dhm_G, size_t G_len )
2280{
Janos Follath865b3eb2019-12-16 11:46:15 +00002281 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002282
Glenn Strausscee11292021-12-20 01:43:17 -05002283 mbedtls_mpi_free( &conf->dhm_P );
2284 mbedtls_mpi_free( &conf->dhm_G );
2285
Hanno Beckera90658f2017-10-04 15:29:08 +01002286 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2287 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2288 {
2289 mbedtls_mpi_free( &conf->dhm_P );
2290 mbedtls_mpi_free( &conf->dhm_G );
2291 return( ret );
2292 }
2293
2294 return( 0 );
2295}
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002297int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002298{
Janos Follath865b3eb2019-12-16 11:46:15 +00002299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002300
Glenn Strausscee11292021-12-20 01:43:17 -05002301 mbedtls_mpi_free( &conf->dhm_P );
2302 mbedtls_mpi_free( &conf->dhm_G );
2303
Gilles Peskinee5702482021-06-11 21:59:08 +02002304 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2305 &conf->dhm_P ) ) != 0 ||
2306 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2307 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002308 {
2309 mbedtls_mpi_free( &conf->dhm_P );
2310 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002311 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002312 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002313
2314 return( 0 );
2315}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002316#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002317
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002318#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2319/*
2320 * Set the minimum length for Diffie-Hellman parameters
2321 */
2322void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2323 unsigned int bitlen )
2324{
2325 conf->dhm_min_bitlen = bitlen;
2326}
2327#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2328
Gilles Peskineeccd8882020-03-10 12:19:08 +01002329#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002330#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002331/*
2332 * Set allowed/preferred hashes for handshake signatures
2333 */
2334void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2335 const int *hashes )
2336{
2337 conf->sig_hashes = hashes;
2338}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002339#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002340
Jerry Yuf017ee42022-01-12 15:49:48 +08002341/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002342void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2343 const uint16_t* sig_algs )
2344{
Jerry Yuf017ee42022-01-12 15:49:48 +08002345#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2346 conf->sig_hashes = NULL;
2347#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2348 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002349}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002350#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002351
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002352#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002353#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002354/*
2355 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002356 *
2357 * mbedtls_ssl_setup() takes the provided list
2358 * and translates it to a list of IANA TLS group identifiers,
2359 * stored in ssl->handshake->group_list.
2360 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002361 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002362void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002363 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002364{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002365 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002366 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002367}
Brett Warrene0edc842021-08-17 09:53:13 +01002368#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002369#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002370
Brett Warrene0edc842021-08-17 09:53:13 +01002371/*
2372 * Set the allowed groups
2373 */
2374void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2375 const uint16_t *group_list )
2376{
2377#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2378 conf->curve_list = NULL;
2379#endif
2380 conf->group_list = group_list;
2381}
2382
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002383#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385{
Hanno Becker947194e2017-04-07 13:25:49 +01002386 /* Initialize to suppress unnecessary compiler warning */
2387 size_t hostname_len = 0;
2388
2389 /* Check if new hostname is valid before
2390 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002391 if( hostname != NULL )
2392 {
2393 hostname_len = strlen( hostname );
2394
2395 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2397 }
2398
2399 /* Now it's clear that we will overwrite the old hostname,
2400 * so we can free it safely */
2401
2402 if( ssl->hostname != NULL )
2403 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002404 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002405 mbedtls_free( ssl->hostname );
2406 }
2407
2408 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002409
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002411 {
2412 ssl->hostname = NULL;
2413 }
2414 else
2415 {
2416 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002417 if( ssl->hostname == NULL )
2418 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002419
Hanno Becker947194e2017-04-07 13:25:49 +01002420 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002421
Hanno Becker947194e2017-04-07 13:25:49 +01002422 ssl->hostname[hostname_len] = '\0';
2423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
2425 return( 0 );
2426}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002427#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002429#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002430void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002432 const unsigned char *, size_t),
2433 void *p_sni )
2434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002435 conf->f_sni = f_sni;
2436 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002441int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002442{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002443 size_t cur_len, tot_len;
2444 const char **p;
2445
2446 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002447 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2448 * MUST NOT be truncated."
2449 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002450 */
2451 tot_len = 0;
2452 for( p = protos; *p != NULL; p++ )
2453 {
2454 cur_len = strlen( *p );
2455 tot_len += cur_len;
2456
Ronald Cron8216dd32020-04-23 16:41:44 +02002457 if( ( cur_len == 0 ) ||
2458 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2459 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002461 }
2462
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002463 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002464
2465 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002466}
2467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002469{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002470 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002473
Johan Pascalb62bb512015-12-03 21:56:45 +01002474#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002475void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2476 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002477{
2478 conf->dtls_srtp_mki_support = support_mki_value;
2479}
2480
Ron Eldoref72faf2018-07-12 11:54:20 +03002481int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2482 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002483 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002484{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002485 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002486 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002488 }
Ron Eldor591f1622018-01-22 12:30:04 +02002489
2490 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002491 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002492 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002493 }
Ron Eldor591f1622018-01-22 12:30:04 +02002494
2495 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2496 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002497 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002498}
2499
Ron Eldoref72faf2018-07-12 11:54:20 +03002500int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002501 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002502{
Johan Pascal253d0262020-09-22 13:04:45 +02002503 const mbedtls_ssl_srtp_profile *p;
2504 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002505
Johan Pascal253d0262020-09-22 13:04:45 +02002506 /* check the profiles list: all entry must be valid,
2507 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002508 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2509 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2510 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002511 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002512 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002513 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002514 list_size++;
2515 }
2516 else
2517 {
2518 /* unsupported value, stop parsing and set the size to an error value */
2519 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002520 }
2521 }
2522
Johan Pascal5ef72d22020-10-28 17:05:47 +01002523 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002524 {
Johan Pascal253d0262020-09-22 13:04:45 +02002525 conf->dtls_srtp_profile_list = NULL;
2526 conf->dtls_srtp_profile_list_len = 0;
2527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 }
2529
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002530 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002531 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002532
2533 return( 0 );
2534}
2535
Johan Pascal5ef72d22020-10-28 17:05:47 +01002536void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2537 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002538{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002539 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2540 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002541 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002542 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002543 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002544 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002545 else
2546 {
2547 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002548 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2549 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002550 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002551}
Johan Pascalb62bb512015-12-03 21:56:45 +01002552#endif /* MBEDTLS_SSL_DTLS_SRTP */
2553
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302554#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002555void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002556{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002557 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002558}
2559
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002560void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002561{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002562 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002563}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302564#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002565
Janos Follath088ce432017-04-10 12:42:31 +01002566#if defined(MBEDTLS_SSL_SRV_C)
2567void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2568 char cert_req_ca_list )
2569{
2570 conf->cert_req_ca_list = cert_req_ca_list;
2571}
2572#endif
2573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002575void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002576{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002577 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002578}
2579#endif
2580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002582void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002584 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002585}
2586#endif
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002589int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002592 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002595 }
2596
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002597 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002598
2599 return( 0 );
2600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002603void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002605 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002606}
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002609void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002610{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002611 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002612}
2613
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002614void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002616 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002617}
2618
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002619void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002620 const unsigned char period[8] )
2621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002622 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002627#if defined(MBEDTLS_SSL_CLI_C)
2628void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002629{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002630 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002631}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002632#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002633
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002634#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002635
Jerry Yud0766ec2022-09-22 10:46:57 +08002636#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002637void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2638 uint16_t num_tickets )
2639{
Jerry Yud0766ec2022-09-22 10:46:57 +08002640 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002641}
2642#endif
2643
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002644void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2645 mbedtls_ssl_ticket_write_t *f_ticket_write,
2646 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2647 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002648{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002649 conf->f_ticket_write = f_ticket_write;
2650 conf->f_ticket_parse = f_ticket_parse;
2651 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002652}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002655
Hanno Becker7e6c1782021-06-08 09:24:55 +01002656void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2657 mbedtls_ssl_export_keys_t *f_export_keys,
2658 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002659{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002660 ssl->f_export_keys = f_export_keys;
2661 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002662}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002663
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002664#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002665void mbedtls_ssl_conf_async_private_cb(
2666 mbedtls_ssl_config *conf,
2667 mbedtls_ssl_async_sign_t *f_async_sign,
2668 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2669 mbedtls_ssl_async_resume_t *f_async_resume,
2670 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002671 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002672{
2673 conf->f_async_sign_start = f_async_sign;
2674 conf->f_async_decrypt_start = f_async_decrypt;
2675 conf->f_async_resume = f_async_resume;
2676 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002677 conf->p_async_config_data = async_config_data;
2678}
2679
Gilles Peskine8f97af72018-04-26 11:46:10 +02002680void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2681{
2682 return( conf->p_async_config_data );
2683}
2684
Gilles Peskine1febfef2018-04-30 11:54:39 +02002685void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002686{
2687 if( ssl->handshake == NULL )
2688 return( NULL );
2689 else
2690 return( ssl->handshake->user_async_ctx );
2691}
2692
Gilles Peskine1febfef2018-04-30 11:54:39 +02002693void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002694 void *ctx )
2695{
2696 if( ssl->handshake != NULL )
2697 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002698}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002699#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002700
Paul Bakker5121ce52009-01-03 21:22:43 +00002701/*
2702 * SSL get accessors
2703 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002704uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002706 if( ssl->session != NULL )
2707 return( ssl->session->verify_result );
2708
2709 if( ssl->session_negotiate != NULL )
2710 return( ssl->session_negotiate->verify_result );
2711
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002712 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713}
2714
Glenn Strauss8f526902022-01-13 00:04:49 -05002715int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2716{
2717 if( ssl == NULL || ssl->session == NULL )
2718 return( 0 );
2719
2720 return( ssl->session->ciphersuite );
2721}
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002724{
Paul Bakker926c8e42013-03-06 10:23:34 +01002725 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002726 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002729}
2730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002735 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002736 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002737 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002738 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002739 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002740 default:
2741 return( "unknown (DTLS)" );
2742 }
2743 }
2744#endif
2745
Glenn Strauss60bfe602022-03-14 19:04:24 -04002746 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002747 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002748 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002749 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002750 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002751 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002752 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002753 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002754 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002755}
2756
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002757#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002758size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2759{
David Horstmann95d516f2021-05-04 18:36:56 +01002760 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002761 size_t read_mfl;
2762
2763 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2764 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2765 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2766 {
2767 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2768 }
2769
2770 /* Check if a smaller max length was negotiated */
2771 if( ssl->session_out != NULL )
2772 {
2773 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2774 if( read_mfl < max_len )
2775 {
2776 max_len = read_mfl;
2777 }
2778 }
2779
2780 // During a handshake, use the value being negotiated
2781 if( ssl->session_negotiate != NULL )
2782 {
2783 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2784 if( read_mfl < max_len )
2785 {
2786 max_len = read_mfl;
2787 }
2788 }
2789
2790 return( max_len );
2791}
2792
2793size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002794{
2795 size_t max_len;
2796
2797 /*
2798 * Assume mfl_code is correct since it was checked when set
2799 */
Angus Grattond8213d02016-05-25 20:56:48 +10002800 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002801
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002802 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002803 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002804 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002805 {
Angus Grattond8213d02016-05-25 20:56:48 +10002806 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002807 }
2808
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002809 /* During a handshake, use the value being negotiated */
2810 if( ssl->session_negotiate != NULL &&
2811 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2812 {
2813 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2814 }
2815
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002816 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002817}
2818#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2819
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002821size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002822{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002823 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2824 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2825 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2826 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2827 return ( 0 );
2828
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002829 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2830 return( ssl->mtu );
2831
2832 if( ssl->mtu == 0 )
2833 return( ssl->handshake->mtu );
2834
2835 return( ssl->mtu < ssl->handshake->mtu ?
2836 ssl->mtu : ssl->handshake->mtu );
2837}
2838#endif /* MBEDTLS_SSL_PROTO_DTLS */
2839
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002840int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2841{
2842 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2843
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002844#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2845 !defined(MBEDTLS_SSL_PROTO_DTLS)
2846 (void) ssl;
2847#endif
2848
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002849#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002850 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002851
2852 if( max_len > mfl )
2853 max_len = mfl;
2854#endif
2855
2856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002857 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002858 {
Hanno Becker89490712020-02-05 10:50:12 +00002859 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002860 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2861 const size_t overhead = (size_t) ret;
2862
2863 if( ret < 0 )
2864 return( ret );
2865
2866 if( mtu <= overhead )
2867 {
2868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2869 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2870 }
2871
2872 if( max_len > mtu - overhead )
2873 max_len = mtu - overhead;
2874 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002875#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002876
Hanno Becker0defedb2018-08-10 12:35:02 +01002877#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2878 !defined(MBEDTLS_SSL_PROTO_DTLS)
2879 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002880#endif
2881
2882 return( (int) max_len );
2883}
2884
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002885int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2886{
2887 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2888
2889#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2890 (void) ssl;
2891#endif
2892
2893#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2894 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2895
2896 if( max_len > mfl )
2897 max_len = mfl;
2898#endif
2899
2900 return( (int) max_len );
2901}
2902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#if defined(MBEDTLS_X509_CRT_PARSE_C)
2904const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002905{
2906 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002907 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002908
Hanno Beckere6824572019-02-07 13:18:46 +00002909#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002910 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002911#else
2912 return( NULL );
2913#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002918int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2919 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002920{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002921 int ret;
2922
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002923 if( ssl == NULL ||
2924 dst == NULL ||
2925 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002926 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002929 }
2930
Hanno Beckere810bbc2021-05-14 16:01:05 +01002931 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2932 * idempotent: Each session can only be exported once.
2933 *
2934 * (This is in preparation for TLS 1.3 support where we will
2935 * need the ability to export multiple sessions (aka tickets),
2936 * which will be achieved by calling mbedtls_ssl_get_session()
2937 * multiple times until it fails.)
2938 *
2939 * Check whether we have already exported the current session,
2940 * and fail if so.
2941 */
2942 if( ssl->session->exported == 1 )
2943 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2944
2945 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2946 if( ret != 0 )
2947 return( ret );
2948
2949 /* Remember that we've exported the session. */
2950 ssl->session->exported = 1;
2951 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002954
Paul Bakker5121ce52009-01-03 21:22:43 +00002955/*
Hanno Beckera835da52019-05-16 12:39:07 +01002956 * Define ticket header determining Mbed TLS version
2957 * and structure of the ticket.
2958 */
2959
Hanno Becker94ef3b32019-05-16 12:50:45 +01002960/*
Hanno Becker50b59662019-05-28 14:30:45 +01002961 * Define bitflag determining compile-time settings influencing
2962 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002963 */
2964
Hanno Becker50b59662019-05-28 14:30:45 +01002965#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002966#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002967#else
Hanno Becker3e088662019-05-29 11:10:18 +01002968#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002969#endif /* MBEDTLS_HAVE_TIME */
2970
2971#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002972#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002973#else
Hanno Becker3e088662019-05-29 11:10:18 +01002974#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002975#endif /* MBEDTLS_X509_CRT_PARSE_C */
2976
2977#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002978#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979#else
Hanno Becker3e088662019-05-29 11:10:18 +01002980#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002981#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2982
2983#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002984#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002985#else
Hanno Becker3e088662019-05-29 11:10:18 +01002986#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002987#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2988
Hanno Becker94ef3b32019-05-16 12:50:45 +01002989#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002990#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002991#else
Hanno Becker3e088662019-05-29 11:10:18 +01002992#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002993#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2994
Hanno Becker94ef3b32019-05-16 12:50:45 +01002995#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2996#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2997#else
2998#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2999#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3000
Hanno Becker3e088662019-05-29 11:10:18 +01003001#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3002#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3003#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3004#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003005#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3006#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003007
Hanno Becker50b59662019-05-28 14:30:45 +01003008#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003009 ( (uint16_t) ( \
3010 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3011 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3012 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3013 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003014 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003015 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003016
Hanno Beckerf8787072019-05-16 12:41:07 +01003017static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003018 MBEDTLS_VERSION_MAJOR,
3019 MBEDTLS_VERSION_MINOR,
3020 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003021 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3022 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003023};
Hanno Beckera835da52019-05-16 12:39:07 +01003024
3025/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003026 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003027 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003028 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003029 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003030 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003031 * opaque mbedtls_version[3]; // library version: major, minor, patch
3032 * opaque session_format[2]; // library-version specific 16-bit field
3033 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003034 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003035 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003036 * Note: When updating the format, remember to keep
3037 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003038 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003039 * // In this version, `session_format` determines
3040 * // the setting of those compile-time
3041 * // configuration options which influence
3042 * // the structure of mbedtls_ssl_session.
3043 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003044 * uint8_t minor_ver; // Protocol minor version. Possible values:
3045 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003046 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003047 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003048 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003049 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003050 * serialized_session_tls12 data;
3051 *
3052 * };
3053 *
3054 * } serialized_session;
3055 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003056 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003057
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003058MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003059static int ssl_session_save( const mbedtls_ssl_session *session,
3060 unsigned char omit_header,
3061 unsigned char *buf,
3062 size_t buf_len,
3063 size_t *olen )
3064{
3065 unsigned char *p = buf;
3066 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003067 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003068#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3069 size_t out_len;
3070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3071#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003072 if( session == NULL )
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3074
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003075 if( !omit_header )
3076 {
3077 /*
3078 * Add Mbed TLS version identifier
3079 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003080 used += sizeof( ssl_serialized_session_header );
3081
3082 if( used <= buf_len )
3083 {
3084 memcpy( p, ssl_serialized_session_header,
3085 sizeof( ssl_serialized_session_header ) );
3086 p += sizeof( ssl_serialized_session_header );
3087 }
3088 }
3089
3090 /*
3091 * TLS version identifier
3092 */
3093 used += 1;
3094 if( used <= buf_len )
3095 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003096 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003097 }
3098
3099 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003100 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003101 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003102 {
3103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003104 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003105 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003106 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3108
Jerry Yu251a12e2022-07-13 15:15:48 +08003109#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3110 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003111 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3112 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003113 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003114 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003115 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003116#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3117
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003118 default:
3119 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3120 }
3121
3122 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003123 if( used > buf_len )
3124 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003125
3126 return( 0 );
3127}
3128
3129/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003130 * Public wrapper for ssl_session_save()
3131 */
3132int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3133 unsigned char *buf,
3134 size_t buf_len,
3135 size_t *olen )
3136{
3137 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3138}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003139
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003140/*
3141 * Deserialize session, see mbedtls_ssl_session_save() for format.
3142 *
3143 * This internal version is wrapped by a public function that cleans up in
3144 * case of error, and has an extra option omit_header.
3145 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003146MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003147static int ssl_session_load( mbedtls_ssl_session *session,
3148 unsigned char omit_header,
3149 const unsigned char *buf,
3150 size_t len )
3151{
3152 const unsigned char *p = buf;
3153 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003154 size_t remaining_len;
3155
3156
3157 if( session == NULL )
3158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003159
3160 if( !omit_header )
3161 {
3162 /*
3163 * Check Mbed TLS version identifier
3164 */
3165
3166 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3168
3169 if( memcmp( p, ssl_serialized_session_header,
3170 sizeof( ssl_serialized_session_header ) ) != 0 )
3171 {
3172 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3173 }
3174 p += sizeof( ssl_serialized_session_header );
3175 }
3176
3177 /*
3178 * TLS version identifier
3179 */
3180 if( 1 > (size_t)( end - p ) )
3181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003182 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003183
3184 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003185 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003186 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003187 {
3188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003189 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003190 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003191#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3192
Jerry Yu438ddd82022-07-07 06:55:50 +00003193#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3194 case MBEDTLS_SSL_VERSION_TLS1_3:
3195 return( ssl_tls13_session_load( session, p, remaining_len ) );
3196#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3197
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003198 default:
3199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3200 }
3201}
3202
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003203/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003204 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003205 */
3206int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3207 const unsigned char *buf,
3208 size_t len )
3209{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003210 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003211
3212 if( ret != 0 )
3213 mbedtls_ssl_session_free( session );
3214
3215 return( ret );
3216}
3217
3218/*
Paul Bakker1961b702013-01-25 14:49:24 +01003219 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003221MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003222static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3223{
3224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3225
Ronald Cron66dbf912022-02-02 15:33:46 +01003226 /*
3227 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003228 * that were written into the output buffer by the previous handshake step,
3229 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003230 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3231 * We proceed to the next handshake step only when all data from the
3232 * previous one have been sent to the peer, thus we make sure that this is
3233 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3234 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3235 * we have to wait before to go ahead.
3236 * In the case of TLS 1.3, handshake step handlers do not send data to the
3237 * peer. Data are only sent here and through
3238 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003239 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003240 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003241 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3242 return( ret );
3243
3244#if defined(MBEDTLS_SSL_PROTO_DTLS)
3245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3246 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3247 {
3248 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3249 return( ret );
3250 }
3251#endif /* MBEDTLS_SSL_PROTO_DTLS */
3252
3253 return( ret );
3254}
3255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003257{
Hanno Becker41934dd2021-08-07 19:13:43 +01003258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Hanno Becker41934dd2021-08-07 19:13:43 +01003260 if( ssl == NULL ||
3261 ssl->conf == NULL ||
3262 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003263 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003264 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003266 }
3267
3268 ret = ssl_prepare_handshake_step( ssl );
3269 if( ret != 0 )
3270 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003271
Jerry Yue7047812021-09-13 19:26:39 +08003272 ret = mbedtls_ssl_handle_pending_alert( ssl );
3273 if( ret != 0 )
3274 goto cleanup;
3275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003278 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3280 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003281
Ronald Cron9f0fba32022-02-10 16:45:15 +01003282 switch( ssl->state )
3283 {
3284 case MBEDTLS_SSL_HELLO_REQUEST:
3285 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3286 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003287
Ronald Cron9f0fba32022-02-10 16:45:15 +01003288 case MBEDTLS_SSL_CLIENT_HELLO:
3289 ret = mbedtls_ssl_write_client_hello( ssl );
3290 break;
3291
3292 default:
3293#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003294 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003295 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3296 else
3297 ret = mbedtls_ssl_handshake_client_step( ssl );
3298#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3299 ret = mbedtls_ssl_handshake_client_step( ssl );
3300#else
3301 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3302#endif
3303 }
Jerry Yub9930e72021-08-06 17:11:51 +08003304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003307 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003308 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003309#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003310 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003311 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003312#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003313
3314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3315 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3316 ret = mbedtls_ssl_handshake_server_step( ssl );
3317#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003319#endif
3320
Jerry Yue7047812021-09-13 19:26:39 +08003321 if( ret != 0 )
3322 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003323 /* handshake_step return error. And it is same
3324 * with alert_reason.
3325 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003326 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003327 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003328 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003329 goto cleanup;
3330 }
3331 }
3332
3333cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003334 return( ret );
3335}
3336
3337/*
3338 * Perform the SSL handshake
3339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003341{
3342 int ret = 0;
3343
Hanno Beckera817ea42020-10-20 15:20:23 +01003344 /* Sanity checks */
3345
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003346 if( ssl == NULL || ssl->conf == NULL )
3347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3348
Hanno Beckera817ea42020-10-20 15:20:23 +01003349#if defined(MBEDTLS_SSL_PROTO_DTLS)
3350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3351 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3352 {
3353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3354 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3356 }
3357#endif /* MBEDTLS_SSL_PROTO_DTLS */
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003360
Hanno Beckera817ea42020-10-20 15:20:23 +01003361 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003362 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003365
3366 if( ret != 0 )
3367 break;
3368 }
3369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
3372 return( ret );
3373}
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#if defined(MBEDTLS_SSL_RENEGOTIATION)
3376#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003377/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003378 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003379 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003380MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003382{
Janos Follath865b3eb2019-12-16 11:46:15 +00003383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003386
3387 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3389 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003390
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003391 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003392 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003394 return( ret );
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003398
3399 return( 0 );
3400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003402
3403/*
3404 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 * - any side: calling mbedtls_ssl_renegotiate(),
3406 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3407 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003408 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003409 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003411 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003412int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003413{
Janos Follath865b3eb2019-12-16 11:46:15 +00003414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003417
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003418 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3419 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003420
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003421 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3422 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003426 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003428 ssl->handshake->out_msg_seq = 1;
3429 else
3430 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003431 }
3432#endif
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3435 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003440 return( ret );
3441 }
3442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003444
3445 return( 0 );
3446}
3447
3448/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003449 * Renegotiate current connection on client,
3450 * or request renegotiation on server
3451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003455
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003456 if( ssl == NULL || ssl->conf == NULL )
3457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003460 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003461 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003462 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003463 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003467
3468 /* Did we already try/start sending HelloRequest? */
3469 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003471
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003472 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003477 /*
3478 * On client, either start the renegotiation process or,
3479 * if already in progress, continue the handshake
3480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003482 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003483 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003485
Hanno Becker40cdaa12020-02-05 10:48:27 +00003486 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003487 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003489 return( ret );
3490 }
3491 }
3492 else
3493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003497 return( ret );
3498 }
3499 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003501
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003502 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003505
Gilles Peskine9b562d52018-04-25 20:32:43 +02003506void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003507{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003508 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3509
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003510 if( handshake == NULL )
3511 return;
3512
Brett Warrene0edc842021-08-17 09:53:13 +01003513#if defined(MBEDTLS_ECP_C)
3514#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3515 if ( ssl->handshake->group_list_heap_allocated )
3516 mbedtls_free( (void*) handshake->group_list );
3517 handshake->group_list = NULL;
3518#endif /* MBEDTLS_DEPRECATED_REMOVED */
3519#endif /* MBEDTLS_ECP_C */
3520
Jerry Yuf017ee42022-01-12 15:49:48 +08003521#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3522#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3523 if ( ssl->handshake->sig_algs_heap_allocated )
3524 mbedtls_free( (void*) handshake->sig_algs );
3525 handshake->sig_algs = NULL;
3526#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003527#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3528 if( ssl->handshake->certificate_request_context )
3529 {
3530 mbedtls_free( (void*) handshake->certificate_request_context );
3531 }
3532#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003533#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3534
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003535#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3536 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3537 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003538 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003539 handshake->async_in_progress = 0;
3540 }
3541#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3542
Andrzej Kurek25f27152022-08-17 16:09:31 -04003543#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003544#if defined(MBEDTLS_USE_PSA_CRYPTO)
3545 psa_hash_abort( &handshake->fin_sha256_psa );
3546#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003547 mbedtls_sha256_free( &handshake->fin_sha256 );
3548#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003549#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003550#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003551#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003552 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003553#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003554 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003555#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003556#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558#if defined(MBEDTLS_DHM_C)
3559 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003560#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003561#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003563#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003564#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003565 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003566#if defined(MBEDTLS_SSL_CLI_C)
3567 mbedtls_free( handshake->ecjpake_cache );
3568 handshake->ecjpake_cache = NULL;
3569 handshake->ecjpake_cache_len = 0;
3570#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003571#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003572
Janos Follath4ae5c292016-02-10 11:27:43 +00003573#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3574 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003575 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003577#endif
3578
Gilles Peskineeccd8882020-03-10 12:19:08 +01003579#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003580#if defined(MBEDTLS_USE_PSA_CRYPTO)
3581 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3582 {
3583 /* The maintenance of the external PSK key slot is the
3584 * user's responsibility. */
3585 if( ssl->handshake->psk_opaque_is_internal )
3586 {
3587 psa_destroy_key( ssl->handshake->psk_opaque );
3588 ssl->handshake->psk_opaque_is_internal = 0;
3589 }
3590 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3591 }
Neil Armstronge952a302022-05-03 10:22:14 +02003592#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003593 if( handshake->psk != NULL )
3594 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003595 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003596 mbedtls_free( handshake->psk );
3597 }
Neil Armstronge952a302022-05-03 10:22:14 +02003598#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003599#endif
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3602 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003603 /*
3604 * Free only the linked list wrapper, not the keys themselves
3605 * since the belong to the SNI callback
3606 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003607 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003609
Gilles Peskineeccd8882020-03-10 12:19:08 +01003610#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003611 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003612 if( handshake->ecrs_peer_cert != NULL )
3613 {
3614 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3615 mbedtls_free( handshake->ecrs_peer_cert );
3616 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003617#endif
3618
Hanno Becker75173122019-02-06 16:18:31 +00003619#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3620 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3621 mbedtls_pk_free( &handshake->peer_pubkey );
3622#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3623
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003624#if defined(MBEDTLS_SSL_CLI_C) && \
3625 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3626 mbedtls_free( handshake->cookie );
3627#endif /* MBEDTLS_SSL_CLI_C &&
3628 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003629
3630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003631 mbedtls_ssl_flight_free( handshake->flight );
3632 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003634
Ronald Cronf12b81d2022-03-15 10:42:41 +01003635#if defined(MBEDTLS_ECDH_C) && \
3636 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003637 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003638 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003639#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3640
Ronald Cron6f135e12021-12-08 16:57:54 +01003641#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003642 mbedtls_ssl_transform_free( handshake->transform_handshake );
3643 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003644 mbedtls_free( handshake->transform_earlydata );
3645 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003646#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003647
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003648
3649#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3650 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3651 * processes datagrams and the fact that a datagram is allowed to have
3652 * several records in it, it is possible that the I/O buffers are not
3653 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003654 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3655 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003656#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003657
Jerry Yuba9c7272021-10-30 11:54:10 +08003658 /* mbedtls_platform_zeroize MUST be last one in this function */
3659 mbedtls_platform_zeroize( handshake,
3660 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003661}
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003664{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003665 if( session == NULL )
3666 return;
3667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003669 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003670#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003671
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003672#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003674#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003675
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003676 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003677}
3678
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003679#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003680
3681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3682#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3683#else
3684#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3685#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3686
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003687#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003688
3689#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3691#else
3692#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3693#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3694
3695#if defined(MBEDTLS_SSL_ALPN)
3696#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3697#else
3698#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3699#endif /* MBEDTLS_SSL_ALPN */
3700
3701#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3702#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3703#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3704#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3705
3706#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3707 ( (uint32_t) ( \
3708 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3709 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3710 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3711 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3712 0u ) )
3713
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003714static unsigned char ssl_serialized_context_header[] = {
3715 MBEDTLS_VERSION_MAJOR,
3716 MBEDTLS_VERSION_MINOR,
3717 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003718 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3719 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3720 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3721 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3722 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003723};
3724
Paul Bakker5121ce52009-01-03 21:22:43 +00003725/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003726 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003727 *
3728 * The format of the serialized data is:
3729 * (in the presentation language of TLS, RFC 8446 section 3)
3730 *
3731 * // header
3732 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003733 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003734 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003735 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003736 * Note: When updating the format, remember to keep these
3737 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003738 *
3739 * // session sub-structure
3740 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3741 * // transform sub-structure
3742 * uint8 random[64]; // ServerHello.random+ClientHello.random
3743 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3744 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3745 * // fields from ssl_context
3746 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3747 * uint64 in_window_top; // DTLS: last validated record seq_num
3748 * uint64 in_window; // DTLS: bitmask for replay protection
3749 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3750 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3751 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3752 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3753 *
3754 * Note that many fields of the ssl_context or sub-structures are not
3755 * serialized, as they fall in one of the following categories:
3756 *
3757 * 1. forced value (eg in_left must be 0)
3758 * 2. pointer to dynamically-allocated memory (eg session, transform)
3759 * 3. value can be re-derived from other data (eg session keys from MS)
3760 * 4. value was temporary (eg content of input buffer)
3761 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003762 */
3763int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3764 unsigned char *buf,
3765 size_t buf_len,
3766 size_t *olen )
3767{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003768 unsigned char *p = buf;
3769 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003770 size_t session_len;
3771 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003772
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003773 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003774 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3775 * this function's documentation.
3776 *
3777 * These are due to assumptions/limitations in the implementation. Some of
3778 * them are likely to stay (no handshake in progress) some might go away
3779 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003780 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003781 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003782 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003783 {
3784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003786 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003787 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003788 {
3789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003791 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003792 /* Double-check that sub-structures are indeed ready */
3793 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003797 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003798 /* There must be no pending incoming or outgoing data */
3799 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003800 {
3801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003803 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003804 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003808 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003809 /* Protocol must be DLTS, not TLS */
3810 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003811 {
3812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003814 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003815 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003816 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
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 /* We must be using an AEAD ciphersuite */
3822 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003826 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003827 /* Renegotiation must not be enabled */
3828#if defined(MBEDTLS_SSL_RENEGOTIATION)
3829 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003830 {
3831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003833 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003834#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003835
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003836 /*
3837 * Version and format identifier
3838 */
3839 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003840
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003841 if( used <= buf_len )
3842 {
3843 memcpy( p, ssl_serialized_context_header,
3844 sizeof( ssl_serialized_context_header ) );
3845 p += sizeof( ssl_serialized_context_header );
3846 }
3847
3848 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003849 * Session (length + data)
3850 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003851 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003852 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3853 return( ret );
3854
3855 used += 4 + session_len;
3856 if( used <= buf_len )
3857 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003858 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3859 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003860
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003861 ret = ssl_session_save( ssl->session, 1,
3862 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003863 if( ret != 0 )
3864 return( ret );
3865
3866 p += session_len;
3867 }
3868
3869 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003870 * Transform
3871 */
3872 used += sizeof( ssl->transform->randbytes );
3873 if( used <= buf_len )
3874 {
3875 memcpy( p, ssl->transform->randbytes,
3876 sizeof( ssl->transform->randbytes ) );
3877 p += sizeof( ssl->transform->randbytes );
3878 }
3879
3880#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3881 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3882 if( used <= buf_len )
3883 {
3884 *p++ = ssl->transform->in_cid_len;
3885 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3886 p += ssl->transform->in_cid_len;
3887
3888 *p++ = ssl->transform->out_cid_len;
3889 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3890 p += ssl->transform->out_cid_len;
3891 }
3892#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3893
3894 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003895 * Saved fields from top-level ssl_context structure
3896 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003897 used += 4;
3898 if( used <= buf_len )
3899 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003900 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3901 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003902 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003903
3904#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3905 used += 16;
3906 if( used <= buf_len )
3907 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003908 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3909 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003910
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003911 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3912 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003913 }
3914#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3915
3916#if defined(MBEDTLS_SSL_PROTO_DTLS)
3917 used += 1;
3918 if( used <= buf_len )
3919 {
3920 *p++ = ssl->disable_datagram_packing;
3921 }
3922#endif /* MBEDTLS_SSL_PROTO_DTLS */
3923
Jerry Yuae0b2e22021-10-08 15:21:19 +08003924 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003925 if( used <= buf_len )
3926 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003927 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3928 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003929 }
3930
3931#if defined(MBEDTLS_SSL_PROTO_DTLS)
3932 used += 2;
3933 if( used <= buf_len )
3934 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003935 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3936 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003937 }
3938#endif /* MBEDTLS_SSL_PROTO_DTLS */
3939
3940#if defined(MBEDTLS_SSL_ALPN)
3941 {
3942 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003943 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003944 : 0;
3945
3946 used += 1 + alpn_len;
3947 if( used <= buf_len )
3948 {
3949 *p++ = alpn_len;
3950
3951 if( ssl->alpn_chosen != NULL )
3952 {
3953 memcpy( p, ssl->alpn_chosen, alpn_len );
3954 p += alpn_len;
3955 }
3956 }
3957 }
3958#endif /* MBEDTLS_SSL_ALPN */
3959
3960 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003961 * Done
3962 */
3963 *olen = used;
3964
3965 if( used > buf_len )
3966 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003967
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003968 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3969
Hanno Becker43aefe22020-02-05 10:44:56 +00003970 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003971}
3972
3973/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003974 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003975 *
3976 * This internal version is wrapped by a public function that cleans up in
3977 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003978 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003979MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003980static int ssl_context_load( mbedtls_ssl_context *ssl,
3981 const unsigned char *buf,
3982 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003983{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003984 const unsigned char *p = buf;
3985 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003986 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003988
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003989 /*
3990 * The context should have been freshly setup or reset.
3991 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003992 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003993 * renegotiating, or if the user mistakenly loaded a session first.)
3994 */
3995 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3996 ssl->session != NULL )
3997 {
3998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3999 }
4000
4001 /*
4002 * We can't check that the config matches the initial one, but we can at
4003 * least check it matches the requirements for serializing.
4004 */
4005 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004006 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4007 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004008#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004009 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004010#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004011 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004012 {
4013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4014 }
4015
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004016 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4017
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004018 /*
4019 * Check version identifier
4020 */
4021 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4023
4024 if( memcmp( p, ssl_serialized_context_header,
4025 sizeof( ssl_serialized_context_header ) ) != 0 )
4026 {
4027 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4028 }
4029 p += sizeof( ssl_serialized_context_header );
4030
4031 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004032 * Session
4033 */
4034 if( (size_t)( end - p ) < 4 )
4035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4036
4037 session_len = ( (size_t) p[0] << 24 ) |
4038 ( (size_t) p[1] << 16 ) |
4039 ( (size_t) p[2] << 8 ) |
4040 ( (size_t) p[3] );
4041 p += 4;
4042
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004043 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004044 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004045 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004046 ssl->session_in = ssl->session;
4047 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004048 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004049
4050 if( (size_t)( end - p ) < session_len )
4051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4052
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004053 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004054 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004055 {
4056 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004057 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004058 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004059
4060 p += session_len;
4061
4062 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004063 * Transform
4064 */
4065
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004066 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004067 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004068 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004069 ssl->transform_in = ssl->transform;
4070 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004071 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004072
4073 /* Read random bytes and populate structure */
4074 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004076#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004077 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004078 ssl->session->ciphersuite,
4079 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004080#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004081 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004082#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004083 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4084 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004085 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004086 ssl->conf->endpoint,
4087 ssl );
4088 if( ret != 0 )
4089 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004091 p += sizeof( ssl->transform->randbytes );
4092
4093#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4094 /* Read connection IDs and store them */
4095 if( (size_t)( end - p ) < 1 )
4096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4097
4098 ssl->transform->in_cid_len = *p++;
4099
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004100 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4102
4103 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4104 p += ssl->transform->in_cid_len;
4105
4106 ssl->transform->out_cid_len = *p++;
4107
4108 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4110
4111 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4112 p += ssl->transform->out_cid_len;
4113#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4114
4115 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004116 * Saved fields from top-level ssl_context structure
4117 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004118 if( (size_t)( end - p ) < 4 )
4119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4120
4121 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4122 ( (uint32_t) p[1] << 16 ) |
4123 ( (uint32_t) p[2] << 8 ) |
4124 ( (uint32_t) p[3] );
4125 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004126
4127#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4128 if( (size_t)( end - p ) < 16 )
4129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4130
4131 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4132 ( (uint64_t) p[1] << 48 ) |
4133 ( (uint64_t) p[2] << 40 ) |
4134 ( (uint64_t) p[3] << 32 ) |
4135 ( (uint64_t) p[4] << 24 ) |
4136 ( (uint64_t) p[5] << 16 ) |
4137 ( (uint64_t) p[6] << 8 ) |
4138 ( (uint64_t) p[7] );
4139 p += 8;
4140
4141 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4142 ( (uint64_t) p[1] << 48 ) |
4143 ( (uint64_t) p[2] << 40 ) |
4144 ( (uint64_t) p[3] << 32 ) |
4145 ( (uint64_t) p[4] << 24 ) |
4146 ( (uint64_t) p[5] << 16 ) |
4147 ( (uint64_t) p[6] << 8 ) |
4148 ( (uint64_t) p[7] );
4149 p += 8;
4150#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4151
4152#if defined(MBEDTLS_SSL_PROTO_DTLS)
4153 if( (size_t)( end - p ) < 1 )
4154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4155
4156 ssl->disable_datagram_packing = *p++;
4157#endif /* MBEDTLS_SSL_PROTO_DTLS */
4158
Jerry Yud9a94fe2021-09-28 18:58:59 +08004159 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004161 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4162 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004163
4164#if defined(MBEDTLS_SSL_PROTO_DTLS)
4165 if( (size_t)( end - p ) < 2 )
4166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4167
4168 ssl->mtu = ( p[0] << 8 ) | p[1];
4169 p += 2;
4170#endif /* MBEDTLS_SSL_PROTO_DTLS */
4171
4172#if defined(MBEDTLS_SSL_ALPN)
4173 {
4174 uint8_t alpn_len;
4175 const char **cur;
4176
4177 if( (size_t)( end - p ) < 1 )
4178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4179
4180 alpn_len = *p++;
4181
4182 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4183 {
4184 /* alpn_chosen should point to an item in the configured list */
4185 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4186 {
4187 if( strlen( *cur ) == alpn_len &&
4188 memcmp( p, cur, alpn_len ) == 0 )
4189 {
4190 ssl->alpn_chosen = *cur;
4191 break;
4192 }
4193 }
4194 }
4195
4196 /* can only happen on conf mismatch */
4197 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4199
4200 p += alpn_len;
4201 }
4202#endif /* MBEDTLS_SSL_ALPN */
4203
4204 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004205 * Forced fields from top-level ssl_context structure
4206 *
4207 * Most of them already set to the correct value by mbedtls_ssl_init() and
4208 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4209 */
4210 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004211 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004212
Hanno Becker361b10d2019-08-30 10:42:49 +01004213 /* Adjust pointers for header fields of outgoing records to
4214 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004215 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004216
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004217#if defined(MBEDTLS_SSL_PROTO_DTLS)
4218 ssl->in_epoch = 1;
4219#endif
4220
4221 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4222 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004223 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4224 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004225 if( ssl->handshake != NULL )
4226 {
4227 mbedtls_ssl_handshake_free( ssl );
4228 mbedtls_free( ssl->handshake );
4229 ssl->handshake = NULL;
4230 }
4231
4232 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004233 * Done - should have consumed entire buffer
4234 */
4235 if( p != end )
4236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004237
4238 return( 0 );
4239}
4240
4241/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004242 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004243 */
4244int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4245 const unsigned char *buf,
4246 size_t len )
4247{
4248 int ret = ssl_context_load( context, buf, len );
4249
4250 if( ret != 0 )
4251 mbedtls_ssl_free( context );
4252
4253 return( ret );
4254}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004255#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004256
4257/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 * Free an SSL context
4259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004261{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004262 if( ssl == NULL )
4263 return;
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004267 if( ssl->out_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 out_buf_len = ssl->out_buf_len;
4271#else
4272 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4273#endif
4274
Darryl Greenb33cc762019-11-28 14:29:44 +00004275 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004277 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004278 }
4279
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004280 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 {
sander-visserb8aa2072020-05-06 22:05:13 +02004282#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4283 size_t in_buf_len = ssl->in_buf_len;
4284#else
4285 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4286#endif
4287
Darryl Greenb33cc762019-11-28 14:29:44 +00004288 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004290 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 }
4292
Paul Bakker48916f92012-09-16 19:57:18 +00004293 if( ssl->transform )
4294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 mbedtls_ssl_transform_free( ssl->transform );
4296 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004297 }
4298
4299 if( ssl->handshake )
4300 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004301 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4303 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 mbedtls_free( ssl->handshake );
4306 mbedtls_free( ssl->transform_negotiate );
4307 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004308 }
4309
Ronald Cron6f135e12021-12-08 16:57:54 +01004310#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004311 mbedtls_ssl_transform_free( ssl->transform_application );
4312 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004313#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004314
Paul Bakkerc0463502013-02-14 11:19:38 +01004315 if( ssl->session )
4316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 mbedtls_ssl_session_free( ssl->session );
4318 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004319 }
4320
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004321#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004322 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004324 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004326 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004328
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004329#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004331#endif
4332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004334
Paul Bakker86f04f42013-02-14 11:20:09 +01004335 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004336 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004337}
4338
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004339/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004340 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004341 */
4342void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4343{
4344 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4345}
4346
Gilles Peskineae270bf2021-06-02 00:05:29 +02004347/* The selection should be the same as mbedtls_x509_crt_profile_default in
4348 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004349 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004350 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004351 * about this list.
4352 */
Brett Warrene0edc842021-08-17 09:53:13 +01004353static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004354#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004355 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004356#endif
4357#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004358 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004359#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004360#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004361 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004362#endif
4363#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004364 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004365#endif
4366#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004367 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004368#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004369#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004370 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004371#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004372#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004373 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004374#endif
4375#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004376 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004377#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004378 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004379};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004380
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004381static int ssl_preset_suiteb_ciphersuites[] = {
4382 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4383 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4384 0
4385};
4386
Gilles Peskineeccd8882020-03-10 12:19:08 +01004387#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004388
Jerry Yu909df7b2022-01-22 11:56:27 +08004389/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004390 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004391 * rules SHOULD be upheld.
4392 * - No duplicate entries.
4393 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004394 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004395 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004396 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004397static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004398
Andrzej Kurek25f27152022-08-17 16:09:31 -04004399#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 +08004400 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4401 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004402#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004403 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004404
Andrzej Kurek25f27152022-08-17 16:09:31 -04004405#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 +08004406 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4407 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004408#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004409 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4410
Andrzej Kurek25f27152022-08-17 16:09:31 -04004411#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 +08004412 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4413 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004414#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004415 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004416
Andrzej Kurek25f27152022-08-17 16:09:31 -04004417#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 +08004418 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004419#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 +08004420
Andrzej Kurek25f27152022-08-17 16:09:31 -04004421#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 +08004422 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004423#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 +08004424
Andrzej Kurek25f27152022-08-17 16:09:31 -04004425#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 +08004426 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004427#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 +08004428
Andrzej Kurek25f27152022-08-17 16:09:31 -04004429#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 +08004430 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4431#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4432
Andrzej Kurek25f27152022-08-17 16:09:31 -04004433#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 +08004434 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4435#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4436
Andrzej Kurek25f27152022-08-17 16:09:31 -04004437#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 +08004438 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4439#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4440
Gabor Mezei15b95a62022-05-09 16:37:58 +02004441 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004442};
4443
4444/* NOTICE: see above */
4445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4446static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004447#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004448#if defined(MBEDTLS_ECDSA_C)
4449 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004450#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004451#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4452 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4453#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004454#if defined(MBEDTLS_RSA_C)
4455 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4456#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004457#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004458#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004459#if defined(MBEDTLS_ECDSA_C)
4460 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004461#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004462#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4463 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4464#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004465#if defined(MBEDTLS_RSA_C)
4466 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4467#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004468#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004469#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004470#if defined(MBEDTLS_ECDSA_C)
4471 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004472#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004473#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4474 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4475#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004476#if defined(MBEDTLS_RSA_C)
4477 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4478#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004479#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004480 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004481};
4482#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4483/* NOTICE: see above */
4484static uint16_t ssl_preset_suiteb_sig_algs[] = {
4485
Andrzej Kurek25f27152022-08-17 16:09:31 -04004486#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 +08004487 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4488 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004489#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004490 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4494 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004495#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004496 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004497
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004500#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 +08004501
Andrzej Kurek25f27152022-08-17 16:09:31 -04004502#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 +08004503 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004504#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004505
Gabor Mezei15b95a62022-05-09 16:37:58 +02004506 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004507};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004508
Jerry Yu909df7b2022-01-22 11:56:27 +08004509/* NOTICE: see above */
4510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4511static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004512#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004513#if defined(MBEDTLS_ECDSA_C)
4514 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004515#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004516#if defined(MBEDTLS_RSA_C)
4517 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4518#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004519#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004520#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004521#if defined(MBEDTLS_ECDSA_C)
4522 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004523#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004524#if defined(MBEDTLS_RSA_C)
4525 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4526#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004527#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004528 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004529};
Jerry Yu909df7b2022-01-22 11:56:27 +08004530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4531
Jerry Yu1a8b4812022-01-20 17:56:50 +08004532#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004533
Brett Warrene0edc842021-08-17 09:53:13 +01004534static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004535#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004536 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004537#endif
4538#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004539 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004540#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004541 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004542};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004543
Jerry Yu1a8b4812022-01-20 17:56:50 +08004544#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004545/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004546 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004547MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004548static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004549{
4550 size_t i, j;
4551 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004552
Gabor Mezei15b95a62022-05-09 16:37:58 +02004553 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004554 {
Jerry Yuf377d642022-01-25 10:43:59 +08004555 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004556 {
Jerry Yuf377d642022-01-25 10:43:59 +08004557 if( sig_algs[i] != sig_algs[j] )
4558 continue;
4559 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4560 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4561 sig_algs[i], j, i );
4562 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004563 }
4564 }
4565 return( ret );
4566}
4567
4568#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4569
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004570/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004571 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004572 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004573int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004574 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004575{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004576#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004578#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004579
Jerry Yu1a8b4812022-01-20 17:56:50 +08004580#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004581 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004582 {
4583 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4584 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4585 }
4586
Jerry Yuf377d642022-01-25 10:43:59 +08004587 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004588 {
4589 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4590 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4591 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004592
4593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004594 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004595 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004596 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004597 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4598 }
4599
Jerry Yuf377d642022-01-25 10:43:59 +08004600 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004601 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004602 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004603 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4604 }
4605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004606#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4607
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004608 /* Use the functions here so that they are covered in tests,
4609 * but otherwise access member directly for efficiency */
4610 mbedtls_ssl_conf_endpoint( conf, endpoint );
4611 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004612
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004613 /*
4614 * Things that are common to all presets
4615 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004616#if defined(MBEDTLS_SSL_CLI_C)
4617 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4618 {
4619 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4621 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4622#endif
4623 }
4624#endif
4625
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4627 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4628#endif
4629
4630#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4631 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4632#endif
4633
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004634#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004635 conf->f_cookie_write = ssl_cookie_write_dummy;
4636 conf->f_cookie_check = ssl_cookie_check_dummy;
4637#endif
4638
4639#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4640 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4641#endif
4642
Janos Follath088ce432017-04-10 12:42:31 +01004643#if defined(MBEDTLS_SSL_SRV_C)
4644 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004645 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004646#endif
4647
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004648#if defined(MBEDTLS_SSL_PROTO_DTLS)
4649 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4650 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4651#endif
4652
4653#if defined(MBEDTLS_SSL_RENEGOTIATION)
4654 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004655 memset( conf->renego_period, 0x00, 2 );
4656 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004657#endif
4658
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004659#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004660 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4661 {
4662 const unsigned char dhm_p[] =
4663 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4664 const unsigned char dhm_g[] =
4665 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004666
Hanno Beckere2defad2021-07-24 05:59:17 +01004667 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4668 dhm_p, sizeof( dhm_p ),
4669 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4670 {
4671 return( ret );
4672 }
4673 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004674#endif
4675
Ronald Cron6f135e12021-12-08 16:57:54 +01004676#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004677#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004678 mbedtls_ssl_conf_new_session_tickets(
4679 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4680#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004681 /*
4682 * Allow all TLS 1.3 key exchange modes by default.
4683 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004684 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004685#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004686
XiaokangQian4d3a6042022-04-21 13:46:17 +00004687 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4688 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004690 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004691 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004692#else
XiaokangQian060d8672022-04-21 09:24:56 +00004693 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004694#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004695 }
4696 else
4697 {
4698#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4699 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4700 {
4701 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4702 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4703 }
4704 else
4705 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4706 {
4707 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4708 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4709 }
4710#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4711 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4712 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4713#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4714 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4715 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4716#else
4717 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4718#endif
4719 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004720
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004721 /*
4722 * Preset-specific defaults
4723 */
4724 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004725 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004726 /*
4727 * NSA Suite B
4728 */
4729 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004730
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004731 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004732
4733#if defined(MBEDTLS_X509_CRT_PARSE_C)
4734 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004735#endif
4736
Gilles Peskineeccd8882020-03-10 12:19:08 +01004737#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004738#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4739 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4740 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4741 else
4742#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4743 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004744#endif
4745
Brett Warrene0edc842021-08-17 09:53:13 +01004746#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4747 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004748#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004749 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004750 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004751
4752 /*
4753 * Default
4754 */
4755 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004756
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004757 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004758
4759#if defined(MBEDTLS_X509_CRT_PARSE_C)
4760 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4761#endif
4762
Gilles Peskineeccd8882020-03-10 12:19:08 +01004763#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004764#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4765 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4766 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4767 else
4768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4769 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004770#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004771
Brett Warrene0edc842021-08-17 09:53:13 +01004772#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4773 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004774#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004775 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004776
4777#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4778 conf->dhm_min_bitlen = 1024;
4779#endif
4780 }
4781
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004782 return( 0 );
4783}
4784
4785/*
4786 * Free mbedtls_ssl_config
4787 */
4788void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4789{
4790#if defined(MBEDTLS_DHM_C)
4791 mbedtls_mpi_free( &conf->dhm_P );
4792 mbedtls_mpi_free( &conf->dhm_G );
4793#endif
4794
Gilles Peskineeccd8882020-03-10 12:19:08 +01004795#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004796#if defined(MBEDTLS_USE_PSA_CRYPTO)
4797 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4798 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004799 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4800 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004801#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004802 if( conf->psk != NULL )
4803 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004804 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004805 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004806 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004807 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004808 }
4809
4810 if( conf->psk_identity != NULL )
4811 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004812 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004813 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004814 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004815 conf->psk_identity_len = 0;
4816 }
4817#endif
4818
4819#if defined(MBEDTLS_X509_CRT_PARSE_C)
4820 ssl_key_cert_free( conf->key_cert );
4821#endif
4822
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004823 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004824}
4825
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004826#if defined(MBEDTLS_PK_C) && \
4827 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004828/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#if defined(MBEDTLS_RSA_C)
4834 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4835 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837#if defined(MBEDTLS_ECDSA_C)
4838 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4839 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004842}
4843
Hanno Becker7e5437a2017-04-28 17:15:26 +01004844unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4845{
4846 switch( type ) {
4847 case MBEDTLS_PK_RSA:
4848 return( MBEDTLS_SSL_SIG_RSA );
4849 case MBEDTLS_PK_ECDSA:
4850 case MBEDTLS_PK_ECKEY:
4851 return( MBEDTLS_SSL_SIG_ECDSA );
4852 default:
4853 return( MBEDTLS_SSL_SIG_ANON );
4854 }
4855}
4856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004858{
4859 switch( sig )
4860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861#if defined(MBEDTLS_RSA_C)
4862 case MBEDTLS_SSL_SIG_RSA:
4863 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_ECDSA_C)
4866 case MBEDTLS_SSL_SIG_ECDSA:
4867 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004868#endif
4869 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004871 }
4872}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004873#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004874
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004875/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004876 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004879{
4880 switch( hash )
4881 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004882#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 case MBEDTLS_SSL_HASH_MD5:
4884 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004885#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004886#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 case MBEDTLS_SSL_HASH_SHA1:
4888 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004889#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004890#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 case MBEDTLS_SSL_HASH_SHA224:
4892 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004893#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004894#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 case MBEDTLS_SSL_HASH_SHA256:
4896 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004897#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004898#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 case MBEDTLS_SSL_HASH_SHA384:
4900 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004901#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004902#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 case MBEDTLS_SSL_HASH_SHA512:
4904 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004905#endif
4906 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004908 }
4909}
4910
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004911/*
4912 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4913 */
4914unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4915{
4916 switch( md )
4917 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004918#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004919 case MBEDTLS_MD_MD5:
4920 return( MBEDTLS_SSL_HASH_MD5 );
4921#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004922#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004923 case MBEDTLS_MD_SHA1:
4924 return( MBEDTLS_SSL_HASH_SHA1 );
4925#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004926#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004927 case MBEDTLS_MD_SHA224:
4928 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004929#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004930#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004931 case MBEDTLS_MD_SHA256:
4932 return( MBEDTLS_SSL_HASH_SHA256 );
4933#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004934#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004935 case MBEDTLS_MD_SHA384:
4936 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004937#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004938#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004939 case MBEDTLS_MD_SHA512:
4940 return( MBEDTLS_SSL_HASH_SHA512 );
4941#endif
4942 default:
4943 return( MBEDTLS_SSL_HASH_NONE );
4944 }
4945}
4946
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004947/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004948 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004949 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004950 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004951int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004952{
Brett Warrene0edc842021-08-17 09:53:13 +01004953 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004954
Brett Warrene0edc842021-08-17 09:53:13 +01004955 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004956 return( -1 );
4957
Brett Warrene0edc842021-08-17 09:53:13 +01004958 for( ; *group_list != 0; group_list++ )
4959 {
4960 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004961 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004962 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004963
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004964 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004965}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004966
4967#if defined(MBEDTLS_ECP_C)
4968/*
4969 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4970 */
4971int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4972{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004973 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004974 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004975
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004976 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004977 return -1;
4978
4979 uint16_t tls_id = grp_info->tls_id;
4980
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004981 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4982}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004983#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#if defined(MBEDTLS_X509_CRT_PARSE_C)
4986int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4987 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004988 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004989 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004991 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004992 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004993 const char *ext_oid;
4994 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004997 {
4998 /* Server part of the key exchange */
4999 switch( ciphersuite->key_exchange )
5000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 case MBEDTLS_KEY_EXCHANGE_RSA:
5002 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005003 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005004 break;
5005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5007 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5008 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5009 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005010 break;
5011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5013 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005014 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005015 break;
5016
5017 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 case MBEDTLS_KEY_EXCHANGE_NONE:
5019 case MBEDTLS_KEY_EXCHANGE_PSK:
5020 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5021 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005022 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005023 usage = 0;
5024 }
5025 }
5026 else
5027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5029 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005030 }
5031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005033 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005034 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005035 ret = -1;
5036 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5041 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005042 }
5043 else
5044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5046 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005047 }
5048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005050 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005051 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005052 ret = -1;
5053 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005054
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005055 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005057#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005058
Jerry Yu148165c2021-09-24 23:20:59 +08005059#if defined(MBEDTLS_USE_PSA_CRYPTO)
5060int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5061 const mbedtls_md_type_t md,
5062 unsigned char *dst,
5063 size_t dst_len,
5064 size_t *olen )
5065{
Ronald Cronf6893e12022-01-07 22:09:01 +01005066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5067 psa_hash_operation_t *hash_operation_to_clone;
5068 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5069
Jerry Yu148165c2021-09-24 23:20:59 +08005070 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005071
5072 switch( md )
5073 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005074#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005075 case MBEDTLS_MD_SHA384:
5076 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5077 break;
5078#endif
5079
Andrzej Kurek25f27152022-08-17 16:09:31 -04005080#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005081 case MBEDTLS_MD_SHA256:
5082 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5083 break;
5084#endif
5085
5086 default:
5087 goto exit;
5088 }
5089
5090 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5091 if( status != PSA_SUCCESS )
5092 goto exit;
5093
5094 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5095 if( status != PSA_SUCCESS )
5096 goto exit;
5097
5098exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005099 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005100}
5101#else /* MBEDTLS_USE_PSA_CRYPTO */
5102
Andrzej Kurek25f27152022-08-17 16:09:31 -04005103#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005104MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005105static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5106 unsigned char *dst,
5107 size_t dst_len,
5108 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005109{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005110 int ret;
5111 mbedtls_sha512_context sha512;
5112
5113 if( dst_len < 48 )
5114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5115
5116 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005117 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005118
5119 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5120 {
5121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5122 goto exit;
5123 }
5124
5125 *olen = 48;
5126
5127exit:
5128
5129 mbedtls_sha512_free( &sha512 );
5130 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005131}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005132#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005133
Andrzej Kurek25f27152022-08-17 16:09:31 -04005134#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005135MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005136static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5137 unsigned char *dst,
5138 size_t dst_len,
5139 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005140{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005141 int ret;
5142 mbedtls_sha256_context sha256;
5143
5144 if( dst_len < 32 )
5145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5146
5147 mbedtls_sha256_init( &sha256 );
5148 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005149
Jerry Yu24c0ec32021-09-09 14:21:07 +08005150 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5151 {
5152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5153 goto exit;
5154 }
5155
5156 *olen = 32;
5157
5158exit:
5159
5160 mbedtls_sha256_free( &sha256 );
5161 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005162}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005163#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005164
Jerry Yu000f9762021-09-14 11:12:51 +08005165int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5166 const mbedtls_md_type_t md,
5167 unsigned char *dst,
5168 size_t dst_len,
5169 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005170{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005171 switch( md )
5172 {
5173
Andrzej Kurek25f27152022-08-17 16:09:31 -04005174#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005175 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005176 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005177#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005178
Andrzej Kurek25f27152022-08-17 16:09:31 -04005179#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005180 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005181 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005182#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005183
5184 default:
5185 break;
5186 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005188}
XiaokangQian647719a2021-12-07 09:16:29 +00005189
Jerry Yu148165c2021-09-24 23:20:59 +08005190#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005191
Gabor Mezei078e8032022-04-27 21:17:56 +02005192#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5193/* mbedtls_ssl_parse_sig_alg_ext()
5194 *
5195 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5196 * value (TLS 1.3 RFC8446):
5197 * enum {
5198 * ....
5199 * ecdsa_secp256r1_sha256( 0x0403 ),
5200 * ecdsa_secp384r1_sha384( 0x0503 ),
5201 * ecdsa_secp521r1_sha512( 0x0603 ),
5202 * ....
5203 * } SignatureScheme;
5204 *
5205 * struct {
5206 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5207 * } SignatureSchemeList;
5208 *
5209 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5210 * value (TLS 1.2 RFC5246):
5211 * enum {
5212 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5213 * sha512(6), (255)
5214 * } HashAlgorithm;
5215 *
5216 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5217 * SignatureAlgorithm;
5218 *
5219 * struct {
5220 * HashAlgorithm hash;
5221 * SignatureAlgorithm signature;
5222 * } SignatureAndHashAlgorithm;
5223 *
5224 * SignatureAndHashAlgorithm
5225 * supported_signature_algorithms<2..2^16-2>;
5226 *
5227 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5228 * generalization of the TLS 1.2 signature algorithm extension.
5229 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5230 * `SignatureScheme` field of TLS 1.3
5231 *
5232 */
5233int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5234 const unsigned char *buf,
5235 const unsigned char *end )
5236{
5237 const unsigned char *p = buf;
5238 size_t supported_sig_algs_len = 0;
5239 const unsigned char *supported_sig_algs_end;
5240 uint16_t sig_alg;
5241 uint32_t common_idx = 0;
5242
5243 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5244 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5245 p += 2;
5246
5247 memset( ssl->handshake->received_sig_algs, 0,
5248 sizeof(ssl->handshake->received_sig_algs) );
5249
5250 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5251 supported_sig_algs_end = p + supported_sig_algs_len;
5252 while( p < supported_sig_algs_end )
5253 {
5254 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5255 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5256 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005257 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5258 sig_alg,
5259 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005260#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005261 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005262 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5263 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5264 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005265 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005266 }
5267#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005268
Jerry Yuf3b46b52022-06-19 16:52:27 +08005269 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5270 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005271
5272 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5273 {
5274 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5275 common_idx += 1;
5276 }
5277 }
5278 /* Check that we consumed all the message. */
5279 if( p != end )
5280 {
5281 MBEDTLS_SSL_DEBUG_MSG( 1,
5282 ( "Signature algorithms extension length misaligned" ) );
5283 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5284 MBEDTLS_ERR_SSL_DECODE_ERROR );
5285 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5286 }
5287
5288 if( common_idx == 0 )
5289 {
5290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5291 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5292 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5293 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5294 }
5295
Gabor Mezei15b95a62022-05-09 16:37:58 +02005296 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005297 return( 0 );
5298}
5299
5300#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5301
Jerry Yudc7bd172022-02-17 13:44:15 +08005302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5303
5304#if defined(MBEDTLS_USE_PSA_CRYPTO)
5305
5306static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5307 mbedtls_svc_key_id_t key,
5308 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005309 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005310 const unsigned char* seed, size_t seed_length,
5311 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005312 const unsigned char* other_secret,
5313 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005314 size_t capacity )
5315{
5316 psa_status_t status;
5317
5318 status = psa_key_derivation_setup( derivation, alg );
5319 if( status != PSA_SUCCESS )
5320 return( status );
5321
5322 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5323 {
5324 status = psa_key_derivation_input_bytes( derivation,
5325 PSA_KEY_DERIVATION_INPUT_SEED,
5326 seed, seed_length );
5327 if( status != PSA_SUCCESS )
5328 return( status );
5329
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005330 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005331 {
5332 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005333 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5334 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005335 if( status != PSA_SUCCESS )
5336 return( status );
5337 }
5338
Jerry Yudc7bd172022-02-17 13:44:15 +08005339 if( mbedtls_svc_key_id_is_null( key ) )
5340 {
5341 status = psa_key_derivation_input_bytes(
5342 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005343 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005344 }
5345 else
5346 {
5347 status = psa_key_derivation_input_key(
5348 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5349 }
5350 if( status != PSA_SUCCESS )
5351 return( status );
5352
5353 status = psa_key_derivation_input_bytes( derivation,
5354 PSA_KEY_DERIVATION_INPUT_LABEL,
5355 label, label_length );
5356 if( status != PSA_SUCCESS )
5357 return( status );
5358 }
5359 else
5360 {
5361 return( PSA_ERROR_NOT_SUPPORTED );
5362 }
5363
5364 status = psa_key_derivation_set_capacity( derivation, capacity );
5365 if( status != PSA_SUCCESS )
5366 return( status );
5367
5368 return( PSA_SUCCESS );
5369}
5370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005371MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005372static int tls_prf_generic( mbedtls_md_type_t md_type,
5373 const unsigned char *secret, size_t slen,
5374 const char *label,
5375 const unsigned char *random, size_t rlen,
5376 unsigned char *dstbuf, size_t dlen )
5377{
5378 psa_status_t status;
5379 psa_algorithm_t alg;
5380 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5381 psa_key_derivation_operation_t derivation =
5382 PSA_KEY_DERIVATION_OPERATION_INIT;
5383
5384 if( md_type == MBEDTLS_MD_SHA384 )
5385 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5386 else
5387 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5388
5389 /* Normally a "secret" should be long enough to be impossible to
5390 * find by brute force, and in particular should not be empty. But
5391 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5392 * and for this use case it makes sense to have a 0-length "secret".
5393 * Since the key API doesn't allow importing a key of length 0,
5394 * keep master_key=0, which setup_psa_key_derivation() understands
5395 * to mean a 0-length "secret" input. */
5396 if( slen != 0 )
5397 {
5398 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5399 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5400 psa_set_key_algorithm( &key_attributes, alg );
5401 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5402
5403 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5404 if( status != PSA_SUCCESS )
5405 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5406 }
5407
5408 status = setup_psa_key_derivation( &derivation,
5409 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005410 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005411 random, rlen,
5412 (unsigned char const *) label,
5413 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005414 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005415 dlen );
5416 if( status != PSA_SUCCESS )
5417 {
5418 psa_key_derivation_abort( &derivation );
5419 psa_destroy_key( master_key );
5420 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5421 }
5422
5423 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5424 if( status != PSA_SUCCESS )
5425 {
5426 psa_key_derivation_abort( &derivation );
5427 psa_destroy_key( master_key );
5428 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5429 }
5430
5431 status = psa_key_derivation_abort( &derivation );
5432 if( status != PSA_SUCCESS )
5433 {
5434 psa_destroy_key( master_key );
5435 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5436 }
5437
5438 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5439 status = psa_destroy_key( master_key );
5440 if( status != PSA_SUCCESS )
5441 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5442
5443 return( 0 );
5444}
5445
5446#else /* MBEDTLS_USE_PSA_CRYPTO */
5447
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005448MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005449static int tls_prf_generic( mbedtls_md_type_t md_type,
5450 const unsigned char *secret, size_t slen,
5451 const char *label,
5452 const unsigned char *random, size_t rlen,
5453 unsigned char *dstbuf, size_t dlen )
5454{
5455 size_t nb;
5456 size_t i, j, k, md_len;
5457 unsigned char *tmp;
5458 size_t tmp_len = 0;
5459 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5460 const mbedtls_md_info_t *md_info;
5461 mbedtls_md_context_t md_ctx;
5462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5463
5464 mbedtls_md_init( &md_ctx );
5465
5466 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5468
5469 md_len = mbedtls_md_get_size( md_info );
5470
5471 tmp_len = md_len + strlen( label ) + rlen;
5472 tmp = mbedtls_calloc( 1, tmp_len );
5473 if( tmp == NULL )
5474 {
5475 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5476 goto exit;
5477 }
5478
5479 nb = strlen( label );
5480 memcpy( tmp + md_len, label, nb );
5481 memcpy( tmp + md_len + nb, random, rlen );
5482 nb += rlen;
5483
5484 /*
5485 * Compute P_<hash>(secret, label + random)[0..dlen]
5486 */
5487 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5488 goto exit;
5489
5490 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5491 if( ret != 0 )
5492 goto exit;
5493 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5494 if( ret != 0 )
5495 goto exit;
5496 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5497 if( ret != 0 )
5498 goto exit;
5499
5500 for( i = 0; i < dlen; i += md_len )
5501 {
5502 ret = mbedtls_md_hmac_reset ( &md_ctx );
5503 if( ret != 0 )
5504 goto exit;
5505 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5506 if( ret != 0 )
5507 goto exit;
5508 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5509 if( ret != 0 )
5510 goto exit;
5511
5512 ret = mbedtls_md_hmac_reset ( &md_ctx );
5513 if( ret != 0 )
5514 goto exit;
5515 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5516 if( ret != 0 )
5517 goto exit;
5518 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5519 if( ret != 0 )
5520 goto exit;
5521
5522 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5523
5524 for( j = 0; j < k; j++ )
5525 dstbuf[i + j] = h_i[j];
5526 }
5527
5528exit:
5529 mbedtls_md_free( &md_ctx );
5530
5531 mbedtls_platform_zeroize( tmp, tmp_len );
5532 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5533
5534 mbedtls_free( tmp );
5535
5536 return( ret );
5537}
5538#endif /* MBEDTLS_USE_PSA_CRYPTO */
5539
Andrzej Kurek25f27152022-08-17 16:09:31 -04005540#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005541MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005542static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5543 const char *label,
5544 const unsigned char *random, size_t rlen,
5545 unsigned char *dstbuf, size_t dlen )
5546{
5547 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5548 label, random, rlen, dstbuf, dlen ) );
5549}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005550#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005551
Andrzej Kurek25f27152022-08-17 16:09:31 -04005552#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005553MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005554static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5555 const char *label,
5556 const unsigned char *random, size_t rlen,
5557 unsigned char *dstbuf, size_t dlen )
5558{
5559 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5560 label, random, rlen, dstbuf, dlen ) );
5561}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005562#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005563
Jerry Yuf009d862022-02-17 14:01:37 +08005564/*
5565 * Set appropriate PRF function and other SSL / TLS1.2 functions
5566 *
5567 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005568 * - hash associated with the ciphersuite (only used by TLS 1.2)
5569 *
5570 * Outputs:
5571 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5572 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005573MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005574static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005575 mbedtls_md_type_t hash )
5576{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005577#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005578 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005579 {
5580 handshake->tls_prf = tls_prf_sha384;
5581 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5582 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5583 }
5584 else
5585#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005587 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005588 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005589 handshake->tls_prf = tls_prf_sha256;
5590 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5591 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5592 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005593#else
Jerry Yuf009d862022-02-17 14:01:37 +08005594 {
Jerry Yuf009d862022-02-17 14:01:37 +08005595 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005596 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5598 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005599#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005600
5601 return( 0 );
5602}
Jerry Yud6ab2352022-02-17 14:03:43 +08005603
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005604/*
5605 * Compute master secret if needed
5606 *
5607 * Parameters:
5608 * [in/out] handshake
5609 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5610 * (PSA-PSK) ciphersuite_info, psk_opaque
5611 * [out] premaster (cleared)
5612 * [out] master
5613 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5614 * debug: conf->f_dbg, conf->p_dbg
5615 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005616 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005617 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005618MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005619static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5620 unsigned char *master,
5621 const mbedtls_ssl_context *ssl )
5622{
5623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5624
5625 /* cf. RFC 5246, Section 8.1:
5626 * "The master secret is always exactly 48 bytes in length." */
5627 size_t const master_secret_len = 48;
5628
5629#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5630 unsigned char session_hash[48];
5631#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5632
5633 /* The label for the KDF used for key expansion.
5634 * This is either "master secret" or "extended master secret"
5635 * depending on whether the Extended Master Secret extension
5636 * is used. */
5637 char const *lbl = "master secret";
5638
Przemek Stekielae4ed302022-04-05 17:15:55 +02005639 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005640 * - If the Extended Master Secret extension is not used,
5641 * this is ClientHello.Random + ServerHello.Random
5642 * (see Sect. 8.1 in RFC 5246).
5643 * - If the Extended Master Secret extension is used,
5644 * this is the transcript of the handshake so far.
5645 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005646 unsigned char const *seed = handshake->randbytes;
5647 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005648
5649#if !defined(MBEDTLS_DEBUG_C) && \
5650 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5651 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5652 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5653 ssl = NULL; /* make sure we don't use it except for those cases */
5654 (void) ssl;
5655#endif
5656
5657 if( handshake->resume != 0 )
5658 {
5659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5660 return( 0 );
5661 }
5662
5663#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5664 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5665 {
5666 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005667 seed = session_hash;
5668 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005669
5670 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005671 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005672 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005673#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005674
Przemek Stekiel99114f32022-04-22 11:20:09 +02005675#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005676 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005677 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005678 {
5679 /* Perform PSK-to-MS expansion in a single step. */
5680 psa_status_t status;
5681 psa_algorithm_t alg;
5682 mbedtls_svc_key_id_t psk;
5683 psa_key_derivation_operation_t derivation =
5684 PSA_KEY_DERIVATION_OPERATION_INIT;
5685 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5686
5687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5688
5689 psk = mbedtls_ssl_get_opaque_psk( ssl );
5690
5691 if( hash_alg == MBEDTLS_MD_SHA384 )
5692 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5693 else
5694 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5695
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005696 size_t other_secret_len = 0;
5697 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005698
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005699 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005700 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005701 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005702 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005703 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005704 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005705 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005706 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005707 other_secret_len = 48;
5708 other_secret = handshake->premaster + 2;
5709 break;
5710 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005711 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005712 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5713 other_secret = handshake->premaster + 2;
5714 break;
5715 default:
5716 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005717 }
5718
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005719 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005720 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005721 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005722 (unsigned char const *) lbl,
5723 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005724 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005725 master_secret_len );
5726 if( status != PSA_SUCCESS )
5727 {
5728 psa_key_derivation_abort( &derivation );
5729 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5730 }
5731
5732 status = psa_key_derivation_output_bytes( &derivation,
5733 master,
5734 master_secret_len );
5735 if( status != PSA_SUCCESS )
5736 {
5737 psa_key_derivation_abort( &derivation );
5738 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5739 }
5740
5741 status = psa_key_derivation_abort( &derivation );
5742 if( status != PSA_SUCCESS )
5743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5744 }
5745 else
5746#endif
5747 {
5748 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005749 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005750 master,
5751 master_secret_len );
5752 if( ret != 0 )
5753 {
5754 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5755 return( ret );
5756 }
5757
5758 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5759 handshake->premaster,
5760 handshake->pmslen );
5761
5762 mbedtls_platform_zeroize( handshake->premaster,
5763 sizeof(handshake->premaster) );
5764 }
5765
5766 return( 0 );
5767}
5768
Jerry Yud62f87e2022-02-17 14:09:02 +08005769int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5770{
5771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5772 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5773 ssl->handshake->ciphersuite_info;
5774
5775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5776
5777 /* Set PRF, calc_verify and calc_finished function pointers */
5778 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005779 ciphersuite_info->mac );
5780 if( ret != 0 )
5781 {
5782 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5783 return( ret );
5784 }
5785
5786 /* Compute master secret if needed */
5787 ret = ssl_compute_master( ssl->handshake,
5788 ssl->session_negotiate->master,
5789 ssl );
5790 if( ret != 0 )
5791 {
5792 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5793 return( ret );
5794 }
5795
5796 /* Swap the client and server random values:
5797 * - MS derivation wanted client+server (RFC 5246 8.1)
5798 * - key derivation wants server+client (RFC 5246 6.3) */
5799 {
5800 unsigned char tmp[64];
5801 memcpy( tmp, ssl->handshake->randbytes, 64 );
5802 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5803 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5804 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5805 }
5806
5807 /* Populate transform structure */
5808 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5809 ssl->session_negotiate->ciphersuite,
5810 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005811#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005812 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005813#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005814 ssl->handshake->tls_prf,
5815 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005816 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005817 ssl->conf->endpoint,
5818 ssl );
5819 if( ret != 0 )
5820 {
5821 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5822 return( ret );
5823 }
5824
5825 /* We no longer need Server/ClientHello.random values */
5826 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5827 sizeof( ssl->handshake->randbytes ) );
5828
5829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5830
5831 return( 0 );
5832}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005833
Ronald Cron4dcbca92022-03-07 10:21:40 +01005834int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5835{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005836 switch( md )
5837 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005838#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005839 case MBEDTLS_SSL_HASH_SHA384:
5840 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5841 break;
5842#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005843#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005844 case MBEDTLS_SSL_HASH_SHA256:
5845 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5846 break;
5847#endif
5848 default:
5849 return( -1 );
5850 }
5851
Ronald Cronc2f13a02022-03-07 10:25:24 +01005852 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005853}
5854
Andrzej Kurek25f27152022-08-17 16:09:31 -04005855#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005856void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5857 unsigned char *hash,
5858 size_t *hlen )
5859{
5860#if defined(MBEDTLS_USE_PSA_CRYPTO)
5861 size_t hash_size;
5862 psa_status_t status;
5863 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5864
5865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5866 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5867 if( status != PSA_SUCCESS )
5868 {
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5870 return;
5871 }
5872
5873 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5874 if( status != PSA_SUCCESS )
5875 {
5876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5877 return;
5878 }
5879
5880 *hlen = 32;
5881 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5883#else
5884 mbedtls_sha256_context sha256;
5885
5886 mbedtls_sha256_init( &sha256 );
5887
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5889
5890 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5891 mbedtls_sha256_finish( &sha256, hash );
5892
5893 *hlen = 32;
5894
5895 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5897
5898 mbedtls_sha256_free( &sha256 );
5899#endif /* MBEDTLS_USE_PSA_CRYPTO */
5900 return;
5901}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005902#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005903
Andrzej Kurek25f27152022-08-17 16:09:31 -04005904#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005905void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5906 unsigned char *hash,
5907 size_t *hlen )
5908{
5909#if defined(MBEDTLS_USE_PSA_CRYPTO)
5910 size_t hash_size;
5911 psa_status_t status;
5912 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5913
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5915 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5916 if( status != PSA_SUCCESS )
5917 {
5918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5919 return;
5920 }
5921
5922 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5923 if( status != PSA_SUCCESS )
5924 {
5925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5926 return;
5927 }
5928
5929 *hlen = 48;
5930 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5932#else
5933 mbedtls_sha512_context sha512;
5934
5935 mbedtls_sha512_init( &sha512 );
5936
5937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5938
Andrzej Kureka242e832022-08-11 10:03:14 -04005939 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005940 mbedtls_sha512_finish( &sha512, hash );
5941
5942 *hlen = 48;
5943
5944 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5946
5947 mbedtls_sha512_free( &sha512 );
5948#endif /* MBEDTLS_USE_PSA_CRYPTO */
5949 return;
5950}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005951#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005952
Neil Armstrong80f6f322022-05-03 17:56:38 +02005953#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5954 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005955int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5956{
5957 unsigned char *p = ssl->handshake->premaster;
5958 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5959 const unsigned char *psk = NULL;
5960 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005961 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005962
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005963 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005964 {
5965 /*
5966 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005967 * checked before calling this function.
5968 *
5969 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005970 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005971 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005972 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5976 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005977 }
5978
5979 /*
5980 * PMS = struct {
5981 * opaque other_secret<0..2^16-1>;
5982 * opaque psk<0..2^16-1>;
5983 * };
5984 * with "other_secret" depending on the particular key exchange
5985 */
5986#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5987 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5988 {
5989 if( end - p < 2 )
5990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5991
5992 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5993 p += 2;
5994
5995 if( end < p || (size_t)( end - p ) < psk_len )
5996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5997
5998 memset( p, 0, psk_len );
5999 p += psk_len;
6000 }
6001 else
6002#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6003#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6004 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6005 {
6006 /*
6007 * other_secret already set by the ClientKeyExchange message,
6008 * and is 48 bytes long
6009 */
6010 if( end - p < 2 )
6011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6012
6013 *p++ = 0;
6014 *p++ = 48;
6015 p += 48;
6016 }
6017 else
6018#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6019#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6020 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6021 {
6022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6023 size_t len;
6024
6025 /* Write length only when we know the actual value */
6026 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6027 p + 2, end - ( p + 2 ), &len,
6028 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6029 {
6030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6031 return( ret );
6032 }
6033 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6034 p += 2 + len;
6035
6036 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6037 }
6038 else
6039#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006040#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006041 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6042 {
6043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6044 size_t zlen;
6045
6046 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6047 p + 2, end - ( p + 2 ),
6048 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6049 {
6050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6051 return( ret );
6052 }
6053
6054 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6055 p += 2 + zlen;
6056
6057 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6058 MBEDTLS_DEBUG_ECDH_Z );
6059 }
6060 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006061#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006062 {
6063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6065 }
6066
6067 /* opaque psk<0..2^16-1>; */
6068 if( end - p < 2 )
6069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6070
6071 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6072 p += 2;
6073
6074 if( end < p || (size_t)( end - p ) < psk_len )
6075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6076
6077 memcpy( p, psk, psk_len );
6078 p += psk_len;
6079
6080 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6081
6082 return( 0 );
6083}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006084#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006085
6086#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006087MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006088static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6089
6090#if defined(MBEDTLS_SSL_PROTO_DTLS)
6091int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6092{
6093 /* If renegotiation is not enforced, retransmit until we would reach max
6094 * timeout if we were using the usual handshake doubling scheme */
6095 if( ssl->conf->renego_max_records < 0 )
6096 {
6097 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6098 unsigned char doublings = 1;
6099
6100 while( ratio != 0 )
6101 {
6102 ++doublings;
6103 ratio >>= 1;
6104 }
6105
6106 if( ++ssl->renego_records_seen > doublings )
6107 {
6108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6109 return( 0 );
6110 }
6111 }
6112
6113 return( ssl_write_hello_request( ssl ) );
6114}
6115#endif
6116#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006117
Jerry Yud9526692022-02-17 14:23:47 +08006118/*
6119 * Handshake functions
6120 */
6121#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6122/* No certificate support -> dummy functions */
6123int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6124{
6125 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6126 ssl->handshake->ciphersuite_info;
6127
6128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6129
6130 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6131 {
6132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6133 ssl->state++;
6134 return( 0 );
6135 }
6136
6137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6139}
6140
6141int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6142{
6143 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6144 ssl->handshake->ciphersuite_info;
6145
6146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6147
6148 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6149 {
6150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6151 ssl->state++;
6152 return( 0 );
6153 }
6154
6155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6157}
6158
6159#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6160/* Some certificate support -> implement write and parse */
6161
6162int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6163{
6164 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6165 size_t i, n;
6166 const mbedtls_x509_crt *crt;
6167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6168 ssl->handshake->ciphersuite_info;
6169
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6171
6172 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6173 {
6174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6175 ssl->state++;
6176 return( 0 );
6177 }
6178
6179#if defined(MBEDTLS_SSL_CLI_C)
6180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6181 {
6182 if( ssl->handshake->client_auth == 0 )
6183 {
6184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6185 ssl->state++;
6186 return( 0 );
6187 }
6188 }
6189#endif /* MBEDTLS_SSL_CLI_C */
6190#if defined(MBEDTLS_SSL_SRV_C)
6191 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6192 {
6193 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6194 {
6195 /* Should never happen because we shouldn't have picked the
6196 * ciphersuite if we don't have a certificate. */
6197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6198 }
6199 }
6200#endif
6201
6202 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6203
6204 /*
6205 * 0 . 0 handshake type
6206 * 1 . 3 handshake length
6207 * 4 . 6 length of all certs
6208 * 7 . 9 length of cert. 1
6209 * 10 . n-1 peer certificate
6210 * n . n+2 length of cert. 2
6211 * n+3 . ... upper level cert, etc.
6212 */
6213 i = 7;
6214 crt = mbedtls_ssl_own_cert( ssl );
6215
6216 while( crt != NULL )
6217 {
6218 n = crt->raw.len;
6219 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6220 {
6221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6222 " > %" MBEDTLS_PRINTF_SIZET,
6223 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6224 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6225 }
6226
6227 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6228 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6229 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6230
6231 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6232 i += n; crt = crt->next;
6233 }
6234
6235 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6236 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6237 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6238
6239 ssl->out_msglen = i;
6240 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6241 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6242
6243 ssl->state++;
6244
6245 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6246 {
6247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6248 return( ret );
6249 }
6250
6251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6252
6253 return( ret );
6254}
6255
6256#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6257
6258#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006259MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006260static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6261 unsigned char *crt_buf,
6262 size_t crt_buf_len )
6263{
6264 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6265
6266 if( peer_crt == NULL )
6267 return( -1 );
6268
6269 if( peer_crt->raw.len != crt_buf_len )
6270 return( -1 );
6271
6272 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6273}
6274#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006275MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006276static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6277 unsigned char *crt_buf,
6278 size_t crt_buf_len )
6279{
6280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6281 unsigned char const * const peer_cert_digest =
6282 ssl->session->peer_cert_digest;
6283 mbedtls_md_type_t const peer_cert_digest_type =
6284 ssl->session->peer_cert_digest_type;
6285 mbedtls_md_info_t const * const digest_info =
6286 mbedtls_md_info_from_type( peer_cert_digest_type );
6287 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6288 size_t digest_len;
6289
6290 if( peer_cert_digest == NULL || digest_info == NULL )
6291 return( -1 );
6292
6293 digest_len = mbedtls_md_get_size( digest_info );
6294 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6295 return( -1 );
6296
6297 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6298 if( ret != 0 )
6299 return( -1 );
6300
6301 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6302}
6303#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6304#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6305
6306/*
6307 * Once the certificate message is read, parse it into a cert chain and
6308 * perform basic checks, but leave actual verification to the caller
6309 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006310MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006311static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6312 mbedtls_x509_crt *chain )
6313{
6314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6315#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6316 int crt_cnt=0;
6317#endif
6318 size_t i, n;
6319 uint8_t alert;
6320
6321 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6322 {
6323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6325 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6326 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6327 }
6328
6329 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6330 {
6331 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6332 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6333 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6334 }
6335
6336 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6337 {
6338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6341 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6342 }
6343
6344 i = mbedtls_ssl_hs_hdr_len( ssl );
6345
6346 /*
6347 * Same message structure as in mbedtls_ssl_write_certificate()
6348 */
6349 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6350
6351 if( ssl->in_msg[i] != 0 ||
6352 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6353 {
6354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6355 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6356 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6357 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6358 }
6359
6360 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6361 i += 3;
6362
6363 /* Iterate through and parse the CRTs in the provided chain. */
6364 while( i < ssl->in_hslen )
6365 {
6366 /* Check that there's room for the next CRT's length fields. */
6367 if ( i + 3 > ssl->in_hslen ) {
6368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6369 mbedtls_ssl_send_alert_message( ssl,
6370 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6371 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6372 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6373 }
6374 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6375 * anything beyond 2**16 ~ 64K. */
6376 if( ssl->in_msg[i] != 0 )
6377 {
6378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6379 mbedtls_ssl_send_alert_message( ssl,
6380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6381 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6382 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6383 }
6384
6385 /* Read length of the next CRT in the chain. */
6386 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6387 | (unsigned int) ssl->in_msg[i + 2];
6388 i += 3;
6389
6390 if( n < 128 || i + n > ssl->in_hslen )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6393 mbedtls_ssl_send_alert_message( ssl,
6394 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6395 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6396 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6397 }
6398
6399 /* Check if we're handling the first CRT in the chain. */
6400#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6401 if( crt_cnt++ == 0 &&
6402 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6403 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6404 {
6405 /* During client-side renegotiation, check that the server's
6406 * end-CRTs hasn't changed compared to the initial handshake,
6407 * mitigating the triple handshake attack. On success, reuse
6408 * the original end-CRT instead of parsing it again. */
6409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6410 if( ssl_check_peer_crt_unchanged( ssl,
6411 &ssl->in_msg[i],
6412 n ) != 0 )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6415 mbedtls_ssl_send_alert_message( ssl,
6416 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6417 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6418 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6419 }
6420
6421 /* Now we can safely free the original chain. */
6422 ssl_clear_peer_cert( ssl->session );
6423 }
6424#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6425
6426 /* Parse the next certificate in the chain. */
6427#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6428 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6429#else
6430 /* If we don't need to store the CRT chain permanently, parse
6431 * it in-place from the input buffer instead of making a copy. */
6432 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6433#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6434 switch( ret )
6435 {
6436 case 0: /*ok*/
6437 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6438 /* Ignore certificate with an unknown algorithm: maybe a
6439 prior certificate was already trusted. */
6440 break;
6441
6442 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6443 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6444 goto crt_parse_der_failed;
6445
6446 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6447 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6448 goto crt_parse_der_failed;
6449
6450 default:
6451 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6452 crt_parse_der_failed:
6453 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6454 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6455 return( ret );
6456 }
6457
6458 i += n;
6459 }
6460
6461 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6462 return( 0 );
6463}
6464
6465#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006466MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006467static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6468{
6469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6470 return( -1 );
6471
6472 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6473 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6474 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6475 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6476 {
Ronald Cron19385882022-06-15 16:26:13 +02006477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006478 return( 0 );
6479 }
6480 return( -1 );
6481}
6482#endif /* MBEDTLS_SSL_SRV_C */
6483
6484/* Check if a certificate message is expected.
6485 * Return either
6486 * - SSL_CERTIFICATE_EXPECTED, or
6487 * - SSL_CERTIFICATE_SKIP
6488 * indicating whether a Certificate message is expected or not.
6489 */
6490#define SSL_CERTIFICATE_EXPECTED 0
6491#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006492MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006493static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6494 int authmode )
6495{
6496 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6497 ssl->handshake->ciphersuite_info;
6498
6499 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6500 return( SSL_CERTIFICATE_SKIP );
6501
6502#if defined(MBEDTLS_SSL_SRV_C)
6503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6504 {
6505 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6506 return( SSL_CERTIFICATE_SKIP );
6507
6508 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6509 {
6510 ssl->session_negotiate->verify_result =
6511 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6512 return( SSL_CERTIFICATE_SKIP );
6513 }
6514 }
6515#else
6516 ((void) authmode);
6517#endif /* MBEDTLS_SSL_SRV_C */
6518
6519 return( SSL_CERTIFICATE_EXPECTED );
6520}
6521
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006522MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006523static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6524 int authmode,
6525 mbedtls_x509_crt *chain,
6526 void *rs_ctx )
6527{
6528 int ret = 0;
6529 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6530 ssl->handshake->ciphersuite_info;
6531 int have_ca_chain = 0;
6532
6533 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6534 void *p_vrfy;
6535
6536 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6537 return( 0 );
6538
6539 if( ssl->f_vrfy != NULL )
6540 {
6541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6542 f_vrfy = ssl->f_vrfy;
6543 p_vrfy = ssl->p_vrfy;
6544 }
6545 else
6546 {
6547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6548 f_vrfy = ssl->conf->f_vrfy;
6549 p_vrfy = ssl->conf->p_vrfy;
6550 }
6551
6552 /*
6553 * Main check: verify certificate
6554 */
6555#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6556 if( ssl->conf->f_ca_cb != NULL )
6557 {
6558 ((void) rs_ctx);
6559 have_ca_chain = 1;
6560
6561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6562 ret = mbedtls_x509_crt_verify_with_ca_cb(
6563 chain,
6564 ssl->conf->f_ca_cb,
6565 ssl->conf->p_ca_cb,
6566 ssl->conf->cert_profile,
6567 ssl->hostname,
6568 &ssl->session_negotiate->verify_result,
6569 f_vrfy, p_vrfy );
6570 }
6571 else
6572#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6573 {
6574 mbedtls_x509_crt *ca_chain;
6575 mbedtls_x509_crl *ca_crl;
6576
6577#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6578 if( ssl->handshake->sni_ca_chain != NULL )
6579 {
6580 ca_chain = ssl->handshake->sni_ca_chain;
6581 ca_crl = ssl->handshake->sni_ca_crl;
6582 }
6583 else
6584#endif
6585 {
6586 ca_chain = ssl->conf->ca_chain;
6587 ca_crl = ssl->conf->ca_crl;
6588 }
6589
6590 if( ca_chain != NULL )
6591 have_ca_chain = 1;
6592
6593 ret = mbedtls_x509_crt_verify_restartable(
6594 chain,
6595 ca_chain, ca_crl,
6596 ssl->conf->cert_profile,
6597 ssl->hostname,
6598 &ssl->session_negotiate->verify_result,
6599 f_vrfy, p_vrfy, rs_ctx );
6600 }
6601
6602 if( ret != 0 )
6603 {
6604 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6605 }
6606
6607#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6608 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6609 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6610#endif
6611
6612 /*
6613 * Secondary checks: always done, but change 'ret' only if it was 0
6614 */
6615
6616#if defined(MBEDTLS_ECP_C)
6617 {
6618 const mbedtls_pk_context *pk = &chain->pk;
6619
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006620 /* If certificate uses an EC key, make sure the curve is OK.
6621 * This is a public key, so it can't be opaque, so can_do() is a good
6622 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006623 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006624 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006625 /* and in the unlikely case the above assumption no longer holds
6626 * we are making sure that pk_ec() here does not return a NULL
6627 */
6628 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6629 if( ec == NULL )
6630 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6633 }
Jerry Yud9526692022-02-17 14:23:47 +08006634
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006635 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6636 {
6637 ssl->session_negotiate->verify_result |=
6638 MBEDTLS_X509_BADCERT_BAD_KEY;
6639
6640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6641 if( ret == 0 )
6642 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6643 }
Jerry Yud9526692022-02-17 14:23:47 +08006644 }
6645 }
6646#endif /* MBEDTLS_ECP_C */
6647
6648 if( mbedtls_ssl_check_cert_usage( chain,
6649 ciphersuite_info,
6650 ! ssl->conf->endpoint,
6651 &ssl->session_negotiate->verify_result ) != 0 )
6652 {
6653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6654 if( ret == 0 )
6655 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6656 }
6657
6658 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6659 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6660 * with details encoded in the verification flags. All other kinds
6661 * of error codes, including those from the user provided f_vrfy
6662 * functions, are treated as fatal and lead to a failure of
6663 * ssl_parse_certificate even if verification was optional. */
6664 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6665 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6666 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6667 {
6668 ret = 0;
6669 }
6670
6671 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6674 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6675 }
6676
6677 if( ret != 0 )
6678 {
6679 uint8_t alert;
6680
6681 /* The certificate may have been rejected for several reasons.
6682 Pick one and send the corresponding alert. Which alert to send
6683 may be a subject of debate in some cases. */
6684 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6685 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6687 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6689 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6690 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6691 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6692 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6693 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6694 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6695 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6696 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6697 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6698 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6699 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6700 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6701 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6702 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6703 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6704 else
6705 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6706 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6707 alert );
6708 }
6709
6710#if defined(MBEDTLS_DEBUG_C)
6711 if( ssl->session_negotiate->verify_result != 0 )
6712 {
6713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6714 (unsigned int) ssl->session_negotiate->verify_result ) );
6715 }
6716 else
6717 {
6718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6719 }
6720#endif /* MBEDTLS_DEBUG_C */
6721
6722 return( ret );
6723}
6724
6725#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006726MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006727static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6728 unsigned char *start, size_t len )
6729{
6730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6731 /* Remember digest of the peer's end-CRT. */
6732 ssl->session_negotiate->peer_cert_digest =
6733 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6734 if( ssl->session_negotiate->peer_cert_digest == NULL )
6735 {
6736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6737 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6738 mbedtls_ssl_send_alert_message( ssl,
6739 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6740 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6741
6742 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6743 }
6744
6745 ret = mbedtls_md( mbedtls_md_info_from_type(
6746 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6747 start, len,
6748 ssl->session_negotiate->peer_cert_digest );
6749
6750 ssl->session_negotiate->peer_cert_digest_type =
6751 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6752 ssl->session_negotiate->peer_cert_digest_len =
6753 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6754
6755 return( ret );
6756}
6757
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006758MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006759static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6760 unsigned char *start, size_t len )
6761{
6762 unsigned char *end = start + len;
6763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6764
6765 /* Make a copy of the peer's raw public key. */
6766 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6767 ret = mbedtls_pk_parse_subpubkey( &start, end,
6768 &ssl->handshake->peer_pubkey );
6769 if( ret != 0 )
6770 {
6771 /* We should have parsed the public key before. */
6772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6773 }
6774
6775 return( 0 );
6776}
6777#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6778
6779int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6780{
6781 int ret = 0;
6782 int crt_expected;
6783#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6784 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6785 ? ssl->handshake->sni_authmode
6786 : ssl->conf->authmode;
6787#else
6788 const int authmode = ssl->conf->authmode;
6789#endif
6790 void *rs_ctx = NULL;
6791 mbedtls_x509_crt *chain = NULL;
6792
6793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6794
6795 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6796 if( crt_expected == SSL_CERTIFICATE_SKIP )
6797 {
6798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6799 goto exit;
6800 }
6801
6802#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6803 if( ssl->handshake->ecrs_enabled &&
6804 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6805 {
6806 chain = ssl->handshake->ecrs_peer_cert;
6807 ssl->handshake->ecrs_peer_cert = NULL;
6808 goto crt_verify;
6809 }
6810#endif
6811
6812 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6813 {
6814 /* mbedtls_ssl_read_record may have sent an alert already. We
6815 let it decide whether to alert. */
6816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6817 goto exit;
6818 }
6819
6820#if defined(MBEDTLS_SSL_SRV_C)
6821 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6822 {
6823 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6824
6825 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6826 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6827
6828 goto exit;
6829 }
6830#endif /* MBEDTLS_SSL_SRV_C */
6831
6832 /* Clear existing peer CRT structure in case we tried to
6833 * reuse a session but it failed, and allocate a new one. */
6834 ssl_clear_peer_cert( ssl->session_negotiate );
6835
6836 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6837 if( chain == NULL )
6838 {
6839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6840 sizeof( mbedtls_x509_crt ) ) );
6841 mbedtls_ssl_send_alert_message( ssl,
6842 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6843 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6844
6845 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6846 goto exit;
6847 }
6848 mbedtls_x509_crt_init( chain );
6849
6850 ret = ssl_parse_certificate_chain( ssl, chain );
6851 if( ret != 0 )
6852 goto exit;
6853
6854#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6855 if( ssl->handshake->ecrs_enabled)
6856 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6857
6858crt_verify:
6859 if( ssl->handshake->ecrs_enabled)
6860 rs_ctx = &ssl->handshake->ecrs_ctx;
6861#endif
6862
6863 ret = ssl_parse_certificate_verify( ssl, authmode,
6864 chain, rs_ctx );
6865 if( ret != 0 )
6866 goto exit;
6867
6868#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6869 {
6870 unsigned char *crt_start, *pk_start;
6871 size_t crt_len, pk_len;
6872
6873 /* We parse the CRT chain without copying, so
6874 * these pointers point into the input buffer,
6875 * and are hence still valid after freeing the
6876 * CRT chain. */
6877
6878 crt_start = chain->raw.p;
6879 crt_len = chain->raw.len;
6880
6881 pk_start = chain->pk_raw.p;
6882 pk_len = chain->pk_raw.len;
6883
6884 /* Free the CRT structures before computing
6885 * digest and copying the peer's public key. */
6886 mbedtls_x509_crt_free( chain );
6887 mbedtls_free( chain );
6888 chain = NULL;
6889
6890 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6891 if( ret != 0 )
6892 goto exit;
6893
6894 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6895 if( ret != 0 )
6896 goto exit;
6897 }
6898#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6899 /* Pass ownership to session structure. */
6900 ssl->session_negotiate->peer_cert = chain;
6901 chain = NULL;
6902#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6903
6904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6905
6906exit:
6907
6908 if( ret == 0 )
6909 ssl->state++;
6910
6911#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6912 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6913 {
6914 ssl->handshake->ecrs_peer_cert = chain;
6915 chain = NULL;
6916 }
6917#endif
6918
6919 if( chain != NULL )
6920 {
6921 mbedtls_x509_crt_free( chain );
6922 mbedtls_free( chain );
6923 }
6924
6925 return( ret );
6926}
6927#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6928
Andrzej Kurek25f27152022-08-17 16:09:31 -04006929#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006930static void ssl_calc_finished_tls_sha256(
6931 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6932{
6933 int len = 12;
6934 const char *sender;
6935 unsigned char padbuf[32];
6936#if defined(MBEDTLS_USE_PSA_CRYPTO)
6937 size_t hash_size;
6938 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6939 psa_status_t status;
6940#else
6941 mbedtls_sha256_context sha256;
6942#endif
6943
6944 mbedtls_ssl_session *session = ssl->session_negotiate;
6945 if( !session )
6946 session = ssl->session;
6947
6948 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6949 ? "client finished"
6950 : "server finished";
6951
6952#if defined(MBEDTLS_USE_PSA_CRYPTO)
6953 sha256_psa = psa_hash_operation_init();
6954
6955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6956
6957 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6958 if( status != PSA_SUCCESS )
6959 {
6960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6961 return;
6962 }
6963
6964 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6965 if( status != PSA_SUCCESS )
6966 {
6967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6968 return;
6969 }
6970 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6971#else
6972
6973 mbedtls_sha256_init( &sha256 );
6974
6975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6976
6977 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6978
6979 /*
6980 * TLSv1.2:
6981 * hash = PRF( master, finished_label,
6982 * Hash( handshake ) )[0.11]
6983 */
6984
6985#if !defined(MBEDTLS_SHA256_ALT)
6986 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6987 sha256.state, sizeof( sha256.state ) );
6988#endif
6989
6990 mbedtls_sha256_finish( &sha256, padbuf );
6991 mbedtls_sha256_free( &sha256 );
6992#endif /* MBEDTLS_USE_PSA_CRYPTO */
6993
6994 ssl->handshake->tls_prf( session->master, 48, sender,
6995 padbuf, 32, buf, len );
6996
6997 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6998
6999 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7000
7001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7002}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007003#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007004
Jerry Yub7ba49e2022-02-17 14:25:53 +08007005
Andrzej Kurek25f27152022-08-17 16:09:31 -04007006#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007007static void ssl_calc_finished_tls_sha384(
7008 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7009{
7010 int len = 12;
7011 const char *sender;
7012 unsigned char padbuf[48];
7013#if defined(MBEDTLS_USE_PSA_CRYPTO)
7014 size_t hash_size;
7015 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7016 psa_status_t status;
7017#else
7018 mbedtls_sha512_context sha512;
7019#endif
7020
7021 mbedtls_ssl_session *session = ssl->session_negotiate;
7022 if( !session )
7023 session = ssl->session;
7024
7025 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7026 ? "client finished"
7027 : "server finished";
7028
7029#if defined(MBEDTLS_USE_PSA_CRYPTO)
7030 sha384_psa = psa_hash_operation_init();
7031
7032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7033
7034 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7035 if( status != PSA_SUCCESS )
7036 {
7037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7038 return;
7039 }
7040
7041 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7042 if( status != PSA_SUCCESS )
7043 {
7044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7045 return;
7046 }
7047 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7048#else
7049 mbedtls_sha512_init( &sha512 );
7050
7051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7052
Andrzej Kureka242e832022-08-11 10:03:14 -04007053 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007054
7055 /*
7056 * TLSv1.2:
7057 * hash = PRF( master, finished_label,
7058 * Hash( handshake ) )[0.11]
7059 */
7060
7061#if !defined(MBEDTLS_SHA512_ALT)
7062 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7063 sha512.state, sizeof( sha512.state ) );
7064#endif
7065 mbedtls_sha512_finish( &sha512, padbuf );
7066
7067 mbedtls_sha512_free( &sha512 );
7068#endif
7069
7070 ssl->handshake->tls_prf( session->master, 48, sender,
7071 padbuf, 48, buf, len );
7072
7073 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7074
7075 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7076
7077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7078}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007079#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007080
Jerry Yuaef00152022-02-17 14:27:31 +08007081void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7082{
7083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7084
7085 /*
7086 * Free our handshake params
7087 */
7088 mbedtls_ssl_handshake_free( ssl );
7089 mbedtls_free( ssl->handshake );
7090 ssl->handshake = NULL;
7091
7092 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007093 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007094 */
7095 if( ssl->transform )
7096 {
7097 mbedtls_ssl_transform_free( ssl->transform );
7098 mbedtls_free( ssl->transform );
7099 }
7100 ssl->transform = ssl->transform_negotiate;
7101 ssl->transform_negotiate = NULL;
7102
7103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7104}
7105
Jerry Yu2a9fff52022-02-17 14:28:51 +08007106void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7107{
7108 int resume = ssl->handshake->resume;
7109
7110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7111
7112#if defined(MBEDTLS_SSL_RENEGOTIATION)
7113 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7114 {
7115 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7116 ssl->renego_records_seen = 0;
7117 }
7118#endif
7119
7120 /*
7121 * Free the previous session and switch in the current one
7122 */
7123 if( ssl->session )
7124 {
7125#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7126 /* RFC 7366 3.1: keep the EtM state */
7127 ssl->session_negotiate->encrypt_then_mac =
7128 ssl->session->encrypt_then_mac;
7129#endif
7130
7131 mbedtls_ssl_session_free( ssl->session );
7132 mbedtls_free( ssl->session );
7133 }
7134 ssl->session = ssl->session_negotiate;
7135 ssl->session_negotiate = NULL;
7136
7137 /*
7138 * Add cache entry
7139 */
7140 if( ssl->conf->f_set_cache != NULL &&
7141 ssl->session->id_len != 0 &&
7142 resume == 0 )
7143 {
7144 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7145 ssl->session->id,
7146 ssl->session->id_len,
7147 ssl->session ) != 0 )
7148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7149 }
7150
7151#if defined(MBEDTLS_SSL_PROTO_DTLS)
7152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7153 ssl->handshake->flight != NULL )
7154 {
7155 /* Cancel handshake timer */
7156 mbedtls_ssl_set_timer( ssl, 0 );
7157
7158 /* Keep last flight around in case we need to resend it:
7159 * we need the handshake and transform structures for that */
7160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7161 }
7162 else
7163#endif
7164 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7165
7166 ssl->state++;
7167
7168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7169}
7170
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007171int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7172{
7173 int ret, hash_len;
7174
7175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7176
7177 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7178
7179 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7180
7181 /*
7182 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7183 * may define some other value. Currently (early 2016), no defined
7184 * ciphersuite does this (and this is unlikely to change as activity has
7185 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7186 */
7187 hash_len = 12;
7188
7189#if defined(MBEDTLS_SSL_RENEGOTIATION)
7190 ssl->verify_data_len = hash_len;
7191 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7192#endif
7193
7194 ssl->out_msglen = 4 + hash_len;
7195 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7196 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7197
7198 /*
7199 * In case of session resuming, invert the client and server
7200 * ChangeCipherSpec messages order.
7201 */
7202 if( ssl->handshake->resume != 0 )
7203 {
7204#if defined(MBEDTLS_SSL_CLI_C)
7205 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7206 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7207#endif
7208#if defined(MBEDTLS_SSL_SRV_C)
7209 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7210 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7211#endif
7212 }
7213 else
7214 ssl->state++;
7215
7216 /*
7217 * Switch to our negotiated transform and session parameters for outbound
7218 * data.
7219 */
7220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7221
7222#if defined(MBEDTLS_SSL_PROTO_DTLS)
7223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7224 {
7225 unsigned char i;
7226
7227 /* Remember current epoch settings for resending */
7228 ssl->handshake->alt_transform_out = ssl->transform_out;
7229 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7230 sizeof( ssl->handshake->alt_out_ctr ) );
7231
7232 /* Set sequence_number to zero */
7233 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7234
7235
7236 /* Increment epoch */
7237 for( i = 2; i > 0; i-- )
7238 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7239 break;
7240
7241 /* The loop goes to its end iff the counter is wrapping */
7242 if( i == 0 )
7243 {
7244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7245 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7246 }
7247 }
7248 else
7249#endif /* MBEDTLS_SSL_PROTO_DTLS */
7250 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7251
7252 ssl->transform_out = ssl->transform_negotiate;
7253 ssl->session_out = ssl->session_negotiate;
7254
7255#if defined(MBEDTLS_SSL_PROTO_DTLS)
7256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7257 mbedtls_ssl_send_flight_completed( ssl );
7258#endif
7259
7260 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7261 {
7262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7263 return( ret );
7264 }
7265
7266#if defined(MBEDTLS_SSL_PROTO_DTLS)
7267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7268 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7269 {
7270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7271 return( ret );
7272 }
7273#endif
7274
7275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7276
7277 return( 0 );
7278}
7279
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007280#define SSL_MAX_HASH_LEN 12
7281
7282int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7283{
7284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7285 unsigned int hash_len = 12;
7286 unsigned char buf[SSL_MAX_HASH_LEN];
7287
7288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7289
7290 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7291
7292 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7293 {
7294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7295 goto exit;
7296 }
7297
7298 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7299 {
7300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7301 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7302 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7303 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7304 goto exit;
7305 }
7306
7307 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7308 {
7309 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7310 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7311 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7312 goto exit;
7313 }
7314
7315 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7316 {
7317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7318 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7319 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7320 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7321 goto exit;
7322 }
7323
7324 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7325 buf, hash_len ) != 0 )
7326 {
7327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7329 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7330 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7331 goto exit;
7332 }
7333
7334#if defined(MBEDTLS_SSL_RENEGOTIATION)
7335 ssl->verify_data_len = hash_len;
7336 memcpy( ssl->peer_verify_data, buf, hash_len );
7337#endif
7338
7339 if( ssl->handshake->resume != 0 )
7340 {
7341#if defined(MBEDTLS_SSL_CLI_C)
7342 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7343 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7344#endif
7345#if defined(MBEDTLS_SSL_SRV_C)
7346 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7347 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7348#endif
7349 }
7350 else
7351 ssl->state++;
7352
7353#if defined(MBEDTLS_SSL_PROTO_DTLS)
7354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7355 mbedtls_ssl_recv_flight_completed( ssl );
7356#endif
7357
7358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7359
7360exit:
7361 mbedtls_platform_zeroize( buf, hash_len );
7362 return( ret );
7363}
7364
Jerry Yu392112c2022-02-17 14:34:10 +08007365#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7366/*
7367 * Helper to get TLS 1.2 PRF from ciphersuite
7368 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7369 */
7370static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7371{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007372#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007373 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7374 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7375
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007376 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007377 return( tls_prf_sha384 );
7378#else
7379 (void) ciphersuite_id;
7380#endif
7381 return( tls_prf_sha256 );
7382}
7383#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007384
7385static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7386{
7387 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007388#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007389 if( tls_prf == tls_prf_sha384 )
7390 {
7391 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7392 }
7393 else
7394#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007395#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007396 if( tls_prf == tls_prf_sha256 )
7397 {
7398 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7399 }
7400 else
7401#endif
7402 return( MBEDTLS_SSL_TLS_PRF_NONE );
7403}
7404
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007405/*
7406 * Populate a transform structure with session keys and all the other
7407 * necessary information.
7408 *
7409 * Parameters:
7410 * - [in/out]: transform: structure to populate
7411 * [in] must be just initialised with mbedtls_ssl_transform_init()
7412 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7413 * - [in] ciphersuite
7414 * - [in] master
7415 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007416 * - [in] tls_prf: pointer to PRF to use for key derivation
7417 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007418 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007419 * - [in] endpoint: client or server
7420 * - [in] ssl: used for:
7421 * - ssl->conf->{f,p}_export_keys
7422 * [in] optionally used for:
7423 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7424 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007425MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007426static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7427 int ciphersuite,
7428 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007429#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007430 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007431#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007432 ssl_tls_prf_t tls_prf,
7433 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007434 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007435 unsigned endpoint,
7436 const mbedtls_ssl_context *ssl )
7437{
7438 int ret = 0;
7439 unsigned char keyblk[256];
7440 unsigned char *key1;
7441 unsigned char *key2;
7442 unsigned char *mac_enc;
7443 unsigned char *mac_dec;
7444 size_t mac_key_len = 0;
7445 size_t iv_copy_len;
7446 size_t keylen;
7447 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007448 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007449#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007450 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007451 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007452#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007453
7454#if defined(MBEDTLS_USE_PSA_CRYPTO)
7455 psa_key_type_t key_type;
7456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7457 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007458 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007459 size_t key_bits;
7460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7461#endif
7462
7463#if !defined(MBEDTLS_DEBUG_C) && \
7464 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7465 if( ssl->f_export_keys == NULL )
7466 {
7467 ssl = NULL; /* make sure we don't use it except for these cases */
7468 (void) ssl;
7469 }
7470#endif
7471
7472 /*
7473 * Some data just needs copying into the structure
7474 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007475#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007476 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007477#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007478 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007479
7480#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7481 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7482#endif
7483
7484#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007485 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007486 {
7487 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7488 * generation separate. This should never happen. */
7489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7490 }
7491#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7492
7493 /*
7494 * Get various info structures
7495 */
7496 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7497 if( ciphersuite_info == NULL )
7498 {
7499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7500 ciphersuite ) );
7501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7502 }
7503
Neil Armstrongab555e02022-04-04 11:07:59 +02007504 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007505#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007506 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007507#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007508 ciphersuite_info );
7509
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007510 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7511 transform->taglen =
7512 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7513
7514#if defined(MBEDTLS_USE_PSA_CRYPTO)
7515 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7516 transform->taglen,
7517 &alg,
7518 &key_type,
7519 &key_bits ) ) != PSA_SUCCESS )
7520 {
7521 ret = psa_ssl_status_to_mbedtls( status );
7522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7523 goto end;
7524 }
7525#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007526 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7527 if( cipher_info == NULL )
7528 {
7529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7530 ciphersuite_info->cipher ) );
7531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7532 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007533#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007534
Neil Armstronge4512952022-03-08 09:08:22 +01007535#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007536 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007537 if( mac_alg == 0 )
7538 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007540 (unsigned) ciphersuite_info->mac ) );
7541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7542 }
7543#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007544 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7545 if( md_info == NULL )
7546 {
7547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7548 (unsigned) ciphersuite_info->mac ) );
7549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7550 }
Neil Armstronge4512952022-03-08 09:08:22 +01007551#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007552
7553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7554 /* Copy own and peer's CID if the use of the CID
7555 * extension has been negotiated. */
7556 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7557 {
7558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7559
7560 transform->in_cid_len = ssl->own_cid_len;
7561 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7562 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7563 transform->in_cid_len );
7564
7565 transform->out_cid_len = ssl->handshake->peer_cid_len;
7566 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7567 ssl->handshake->peer_cid_len );
7568 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7569 transform->out_cid_len );
7570 }
7571#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7572
7573 /*
7574 * Compute key block using the PRF
7575 */
7576 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7577 if( ret != 0 )
7578 {
7579 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7580 return( ret );
7581 }
7582
7583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7584 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7585 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7586 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7587 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7588
7589 /*
7590 * Determine the appropriate key, IV and MAC length.
7591 */
7592
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007593#if defined(MBEDTLS_USE_PSA_CRYPTO)
7594 keylen = PSA_BITS_TO_BYTES(key_bits);
7595#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007596 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007597#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007598
7599#if defined(MBEDTLS_GCM_C) || \
7600 defined(MBEDTLS_CCM_C) || \
7601 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007602 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007603 {
7604 size_t explicit_ivlen;
7605
7606 transform->maclen = 0;
7607 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007608
7609 /* All modes haves 96-bit IVs, but the length of the static parts vary
7610 * with mode and version:
7611 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7612 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7613 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7614 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7615 * sequence number).
7616 */
7617 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007618#if defined(MBEDTLS_USE_PSA_CRYPTO)
7619 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7620#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007621 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007622#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 transform->fixed_ivlen = 12;
7624 else
7625 transform->fixed_ivlen = 4;
7626
7627 /* Minimum length of encrypted record */
7628 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7629 transform->minlen = explicit_ivlen + transform->taglen;
7630 }
7631 else
7632#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7633#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007634 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7635 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7636 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007637 {
Neil Armstronge4512952022-03-08 09:08:22 +01007638#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007639 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007640#else
7641 size_t block_size = cipher_info->block_size;
7642#endif /* MBEDTLS_USE_PSA_CRYPTO */
7643
7644#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007645 /* Get MAC length */
7646 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7647#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007648 /* Initialize HMAC contexts */
7649 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7650 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7651 {
7652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7653 goto end;
7654 }
7655
7656 /* Get MAC length */
7657 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007658#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007659 transform->maclen = mac_key_len;
7660
7661 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007662#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007663 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007664#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007665 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007666#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667
7668 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007669 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 transform->minlen = transform->maclen;
7671 else
7672 {
7673 /*
7674 * GenericBlockCipher:
7675 * 1. if EtM is in use: one block plus MAC
7676 * otherwise: * first multiple of blocklen greater than maclen
7677 * 2. IV
7678 */
7679#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007680 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007681 {
7682 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007683 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007684 }
7685 else
7686#endif
7687 {
7688 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007689 + block_size
7690 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007691 }
7692
Glenn Strauss07c64162022-03-14 12:34:51 -04007693 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007694 {
7695 transform->minlen += transform->ivlen;
7696 }
7697 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007698 {
7699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7700 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7701 goto end;
7702 }
7703 }
7704 }
7705 else
7706#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7707 {
7708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7710 }
7711
7712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7713 (unsigned) keylen,
7714 (unsigned) transform->minlen,
7715 (unsigned) transform->ivlen,
7716 (unsigned) transform->maclen ) );
7717
7718 /*
7719 * Finally setup the cipher contexts, IVs and MAC secrets.
7720 */
7721#if defined(MBEDTLS_SSL_CLI_C)
7722 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7723 {
7724 key1 = keyblk + mac_key_len * 2;
7725 key2 = keyblk + mac_key_len * 2 + keylen;
7726
7727 mac_enc = keyblk;
7728 mac_dec = keyblk + mac_key_len;
7729
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007730 iv_copy_len = ( transform->fixed_ivlen ) ?
7731 transform->fixed_ivlen : transform->ivlen;
7732 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7733 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7734 iv_copy_len );
7735 }
7736 else
7737#endif /* MBEDTLS_SSL_CLI_C */
7738#if defined(MBEDTLS_SSL_SRV_C)
7739 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7740 {
7741 key1 = keyblk + mac_key_len * 2 + keylen;
7742 key2 = keyblk + mac_key_len * 2;
7743
7744 mac_enc = keyblk + mac_key_len;
7745 mac_dec = keyblk;
7746
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 iv_copy_len = ( transform->fixed_ivlen ) ?
7748 transform->fixed_ivlen : transform->ivlen;
7749 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7750 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7751 iv_copy_len );
7752 }
7753 else
7754#endif /* MBEDTLS_SSL_SRV_C */
7755 {
7756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7757 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7758 goto end;
7759 }
7760
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 if( ssl != NULL && ssl->f_export_keys != NULL )
7762 {
7763 ssl->f_export_keys( ssl->p_export_keys,
7764 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7765 master, 48,
7766 randbytes + 32,
7767 randbytes,
7768 tls_prf_get_type( tls_prf ) );
7769 }
7770
7771#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007772 transform->psa_alg = alg;
7773
7774 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7775 {
7776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7777 psa_set_key_algorithm( &attributes, alg );
7778 psa_set_key_type( &attributes, key_type );
7779
7780 if( ( status = psa_import_key( &attributes,
7781 key1,
7782 PSA_BITS_TO_BYTES( key_bits ),
7783 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7784 {
7785 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7786 ret = psa_ssl_status_to_mbedtls( status );
7787 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7788 goto end;
7789 }
7790
7791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7792
7793 if( ( status = psa_import_key( &attributes,
7794 key2,
7795 PSA_BITS_TO_BYTES( key_bits ),
7796 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7797 {
7798 ret = psa_ssl_status_to_mbedtls( status );
7799 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7800 goto end;
7801 }
7802 }
7803#else
7804 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7805 cipher_info ) ) != 0 )
7806 {
7807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7808 goto end;
7809 }
7810
7811 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7812 cipher_info ) ) != 0 )
7813 {
7814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7815 goto end;
7816 }
7817
7818 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7819 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7820 MBEDTLS_ENCRYPT ) ) != 0 )
7821 {
7822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7823 goto end;
7824 }
7825
7826 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7827 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7828 MBEDTLS_DECRYPT ) ) != 0 )
7829 {
7830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7831 goto end;
7832 }
7833
7834#if defined(MBEDTLS_CIPHER_MODE_CBC)
7835 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7836 {
7837 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7838 MBEDTLS_PADDING_NONE ) ) != 0 )
7839 {
7840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7841 goto end;
7842 }
7843
7844 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7845 MBEDTLS_PADDING_NONE ) ) != 0 )
7846 {
7847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7848 goto end;
7849 }
7850 }
7851#endif /* MBEDTLS_CIPHER_MODE_CBC */
7852#endif /* MBEDTLS_USE_PSA_CRYPTO */
7853
Neil Armstrong29c0c042022-03-17 17:47:28 +01007854#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7855 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7856 For AEAD-based ciphersuites, there is nothing to do here. */
7857 if( mac_key_len != 0 )
7858 {
7859#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007860 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007861
7862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007863 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007864 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7865
7866 if( ( status = psa_import_key( &attributes,
7867 mac_enc, mac_key_len,
7868 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7869 {
7870 ret = psa_ssl_status_to_mbedtls( status );
7871 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7872 goto end;
7873 }
7874
Ronald Cronfb39f152022-03-25 14:36:28 +01007875 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007876 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7877#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7878 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7879#endif
7880 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007881 /* mbedtls_ct_hmac() requires the key to be exportable */
7882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7883 PSA_KEY_USAGE_VERIFY_HASH );
7884 else
7885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7886
7887 if( ( status = psa_import_key( &attributes,
7888 mac_dec, mac_key_len,
7889 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7890 {
7891 ret = psa_ssl_status_to_mbedtls( status );
7892 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7893 goto end;
7894 }
7895#else
7896 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7897 if( ret != 0 )
7898 goto end;
7899 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7900 if( ret != 0 )
7901 goto end;
7902#endif /* MBEDTLS_USE_PSA_CRYPTO */
7903 }
7904#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7905
7906 ((void) mac_dec);
7907 ((void) mac_enc);
7908
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007909end:
7910 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7911 return( ret );
7912}
7913
Jerry Yuee40f9d2022-02-17 14:55:16 +08007914#if defined(MBEDTLS_USE_PSA_CRYPTO)
7915int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7916 unsigned char *hash, size_t *hashlen,
7917 unsigned char *data, size_t data_len,
7918 mbedtls_md_type_t md_alg )
7919{
7920 psa_status_t status;
7921 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007922 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007923
7924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7925
7926 if( ( status = psa_hash_setup( &hash_operation,
7927 hash_alg ) ) != PSA_SUCCESS )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7930 goto exit;
7931 }
7932
7933 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7934 64 ) ) != PSA_SUCCESS )
7935 {
7936 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7937 goto exit;
7938 }
7939
7940 if( ( status = psa_hash_update( &hash_operation,
7941 data, data_len ) ) != PSA_SUCCESS )
7942 {
7943 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7944 goto exit;
7945 }
7946
7947 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7948 hashlen ) ) != PSA_SUCCESS )
7949 {
7950 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7951 goto exit;
7952 }
7953
7954exit:
7955 if( status != PSA_SUCCESS )
7956 {
7957 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7958 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7959 switch( status )
7960 {
7961 case PSA_ERROR_NOT_SUPPORTED:
7962 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7963 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7964 case PSA_ERROR_BUFFER_TOO_SMALL:
7965 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7966 case PSA_ERROR_INSUFFICIENT_MEMORY:
7967 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7968 default:
7969 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7970 }
7971 }
7972 return( 0 );
7973}
7974
7975#else
7976
7977int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7978 unsigned char *hash, size_t *hashlen,
7979 unsigned char *data, size_t data_len,
7980 mbedtls_md_type_t md_alg )
7981{
7982 int ret = 0;
7983 mbedtls_md_context_t ctx;
7984 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7985 *hashlen = mbedtls_md_get_size( md_info );
7986
7987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7988
7989 mbedtls_md_init( &ctx );
7990
7991 /*
7992 * digitally-signed struct {
7993 * opaque client_random[32];
7994 * opaque server_random[32];
7995 * ServerDHParams params;
7996 * };
7997 */
7998 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7999 {
8000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8001 goto exit;
8002 }
8003 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8004 {
8005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8006 goto exit;
8007 }
8008 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8009 {
8010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8011 goto exit;
8012 }
8013 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8014 {
8015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8016 goto exit;
8017 }
8018 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8019 {
8020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8021 goto exit;
8022 }
8023
8024exit:
8025 mbedtls_md_free( &ctx );
8026
8027 if( ret != 0 )
8028 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8029 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8030
8031 return( ret );
8032}
8033#endif /* MBEDTLS_USE_PSA_CRYPTO */
8034
Jerry Yud9d91da2022-02-17 14:57:06 +08008035#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8036
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008037/* Find the preferred hash for a given signature algorithm. */
8038unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8039 mbedtls_ssl_context *ssl,
8040 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008041{
Gabor Mezei078e8032022-04-27 21:17:56 +02008042 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008043 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008044
8045 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008046 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008047
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008048 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008049 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008050 unsigned int hash_alg_received =
8051 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8052 received_sig_algs[i] );
8053 unsigned int sig_alg_received =
8054 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8055 received_sig_algs[i] );
8056
8057 if( sig_alg == sig_alg_received )
8058 {
8059#if defined(MBEDTLS_USE_PSA_CRYPTO)
8060 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8061 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008062 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008063 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008064
Neil Armstrong96eceb82022-06-30 18:05:05 +02008065 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8066 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8067 PSA_ALG_ECDSA( psa_hash_alg ),
8068 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008069 continue;
8070
Neil Armstrong96eceb82022-06-30 18:05:05 +02008071 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008072 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8073 PSA_ALG_RSA_PKCS1V15_SIGN(
8074 psa_hash_alg ),
8075 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008076 continue;
8077 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008078#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008079
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008080 return( hash_alg_received );
8081 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008082 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008083
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008084 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008085}
8086
8087#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8088
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008089/* Serialization of TLS 1.2 sessions:
8090 *
8091 * struct {
8092 * uint64 start_time;
8093 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008094 * uint8 session_id_len; // at most 32
8095 * opaque session_id[32];
8096 * opaque master[48]; // fixed length in the standard
8097 * uint32 verify_result;
8098 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8099 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8100 * uint32 ticket_lifetime;
8101 * uint8 mfl_code; // up to 255 according to standard
8102 * uint8 encrypt_then_mac; // 0 or 1
8103 * } serialized_session_tls12;
8104 *
8105 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008106static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008107 unsigned char *buf,
8108 size_t buf_len )
8109{
8110 unsigned char *p = buf;
8111 size_t used = 0;
8112
8113#if defined(MBEDTLS_HAVE_TIME)
8114 uint64_t start;
8115#endif
8116#if defined(MBEDTLS_X509_CRT_PARSE_C)
8117#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8118 size_t cert_len;
8119#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8120#endif /* MBEDTLS_X509_CRT_PARSE_C */
8121
8122 /*
8123 * Time
8124 */
8125#if defined(MBEDTLS_HAVE_TIME)
8126 used += 8;
8127
8128 if( used <= buf_len )
8129 {
8130 start = (uint64_t) session->start;
8131
8132 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8133 p += 8;
8134 }
8135#endif /* MBEDTLS_HAVE_TIME */
8136
8137 /*
8138 * Basic mandatory fields
8139 */
8140 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008141 + 1 /* id_len */
8142 + sizeof( session->id )
8143 + sizeof( session->master )
8144 + 4; /* verify_result */
8145
8146 if( used <= buf_len )
8147 {
8148 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8149 p += 2;
8150
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008151 *p++ = MBEDTLS_BYTE_0( session->id_len );
8152 memcpy( p, session->id, 32 );
8153 p += 32;
8154
8155 memcpy( p, session->master, 48 );
8156 p += 48;
8157
8158 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8159 p += 4;
8160 }
8161
8162 /*
8163 * Peer's end-entity certificate
8164 */
8165#if defined(MBEDTLS_X509_CRT_PARSE_C)
8166#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8167 if( session->peer_cert == NULL )
8168 cert_len = 0;
8169 else
8170 cert_len = session->peer_cert->raw.len;
8171
8172 used += 3 + cert_len;
8173
8174 if( used <= buf_len )
8175 {
8176 *p++ = MBEDTLS_BYTE_2( cert_len );
8177 *p++ = MBEDTLS_BYTE_1( cert_len );
8178 *p++ = MBEDTLS_BYTE_0( cert_len );
8179
8180 if( session->peer_cert != NULL )
8181 {
8182 memcpy( p, session->peer_cert->raw.p, cert_len );
8183 p += cert_len;
8184 }
8185 }
8186#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8187 if( session->peer_cert_digest != NULL )
8188 {
8189 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8190 if( used <= buf_len )
8191 {
8192 *p++ = (unsigned char) session->peer_cert_digest_type;
8193 *p++ = (unsigned char) session->peer_cert_digest_len;
8194 memcpy( p, session->peer_cert_digest,
8195 session->peer_cert_digest_len );
8196 p += session->peer_cert_digest_len;
8197 }
8198 }
8199 else
8200 {
8201 used += 2;
8202 if( used <= buf_len )
8203 {
8204 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8205 *p++ = 0;
8206 }
8207 }
8208#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8209#endif /* MBEDTLS_X509_CRT_PARSE_C */
8210
8211 /*
8212 * Session ticket if any, plus associated data
8213 */
8214#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8215 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8216
8217 if( used <= buf_len )
8218 {
8219 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8220 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8221 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8222
8223 if( session->ticket != NULL )
8224 {
8225 memcpy( p, session->ticket, session->ticket_len );
8226 p += session->ticket_len;
8227 }
8228
8229 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8230 p += 4;
8231 }
8232#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8233
8234 /*
8235 * Misc extension-related info
8236 */
8237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8238 used += 1;
8239
8240 if( used <= buf_len )
8241 *p++ = session->mfl_code;
8242#endif
8243
8244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8245 used += 1;
8246
8247 if( used <= buf_len )
8248 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8249#endif
8250
8251 return( used );
8252}
8253
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008254MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008255static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008256 const unsigned char *buf,
8257 size_t len )
8258{
8259#if defined(MBEDTLS_HAVE_TIME)
8260 uint64_t start;
8261#endif
8262#if defined(MBEDTLS_X509_CRT_PARSE_C)
8263#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8264 size_t cert_len;
8265#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8266#endif /* MBEDTLS_X509_CRT_PARSE_C */
8267
8268 const unsigned char *p = buf;
8269 const unsigned char * const end = buf + len;
8270
8271 /*
8272 * Time
8273 */
8274#if defined(MBEDTLS_HAVE_TIME)
8275 if( 8 > (size_t)( end - p ) )
8276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8277
8278 start = ( (uint64_t) p[0] << 56 ) |
8279 ( (uint64_t) p[1] << 48 ) |
8280 ( (uint64_t) p[2] << 40 ) |
8281 ( (uint64_t) p[3] << 32 ) |
8282 ( (uint64_t) p[4] << 24 ) |
8283 ( (uint64_t) p[5] << 16 ) |
8284 ( (uint64_t) p[6] << 8 ) |
8285 ( (uint64_t) p[7] );
8286 p += 8;
8287
8288 session->start = (time_t) start;
8289#endif /* MBEDTLS_HAVE_TIME */
8290
8291 /*
8292 * Basic mandatory fields
8293 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008294 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8296
8297 session->ciphersuite = ( p[0] << 8 ) | p[1];
8298 p += 2;
8299
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008300 session->id_len = *p++;
8301 memcpy( session->id, p, 32 );
8302 p += 32;
8303
8304 memcpy( session->master, p, 48 );
8305 p += 48;
8306
8307 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8308 ( (uint32_t) p[1] << 16 ) |
8309 ( (uint32_t) p[2] << 8 ) |
8310 ( (uint32_t) p[3] );
8311 p += 4;
8312
8313 /* Immediately clear invalid pointer values that have been read, in case
8314 * we exit early before we replaced them with valid ones. */
8315#if defined(MBEDTLS_X509_CRT_PARSE_C)
8316#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8317 session->peer_cert = NULL;
8318#else
8319 session->peer_cert_digest = NULL;
8320#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8321#endif /* MBEDTLS_X509_CRT_PARSE_C */
8322#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8323 session->ticket = NULL;
8324#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8325
8326 /*
8327 * Peer certificate
8328 */
8329#if defined(MBEDTLS_X509_CRT_PARSE_C)
8330#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8331 /* Deserialize CRT from the end of the ticket. */
8332 if( 3 > (size_t)( end - p ) )
8333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8334
8335 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8336 p += 3;
8337
8338 if( cert_len != 0 )
8339 {
8340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8341
8342 if( cert_len > (size_t)( end - p ) )
8343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8344
8345 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8346
8347 if( session->peer_cert == NULL )
8348 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8349
8350 mbedtls_x509_crt_init( session->peer_cert );
8351
8352 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8353 p, cert_len ) ) != 0 )
8354 {
8355 mbedtls_x509_crt_free( session->peer_cert );
8356 mbedtls_free( session->peer_cert );
8357 session->peer_cert = NULL;
8358 return( ret );
8359 }
8360
8361 p += cert_len;
8362 }
8363#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8364 /* Deserialize CRT digest from the end of the ticket. */
8365 if( 2 > (size_t)( end - p ) )
8366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8367
8368 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8369 session->peer_cert_digest_len = (size_t) *p++;
8370
8371 if( session->peer_cert_digest_len != 0 )
8372 {
8373 const mbedtls_md_info_t *md_info =
8374 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8375 if( md_info == NULL )
8376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8377 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8379
8380 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8382
8383 session->peer_cert_digest =
8384 mbedtls_calloc( 1, session->peer_cert_digest_len );
8385 if( session->peer_cert_digest == NULL )
8386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8387
8388 memcpy( session->peer_cert_digest, p,
8389 session->peer_cert_digest_len );
8390 p += session->peer_cert_digest_len;
8391 }
8392#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8393#endif /* MBEDTLS_X509_CRT_PARSE_C */
8394
8395 /*
8396 * Session ticket and associated data
8397 */
8398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8399 if( 3 > (size_t)( end - p ) )
8400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8401
8402 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8403 p += 3;
8404
8405 if( session->ticket_len != 0 )
8406 {
8407 if( session->ticket_len > (size_t)( end - p ) )
8408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8409
8410 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8411 if( session->ticket == NULL )
8412 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8413
8414 memcpy( session->ticket, p, session->ticket_len );
8415 p += session->ticket_len;
8416 }
8417
8418 if( 4 > (size_t)( end - p ) )
8419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8420
8421 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8422 ( (uint32_t) p[1] << 16 ) |
8423 ( (uint32_t) p[2] << 8 ) |
8424 ( (uint32_t) p[3] );
8425 p += 4;
8426#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8427
8428 /*
8429 * Misc extension-related info
8430 */
8431#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8432 if( 1 > (size_t)( end - p ) )
8433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8434
8435 session->mfl_code = *p++;
8436#endif
8437
8438#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8439 if( 1 > (size_t)( end - p ) )
8440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8441
8442 session->encrypt_then_mac = *p++;
8443#endif
8444
8445 /* Done, should have consumed entire buffer */
8446 if( p != end )
8447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8448
8449 return( 0 );
8450}
Jerry Yudc7bd172022-02-17 13:44:15 +08008451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8452
XiaokangQian75d40ef2022-04-20 11:05:24 +00008453int mbedtls_ssl_validate_ciphersuite(
8454 const mbedtls_ssl_context *ssl,
8455 const mbedtls_ssl_ciphersuite_t *suite_info,
8456 mbedtls_ssl_protocol_version min_tls_version,
8457 mbedtls_ssl_protocol_version max_tls_version )
8458{
8459 (void) ssl;
8460
8461 if( suite_info == NULL )
8462 return( -1 );
8463
8464 if( ( suite_info->min_tls_version > max_tls_version ) ||
8465 ( suite_info->max_tls_version < min_tls_version ) )
8466 {
8467 return( -1 );
8468 }
8469
XiaokangQian060d8672022-04-21 09:24:56 +00008470#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008471#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8472 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8473 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8474 {
8475 return( -1 );
8476 }
8477#endif
8478
8479 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8480#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8481 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8482 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8483 {
8484 return( -1 );
8485 }
8486#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8487#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8488
8489 return( 0 );
8490}
8491
XiaokangQianeaf36512022-04-24 09:07:44 +00008492#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8493/*
8494 * Function for writing a signature algorithm extension.
8495 *
8496 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8497 * value (TLS 1.3 RFC8446):
8498 * enum {
8499 * ....
8500 * ecdsa_secp256r1_sha256( 0x0403 ),
8501 * ecdsa_secp384r1_sha384( 0x0503 ),
8502 * ecdsa_secp521r1_sha512( 0x0603 ),
8503 * ....
8504 * } SignatureScheme;
8505 *
8506 * struct {
8507 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8508 * } SignatureSchemeList;
8509 *
8510 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8511 * value (TLS 1.2 RFC5246):
8512 * enum {
8513 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8514 * sha512(6), (255)
8515 * } HashAlgorithm;
8516 *
8517 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8518 * SignatureAlgorithm;
8519 *
8520 * struct {
8521 * HashAlgorithm hash;
8522 * SignatureAlgorithm signature;
8523 * } SignatureAndHashAlgorithm;
8524 *
8525 * SignatureAndHashAlgorithm
8526 * supported_signature_algorithms<2..2^16-2>;
8527 *
8528 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8529 * generalization of the TLS 1.2 signature algorithm extension.
8530 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8531 * `SignatureScheme` field of TLS 1.3
8532 *
8533 */
8534int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8535 const unsigned char *end, size_t *out_len )
8536{
8537 unsigned char *p = buf;
8538 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8539 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8540
8541 *out_len = 0;
8542
8543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8544
8545 /* Check if we have space for header and length field:
8546 * - extension_type (2 bytes)
8547 * - extension_data_length (2 bytes)
8548 * - supported_signature_algorithms_length (2 bytes)
8549 */
8550 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8551 p += 6;
8552
8553 /*
8554 * Write supported_signature_algorithms
8555 */
8556 supported_sig_alg = p;
8557 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8558 if( sig_alg == NULL )
8559 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8560
8561 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8562 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8564 *sig_alg,
8565 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008566 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8567 continue;
8568 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8569 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8570 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008572 *sig_alg,
8573 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008574 }
8575
8576 /* Length of supported_signature_algorithms */
8577 supported_sig_alg_len = p - supported_sig_alg;
8578 if( supported_sig_alg_len == 0 )
8579 {
8580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8582 }
8583
XiaokangQianeaf36512022-04-24 09:07:44 +00008584 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008585 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008586 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8587
XiaokangQianeaf36512022-04-24 09:07:44 +00008588 *out_len = p - buf;
8589
8590#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8591 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8592#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8593 return( 0 );
8594}
8595#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8596
XiaokangQian40a35232022-05-07 09:02:40 +00008597#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008598/*
8599 * mbedtls_ssl_parse_server_name_ext
8600 *
8601 * Structure of server_name extension:
8602 *
8603 * enum {
8604 * host_name(0), (255)
8605 * } NameType;
8606 * opaque HostName<1..2^16-1>;
8607 *
8608 * struct {
8609 * NameType name_type;
8610 * select (name_type) {
8611 * case host_name: HostName;
8612 * } name;
8613 * } ServerName;
8614 * struct {
8615 * ServerName server_name_list<1..2^16-1>
8616 * } ServerNameList;
8617 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008618MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008619int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8620 const unsigned char *buf,
8621 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008622{
8623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8624 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008625 size_t server_name_list_len, hostname_len;
8626 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008627
XiaokangQianf2a94202022-05-20 06:44:24 +00008628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008629
8630 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008631 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008632 p += 2;
8633
XiaokangQian9b2b7712022-05-17 02:57:00 +00008634 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8635 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008636 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008637 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008638 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008639 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008640 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8641 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008642
8643 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8644 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008645 /* sni_name is intended to be used only during the parsing of the
8646 * ClientHello message (it is reset to NULL before the end of
8647 * the message parsing). Thus it is ok to just point to the
8648 * reception buffer and not make a copy of it.
8649 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008650 ssl->handshake->sni_name = p + 3;
8651 ssl->handshake->sni_name_len = hostname_len;
8652 if( ssl->conf->f_sni == NULL )
8653 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008654 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008655 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008656 if( ret != 0 )
8657 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008658 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008659 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8660 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008661 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8662 }
8663 return( 0 );
8664 }
8665
8666 p += hostname_len + 3;
8667 }
8668
8669 return( 0 );
8670}
8671#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8672
XiaokangQianacb39922022-06-17 10:18:48 +00008673#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008674MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008675int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8676 const unsigned char *buf,
8677 const unsigned char *end )
8678{
8679 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008680 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008681 const unsigned char *protocol_name_list;
8682 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008683 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008684
8685 /* If ALPN not configured, just ignore the extension */
8686 if( ssl->conf->alpn_list == NULL )
8687 return( 0 );
8688
8689 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008690 * RFC7301, section 3.1
8691 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008692 *
XiaokangQianc7403452022-06-23 03:24:12 +00008693 * struct {
8694 * ProtocolName protocol_name_list<2..2^16-1>
8695 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008696 */
8697
XiaokangQianc7403452022-06-23 03:24:12 +00008698 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008699 * protocol_name_list_len 2 bytes
8700 * protocol_name_len 1 bytes
8701 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008702 */
XiaokangQianacb39922022-06-17 10:18:48 +00008703 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8704
XiaokangQianc7403452022-06-23 03:24:12 +00008705 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008706 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008707 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008708 protocol_name_list = p;
8709 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008710
8711 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008712 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008713 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008714 protocol_name_len = *p++;
8715 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8716 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008717 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 {
8719 MBEDTLS_SSL_PEND_FATAL_ALERT(
8720 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8721 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008722 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008723 }
8724
8725 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008726 }
8727
8728 /* Use our order of preference */
8729 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8730 {
8731 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008732 p = protocol_name_list;
8733 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008734 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008735 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008736 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008737 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008738 {
8739 ssl->alpn_chosen = *alpn;
8740 return( 0 );
8741 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008742
8743 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008744 }
8745 }
8746
XiaokangQian95d5f542022-06-24 02:29:26 +00008747 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008748 MBEDTLS_SSL_PEND_FATAL_ALERT(
8749 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8750 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8751 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8752}
8753
8754int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8755 unsigned char *buf,
8756 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008757 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008758{
8759 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008760 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008761 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008762
8763 if( ssl->alpn_chosen == NULL )
8764 {
8765 return( 0 );
8766 }
8767
XiaokangQian95d5f542022-06-24 02:29:26 +00008768 protocol_name_len = strlen( ssl->alpn_chosen );
8769 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008770
8771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8772 /*
8773 * 0 . 1 ext identifier
8774 * 2 . 3 ext length
8775 * 4 . 5 protocol list length
8776 * 6 . 6 protocol name length
8777 * 7 . 7+n protocol name
8778 */
8779 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8780
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008782
XiaokangQian95d5f542022-06-24 02:29:26 +00008783 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8784 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008785 /* Note: the length of the chosen protocol has been checked to be less
8786 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8787 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008788 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008789
XiaokangQian95d5f542022-06-24 02:29:26 +00008790 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008791 return ( 0 );
8792}
8793#endif /* MBEDTLS_SSL_ALPN */
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#endif /* MBEDTLS_SSL_TLS_C */