blob: f0615ea7d1f68a64d5fc9e064c3eb4a5d8027d6e [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;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001364
1365 if( ssl == NULL ||
1366 session == NULL ||
1367 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001368 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371 }
1372
Hanno Beckere810bbc2021-05-14 16:01:05 +01001373 if( ssl->handshake->resume == 1 )
1374 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1375
Hanno Becker52055ae2019-02-06 14:30:46 +00001376 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1377 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001378 return( ret );
1379
Paul Bakker0a597072012-09-25 21:55:46 +00001380 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001381
1382 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001386void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1387 const int *ciphersuites )
1388{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001389 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390}
1391
Ronald Cron6f135e12021-12-08 16:57:54 +01001392#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001393void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001394 const int kex_modes )
1395{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001396 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001397}
Ronald Cron6f135e12021-12-08 16:57:54 +01001398#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001401void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001402 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001403{
1404 conf->cert_profile = profile;
1405}
1406
Glenn Strauss36872db2022-01-22 05:06:31 -05001407static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1408{
1409 mbedtls_ssl_key_cert *cur = key_cert, *next;
1410
1411 while( cur != NULL )
1412 {
1413 next = cur->next;
1414 mbedtls_free( cur );
1415 cur = next;
1416 }
1417}
1418
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001419/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001420MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001421static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1422 mbedtls_x509_crt *cert,
1423 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001424{
niisato8ee24222018-06-25 19:05:48 +09001425 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001426
Glenn Strauss36872db2022-01-22 05:06:31 -05001427 if( cert == NULL )
1428 {
1429 /* Free list if cert is null */
1430 ssl_key_cert_free( *head );
1431 *head = NULL;
1432 return( 0 );
1433 }
1434
niisato8ee24222018-06-25 19:05:48 +09001435 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1436 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001438
niisato8ee24222018-06-25 19:05:48 +09001439 new_cert->cert = cert;
1440 new_cert->key = key;
1441 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001442
Glenn Strauss36872db2022-01-22 05:06:31 -05001443 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001444 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001445 {
niisato8ee24222018-06-25 19:05:48 +09001446 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001447 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001448 else
1449 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001450 mbedtls_ssl_key_cert *cur = *head;
1451 while( cur->next != NULL )
1452 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001453 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001454 }
1455
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001456 return( 0 );
1457}
1458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001460 mbedtls_x509_crt *own_cert,
1461 mbedtls_pk_context *pk_key )
1462{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001463 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001464}
1465
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001466void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001467 mbedtls_x509_crt *ca_chain,
1468 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001470 conf->ca_chain = ca_chain;
1471 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001472
1473#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1474 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1475 * cannot be used together. */
1476 conf->f_ca_cb = NULL;
1477 conf->p_ca_cb = NULL;
1478#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001479}
Hanno Becker5adaad92019-03-27 16:54:37 +00001480
1481#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1482void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001483 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001484 void *p_ca_cb )
1485{
1486 conf->f_ca_cb = f_ca_cb;
1487 conf->p_ca_cb = p_ca_cb;
1488
1489 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1490 * cannot be used together. */
1491 conf->ca_chain = NULL;
1492 conf->ca_crl = NULL;
1493}
1494#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001496
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001497#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001498const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1499 size_t *name_len )
1500{
1501 *name_len = ssl->handshake->sni_name_len;
1502 return( ssl->handshake->sni_name );
1503}
1504
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001505int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1506 mbedtls_x509_crt *own_cert,
1507 mbedtls_pk_context *pk_key )
1508{
1509 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1510 own_cert, pk_key ) );
1511}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001512
1513void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1514 mbedtls_x509_crt *ca_chain,
1515 mbedtls_x509_crl *ca_crl )
1516{
1517 ssl->handshake->sni_ca_chain = ca_chain;
1518 ssl->handshake->sni_ca_crl = ca_crl;
1519}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001520
Glenn Strauss999ef702022-03-11 01:37:23 -05001521#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1522void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1523 const mbedtls_x509_crt *crt)
1524{
1525 ssl->handshake->dn_hints = crt;
1526}
1527#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1528
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001529void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1530 int authmode )
1531{
1532 ssl->handshake->sni_authmode = authmode;
1533}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001534#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1535
Hanno Becker8927c832019-04-03 12:52:50 +01001536#if defined(MBEDTLS_X509_CRT_PARSE_C)
1537void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1538 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1539 void *p_vrfy )
1540{
1541 ssl->f_vrfy = f_vrfy;
1542 ssl->p_vrfy = p_vrfy;
1543}
1544#endif
1545
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001546#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001547/*
1548 * Set EC J-PAKE password for current handshake
1549 */
1550int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1551 const unsigned char *pw,
1552 size_t pw_len )
1553{
1554 mbedtls_ecjpake_role role;
1555
Janos Follath8eb64132016-06-03 15:40:57 +01001556 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1558
1559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1560 role = MBEDTLS_ECJPAKE_SERVER;
1561 else
1562 role = MBEDTLS_ECJPAKE_CLIENT;
1563
1564 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1565 role,
1566 MBEDTLS_MD_SHA256,
1567 MBEDTLS_ECP_DP_SECP256R1,
1568 pw, pw_len ) );
1569}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001571
Gilles Peskineeccd8882020-03-10 12:19:08 +01001572#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001573
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001574MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001575static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1576{
1577#if defined(MBEDTLS_USE_PSA_CRYPTO)
1578 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1579 return( 1 );
1580#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001581 if( conf->psk != NULL )
1582 return( 1 );
1583
1584 return( 0 );
1585}
1586
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001587static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1588{
1589 /* Remove reference to existing PSK, if any. */
1590#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001591 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001592 {
1593 /* The maintenance of the PSK key slot is the
1594 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001596 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001597#endif /* MBEDTLS_USE_PSA_CRYPTO */
1598 if( conf->psk != NULL )
1599 {
1600 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1601
1602 mbedtls_free( conf->psk );
1603 conf->psk = NULL;
1604 conf->psk_len = 0;
1605 }
1606
1607 /* Remove reference to PSK identity, if any. */
1608 if( conf->psk_identity != NULL )
1609 {
1610 mbedtls_free( conf->psk_identity );
1611 conf->psk_identity = NULL;
1612 conf->psk_identity_len = 0;
1613 }
1614}
1615
Hanno Becker7390c712018-11-15 13:33:04 +00001616/* This function assumes that PSK identity in the SSL config is unset.
1617 * It checks that the provided identity is well-formed and attempts
1618 * to make a copy of it in the SSL config.
1619 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001620MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001621static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1622 unsigned char const *psk_identity,
1623 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001624{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001625 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001626 if( psk_identity == NULL ||
1627 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001628 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001629 {
1630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1631 }
1632
Hanno Becker7390c712018-11-15 13:33:04 +00001633 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1634 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001635 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001636
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001637 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001638 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001639
1640 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001641}
1642
Hanno Becker7390c712018-11-15 13:33:04 +00001643int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1644 const unsigned char *psk, size_t psk_len,
1645 const unsigned char *psk_identity, size_t psk_identity_len )
1646{
Janos Follath865b3eb2019-12-16 11:46:15 +00001647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001648
1649 /* We currently only support one PSK, raw or opaque. */
1650 if( ssl_conf_psk_is_configured( conf ) )
1651 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001652
1653 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001654 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001656 if( psk_len == 0 )
1657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1658 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1660
Hanno Becker7390c712018-11-15 13:33:04 +00001661 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1662 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1663 conf->psk_len = psk_len;
1664 memcpy( conf->psk, psk, conf->psk_len );
1665
1666 /* Check and set PSK Identity */
1667 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1668 if( ret != 0 )
1669 ssl_conf_remove_psk( conf );
1670
1671 return( ret );
1672}
1673
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001674static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1675{
1676#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001677 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001678 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001679 /* The maintenance of the external PSK key slot is the
1680 * user's responsibility. */
1681 if( ssl->handshake->psk_opaque_is_internal )
1682 {
1683 psa_destroy_key( ssl->handshake->psk_opaque );
1684 ssl->handshake->psk_opaque_is_internal = 0;
1685 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001687 }
Neil Armstronge952a302022-05-03 10:22:14 +02001688#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001689 if( ssl->handshake->psk != NULL )
1690 {
1691 mbedtls_platform_zeroize( ssl->handshake->psk,
1692 ssl->handshake->psk_len );
1693 mbedtls_free( ssl->handshake->psk );
1694 ssl->handshake->psk_len = 0;
1695 }
Neil Armstronge952a302022-05-03 10:22:14 +02001696#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001697}
1698
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001699int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1700 const unsigned char *psk, size_t psk_len )
1701{
Neil Armstrong501c9322022-05-03 09:35:09 +02001702#if defined(MBEDTLS_USE_PSA_CRYPTO)
1703 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001705 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001707#endif /* MBEDTLS_USE_PSA_CRYPTO */
1708
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001709 if( psk == NULL || ssl->handshake == NULL )
1710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1711
1712 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1714
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001715 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001716
Neil Armstrong501c9322022-05-03 09:35:09 +02001717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1719 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1720 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001721 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001722 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1723 else
1724 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1725 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1726 }
1727#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001728
Jerry Yu568ec252022-07-22 21:27:34 +08001729#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001730 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1731 {
1732 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1733 psa_set_key_usage_flags( &key_attributes,
1734 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1735 }
1736#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1737
Neil Armstrong501c9322022-05-03 09:35:09 +02001738 psa_set_key_algorithm( &key_attributes, alg );
1739 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1740
1741 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1742 if( status != PSA_SUCCESS )
1743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1744
1745 /* Allow calling psa_destroy_key() on psk remove */
1746 ssl->handshake->psk_opaque_is_internal = 1;
1747 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1748#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001749 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001750 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001751
1752 ssl->handshake->psk_len = psk_len;
1753 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1754
1755 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001756#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001757}
1758
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001759#if defined(MBEDTLS_USE_PSA_CRYPTO)
1760int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001761 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001762 const unsigned char *psk_identity,
1763 size_t psk_identity_len )
1764{
Janos Follath865b3eb2019-12-16 11:46:15 +00001765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001766
1767 /* We currently only support one PSK, raw or opaque. */
1768 if( ssl_conf_psk_is_configured( conf ) )
1769 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001770
Hanno Becker7390c712018-11-15 13:33:04 +00001771 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001772 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001775
1776 /* Check and set PSK Identity */
1777 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1778 psk_identity_len );
1779 if( ret != 0 )
1780 ssl_conf_remove_psk( conf );
1781
1782 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001783}
1784
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001785int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1786 mbedtls_svc_key_id_t psk )
1787{
1788 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1789 ( ssl->handshake == NULL ) )
1790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1791
1792 ssl_remove_psk( ssl );
1793 ssl->handshake->psk_opaque = psk;
1794 return( 0 );
1795}
1796#endif /* MBEDTLS_USE_PSA_CRYPTO */
1797
1798void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1799 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1800 size_t),
1801 void *p_psk )
1802{
1803 conf->f_psk = f_psk;
1804 conf->p_psk = p_psk;
1805}
1806#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1807
1808#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001809static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1810 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001811{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001812#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001813 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001814 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001815#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001816 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001817 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001818 return( MBEDTLS_SSL_MODE_STREAM );
1819}
1820
1821#else /* MBEDTLS_USE_PSA_CRYPTO */
1822
1823static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1824 mbedtls_cipher_mode_t mode )
1825{
1826#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1827 if( mode == MBEDTLS_MODE_CBC )
1828 return( MBEDTLS_SSL_MODE_CBC );
1829#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1830
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001831#if defined(MBEDTLS_GCM_C) || \
1832 defined(MBEDTLS_CCM_C) || \
1833 defined(MBEDTLS_CHACHAPOLY_C)
1834 if( mode == MBEDTLS_MODE_GCM ||
1835 mode == MBEDTLS_MODE_CCM ||
1836 mode == MBEDTLS_MODE_CHACHAPOLY )
1837 return( MBEDTLS_SSL_MODE_AEAD );
1838#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001839
1840 return( MBEDTLS_SSL_MODE_STREAM );
1841}
Gilles Peskine301711e2022-04-26 16:57:05 +02001842#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843
Gilles Peskinee108d982022-04-26 16:50:40 +02001844static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1845 mbedtls_ssl_mode_t base_mode,
1846 int encrypt_then_mac )
1847{
1848#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1849 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1850 base_mode == MBEDTLS_SSL_MODE_CBC )
1851 {
1852 return( MBEDTLS_SSL_MODE_CBC_ETM );
1853 }
1854#else
1855 (void) encrypt_then_mac;
1856#endif
1857 return( base_mode );
1858}
1859
Neil Armstrongab555e02022-04-04 11:07:59 +02001860mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001861 const mbedtls_ssl_transform *transform )
1862{
Gilles Peskinee108d982022-04-26 16:50:40 +02001863 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001864#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001865 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001866#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001867 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1868#endif
1869 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001870
Gilles Peskinee108d982022-04-26 16:50:40 +02001871 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001872#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001873 encrypt_then_mac = transform->encrypt_then_mac;
1874#endif
1875 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001876}
1877
Neil Armstrongab555e02022-04-04 11:07:59 +02001878mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001879#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001880 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001881#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001882 const mbedtls_ssl_ciphersuite_t *suite )
1883{
Gilles Peskinee108d982022-04-26 16:50:40 +02001884 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1885
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001886#if defined(MBEDTLS_USE_PSA_CRYPTO)
1887 psa_status_t status;
1888 psa_algorithm_t alg;
1889 psa_key_type_t type;
1890 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001891 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1892 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001893 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894#else
1895 const mbedtls_cipher_info_t *cipher =
1896 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 {
1899 base_mode =
1900 mbedtls_ssl_get_base_mode(
1901 mbedtls_cipher_info_get_mode( cipher ) );
1902 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001903#endif /* MBEDTLS_USE_PSA_CRYPTO */
1904
Gilles Peskinee108d982022-04-26 16:50:40 +02001905#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1906 int encrypt_then_mac = 0;
1907#endif
1908 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909}
1910
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001911#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001912
1913#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001914/* Serialization of TLS 1.3 sessions:
1915 *
1916 * struct {
1917 * uint64 ticket_received;
1918 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001919 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001920 * } ClientOnlyData;
1921 *
1922 * struct {
1923 * uint8 endpoint;
1924 * uint8 ciphersuite[2];
1925 * uint32 ticket_age_add;
1926 * uint8 ticket_flags;
1927 * opaque resumption_key<0..255>;
1928 * select ( endpoint ) {
1929 * case client: ClientOnlyData;
1930 * case server: uint64 start_time;
1931 * };
1932 * } serialized_session_tls13;
1933 *
1934 */
1935#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001936MBEDTLS_CHECK_RETURN_CRITICAL
1937static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1938 unsigned char *buf,
1939 size_t buf_len,
1940 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001941{
1942 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001943 size_t needed = 1 /* endpoint */
1944 + 2 /* ciphersuite */
1945 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001946 + 1 /* ticket_flags */
1947 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001948 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001949
1950 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1952 needed += session->resumption_key_len; /* resumption_key */
1953
Jerry Yu438ddd82022-07-07 06:55:50 +00001954#if defined(MBEDTLS_HAVE_TIME)
1955 needed += 8; /* start_time or ticket_received */
1956#endif
1957
1958#if defined(MBEDTLS_SSL_CLI_C)
1959 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1960 {
1961 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001962 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001963
1964 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001965 if( session->ticket_len > SIZE_MAX - needed )
1966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001967
Jerry Yue28d9742022-08-18 15:44:03 +08001968 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001969 }
1970#endif /* MBEDTLS_SSL_CLI_C */
1971
Jerry Yue36fdd62022-08-17 21:31:36 +08001972 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001973 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001974 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001975
1976 p[0] = session->endpoint;
1977 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1978 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1979 p[7] = session->ticket_flags;
1980
1981 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001982 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001983 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001984 memcpy( p, session->resumption_key, session->resumption_key_len );
1985 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001986
1987#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1988 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1989 {
1990 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1991 p += 8;
1992 }
1993#endif /* MBEDTLS_HAVE_TIME */
1994
1995#if defined(MBEDTLS_SSL_CLI_C)
1996 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1997 {
1998#if defined(MBEDTLS_HAVE_TIME)
1999 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2000 p += 8;
2001#endif
2002 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2003 p += 4;
2004
2005 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2006 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002007
2008 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002009 {
2010 memcpy( p, session->ticket, session->ticket_len );
2011 p += session->ticket_len;
2012 }
2013 }
2014#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002015 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002016}
2017
2018MBEDTLS_CHECK_RETURN_CRITICAL
2019static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2020 const unsigned char *buf,
2021 size_t len )
2022{
2023 const unsigned char *p = buf;
2024 const unsigned char *end = buf + len;
2025
2026 if( end - p < 9 )
2027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2028 session->endpoint = p[0];
2029 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2030 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2031 session->ticket_flags = p[7];
2032
2033 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002034 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002035 p += 9;
2036
Jerry Yubc7c1a42022-07-21 22:57:37 +08002037 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2039
Jerry Yubc7c1a42022-07-21 22:57:37 +08002040 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002042 memcpy( session->resumption_key, p, session->resumption_key_len );
2043 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002044
2045#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2046 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2047 {
2048 if( end - p < 8 )
2049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2050 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2051 p += 8;
2052 }
2053#endif /* MBEDTLS_HAVE_TIME */
2054
2055#if defined(MBEDTLS_SSL_CLI_C)
2056 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2057 {
2058#if defined(MBEDTLS_HAVE_TIME)
2059 if( end - p < 8 )
2060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2061 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2062 p += 8;
2063#endif
2064 if( end - p < 4 )
2065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2066 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2067 p += 4;
2068
2069 if( end - p < 2 )
2070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2071 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2072 p += 2;
2073
2074 if( end - p < ( long int )session->ticket_len )
2075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2076 if( session->ticket_len > 0 )
2077 {
2078 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2079 if( session->ticket == NULL )
2080 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2081 memcpy( session->ticket, p, session->ticket_len );
2082 p += session->ticket_len;
2083 }
2084 }
2085#endif /* MBEDTLS_SSL_CLI_C */
2086
2087 return( 0 );
2088
2089}
2090#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002091MBEDTLS_CHECK_RETURN_CRITICAL
2092static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2093 unsigned char *buf,
2094 size_t buf_len,
2095 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002096{
2097 ((void) session);
2098 ((void) buf);
2099 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002100 *olen = 0;
2101 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002102}
Jerry Yu438ddd82022-07-07 06:55:50 +00002103
2104static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2105 unsigned char *buf,
2106 size_t buf_len )
2107{
2108 ((void) session);
2109 ((void) buf);
2110 ((void) buf_len);
2111 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2112}
2113#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002114#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2115
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002116psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002117 size_t taglen,
2118 psa_algorithm_t *alg,
2119 psa_key_type_t *key_type,
2120 size_t *key_size )
2121{
2122 switch ( mbedtls_cipher_type )
2123 {
2124 case MBEDTLS_CIPHER_AES_128_CBC:
2125 *alg = PSA_ALG_CBC_NO_PADDING;
2126 *key_type = PSA_KEY_TYPE_AES;
2127 *key_size = 128;
2128 break;
2129 case MBEDTLS_CIPHER_AES_128_CCM:
2130 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2131 *key_type = PSA_KEY_TYPE_AES;
2132 *key_size = 128;
2133 break;
2134 case MBEDTLS_CIPHER_AES_128_GCM:
2135 *alg = PSA_ALG_GCM;
2136 *key_type = PSA_KEY_TYPE_AES;
2137 *key_size = 128;
2138 break;
2139 case MBEDTLS_CIPHER_AES_192_CCM:
2140 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2141 *key_type = PSA_KEY_TYPE_AES;
2142 *key_size = 192;
2143 break;
2144 case MBEDTLS_CIPHER_AES_192_GCM:
2145 *alg = PSA_ALG_GCM;
2146 *key_type = PSA_KEY_TYPE_AES;
2147 *key_size = 192;
2148 break;
2149 case MBEDTLS_CIPHER_AES_256_CBC:
2150 *alg = PSA_ALG_CBC_NO_PADDING;
2151 *key_type = PSA_KEY_TYPE_AES;
2152 *key_size = 256;
2153 break;
2154 case MBEDTLS_CIPHER_AES_256_CCM:
2155 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2156 *key_type = PSA_KEY_TYPE_AES;
2157 *key_size = 256;
2158 break;
2159 case MBEDTLS_CIPHER_AES_256_GCM:
2160 *alg = PSA_ALG_GCM;
2161 *key_type = PSA_KEY_TYPE_AES;
2162 *key_size = 256;
2163 break;
2164 case MBEDTLS_CIPHER_ARIA_128_CBC:
2165 *alg = PSA_ALG_CBC_NO_PADDING;
2166 *key_type = PSA_KEY_TYPE_ARIA;
2167 *key_size = 128;
2168 break;
2169 case MBEDTLS_CIPHER_ARIA_128_CCM:
2170 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2171 *key_type = PSA_KEY_TYPE_ARIA;
2172 *key_size = 128;
2173 break;
2174 case MBEDTLS_CIPHER_ARIA_128_GCM:
2175 *alg = PSA_ALG_GCM;
2176 *key_type = PSA_KEY_TYPE_ARIA;
2177 *key_size = 128;
2178 break;
2179 case MBEDTLS_CIPHER_ARIA_192_CCM:
2180 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2181 *key_type = PSA_KEY_TYPE_ARIA;
2182 *key_size = 192;
2183 break;
2184 case MBEDTLS_CIPHER_ARIA_192_GCM:
2185 *alg = PSA_ALG_GCM;
2186 *key_type = PSA_KEY_TYPE_ARIA;
2187 *key_size = 192;
2188 break;
2189 case MBEDTLS_CIPHER_ARIA_256_CBC:
2190 *alg = PSA_ALG_CBC_NO_PADDING;
2191 *key_type = PSA_KEY_TYPE_ARIA;
2192 *key_size = 256;
2193 break;
2194 case MBEDTLS_CIPHER_ARIA_256_CCM:
2195 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2196 *key_type = PSA_KEY_TYPE_ARIA;
2197 *key_size = 256;
2198 break;
2199 case MBEDTLS_CIPHER_ARIA_256_GCM:
2200 *alg = PSA_ALG_GCM;
2201 *key_type = PSA_KEY_TYPE_ARIA;
2202 *key_size = 256;
2203 break;
2204 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2205 *alg = PSA_ALG_CBC_NO_PADDING;
2206 *key_type = PSA_KEY_TYPE_CAMELLIA;
2207 *key_size = 128;
2208 break;
2209 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2210 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2211 *key_type = PSA_KEY_TYPE_CAMELLIA;
2212 *key_size = 128;
2213 break;
2214 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2215 *alg = PSA_ALG_GCM;
2216 *key_type = PSA_KEY_TYPE_CAMELLIA;
2217 *key_size = 128;
2218 break;
2219 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2220 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2221 *key_type = PSA_KEY_TYPE_CAMELLIA;
2222 *key_size = 192;
2223 break;
2224 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2225 *alg = PSA_ALG_GCM;
2226 *key_type = PSA_KEY_TYPE_CAMELLIA;
2227 *key_size = 192;
2228 break;
2229 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2230 *alg = PSA_ALG_CBC_NO_PADDING;
2231 *key_type = PSA_KEY_TYPE_CAMELLIA;
2232 *key_size = 256;
2233 break;
2234 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2235 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2236 *key_type = PSA_KEY_TYPE_CAMELLIA;
2237 *key_size = 256;
2238 break;
2239 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2240 *alg = PSA_ALG_GCM;
2241 *key_type = PSA_KEY_TYPE_CAMELLIA;
2242 *key_size = 256;
2243 break;
2244 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2245 *alg = PSA_ALG_CHACHA20_POLY1305;
2246 *key_type = PSA_KEY_TYPE_CHACHA20;
2247 *key_size = 256;
2248 break;
2249 case MBEDTLS_CIPHER_NULL:
2250 *alg = MBEDTLS_SSL_NULL_CIPHER;
2251 *key_type = 0;
2252 *key_size = 0;
2253 break;
2254 default:
2255 return PSA_ERROR_NOT_SUPPORTED;
2256 }
2257
2258 return PSA_SUCCESS;
2259}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002260#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002261
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002262#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002263int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2264 const unsigned char *dhm_P, size_t P_len,
2265 const unsigned char *dhm_G, size_t G_len )
2266{
Janos Follath865b3eb2019-12-16 11:46:15 +00002267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002268
Glenn Strausscee11292021-12-20 01:43:17 -05002269 mbedtls_mpi_free( &conf->dhm_P );
2270 mbedtls_mpi_free( &conf->dhm_G );
2271
Hanno Beckera90658f2017-10-04 15:29:08 +01002272 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2273 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2274 {
2275 mbedtls_mpi_free( &conf->dhm_P );
2276 mbedtls_mpi_free( &conf->dhm_G );
2277 return( ret );
2278 }
2279
2280 return( 0 );
2281}
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002283int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002284{
Janos Follath865b3eb2019-12-16 11:46:15 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002286
Glenn Strausscee11292021-12-20 01:43:17 -05002287 mbedtls_mpi_free( &conf->dhm_P );
2288 mbedtls_mpi_free( &conf->dhm_G );
2289
Gilles Peskinee5702482021-06-11 21:59:08 +02002290 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2291 &conf->dhm_P ) ) != 0 ||
2292 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2293 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002294 {
2295 mbedtls_mpi_free( &conf->dhm_P );
2296 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002297 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002298 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002299
2300 return( 0 );
2301}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002302#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002303
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002304#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2305/*
2306 * Set the minimum length for Diffie-Hellman parameters
2307 */
2308void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2309 unsigned int bitlen )
2310{
2311 conf->dhm_min_bitlen = bitlen;
2312}
2313#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2314
Gilles Peskineeccd8882020-03-10 12:19:08 +01002315#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002316#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002317/*
2318 * Set allowed/preferred hashes for handshake signatures
2319 */
2320void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2321 const int *hashes )
2322{
2323 conf->sig_hashes = hashes;
2324}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002325#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002326
Jerry Yuf017ee42022-01-12 15:49:48 +08002327/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002328void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2329 const uint16_t* sig_algs )
2330{
Jerry Yuf017ee42022-01-12 15:49:48 +08002331#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2332 conf->sig_hashes = NULL;
2333#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2334 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002335}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002336#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002337
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002338#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002339#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002340/*
2341 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002342 *
2343 * mbedtls_ssl_setup() takes the provided list
2344 * and translates it to a list of IANA TLS group identifiers,
2345 * stored in ssl->handshake->group_list.
2346 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002347 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002348void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002349 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002351 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002352 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002353}
Brett Warrene0edc842021-08-17 09:53:13 +01002354#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002355#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002356
Brett Warrene0edc842021-08-17 09:53:13 +01002357/*
2358 * Set the allowed groups
2359 */
2360void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2361 const uint16_t *group_list )
2362{
2363#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2364 conf->curve_list = NULL;
2365#endif
2366 conf->group_list = group_list;
2367}
2368
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002369#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002371{
Hanno Becker947194e2017-04-07 13:25:49 +01002372 /* Initialize to suppress unnecessary compiler warning */
2373 size_t hostname_len = 0;
2374
2375 /* Check if new hostname is valid before
2376 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002377 if( hostname != NULL )
2378 {
2379 hostname_len = strlen( hostname );
2380
2381 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2383 }
2384
2385 /* Now it's clear that we will overwrite the old hostname,
2386 * so we can free it safely */
2387
2388 if( ssl->hostname != NULL )
2389 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002390 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002391 mbedtls_free( ssl->hostname );
2392 }
2393
2394 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002397 {
2398 ssl->hostname = NULL;
2399 }
2400 else
2401 {
2402 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002403 if( ssl->hostname == NULL )
2404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002405
Hanno Becker947194e2017-04-07 13:25:49 +01002406 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002407
Hanno Becker947194e2017-04-07 13:25:49 +01002408 ssl->hostname[hostname_len] = '\0';
2409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 return( 0 );
2412}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002415#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002416void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002418 const unsigned char *, size_t),
2419 void *p_sni )
2420{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002421 conf->f_sni = f_sni;
2422 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002427int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002428{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002429 size_t cur_len, tot_len;
2430 const char **p;
2431
2432 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002433 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2434 * MUST NOT be truncated."
2435 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002436 */
2437 tot_len = 0;
2438 for( p = protos; *p != NULL; p++ )
2439 {
2440 cur_len = strlen( *p );
2441 tot_len += cur_len;
2442
Ronald Cron8216dd32020-04-23 16:41:44 +02002443 if( ( cur_len == 0 ) ||
2444 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2445 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002447 }
2448
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002449 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002450
2451 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002452}
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002455{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002456 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002459
Johan Pascalb62bb512015-12-03 21:56:45 +01002460#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002461void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2462 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002463{
2464 conf->dtls_srtp_mki_support = support_mki_value;
2465}
2466
Ron Eldoref72faf2018-07-12 11:54:20 +03002467int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2468 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002469 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002470{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002471 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002472 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002474 }
Ron Eldor591f1622018-01-22 12:30:04 +02002475
2476 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002477 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002478 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002479 }
Ron Eldor591f1622018-01-22 12:30:04 +02002480
2481 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2482 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002483 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002484}
2485
Ron Eldoref72faf2018-07-12 11:54:20 +03002486int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002487 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002488{
Johan Pascal253d0262020-09-22 13:04:45 +02002489 const mbedtls_ssl_srtp_profile *p;
2490 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002491
Johan Pascal253d0262020-09-22 13:04:45 +02002492 /* check the profiles list: all entry must be valid,
2493 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002494 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2495 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2496 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002497 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002498 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002499 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002500 list_size++;
2501 }
2502 else
2503 {
2504 /* unsupported value, stop parsing and set the size to an error value */
2505 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002506 }
2507 }
2508
Johan Pascal5ef72d22020-10-28 17:05:47 +01002509 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002510 {
Johan Pascal253d0262020-09-22 13:04:45 +02002511 conf->dtls_srtp_profile_list = NULL;
2512 conf->dtls_srtp_profile_list_len = 0;
2513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2514 }
2515
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002516 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002517 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002518
2519 return( 0 );
2520}
2521
Johan Pascal5ef72d22020-10-28 17:05:47 +01002522void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2523 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002524{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002525 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2526 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002527 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002528 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002529 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002530 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002531 else
2532 {
2533 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002534 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2535 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002536 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002537}
Johan Pascalb62bb512015-12-03 21:56:45 +01002538#endif /* MBEDTLS_SSL_DTLS_SRTP */
2539
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302540#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002541void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002542{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002543 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002544}
2545
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002546void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002547{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002548 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002549}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302550#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002551
Janos Follath088ce432017-04-10 12:42:31 +01002552#if defined(MBEDTLS_SSL_SRV_C)
2553void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2554 char cert_req_ca_list )
2555{
2556 conf->cert_req_ca_list = cert_req_ca_list;
2557}
2558#endif
2559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002561void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002563 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002564}
2565#endif
2566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002568void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002569{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002570 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002571}
2572#endif
2573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002575int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002576{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002578 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002581 }
2582
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002583 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002584
2585 return( 0 );
2586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002588
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002589void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002590{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002591 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002592}
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002595void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002596{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002597 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002598}
2599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002600void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002602 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002603}
2604
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002605void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002606 const unsigned char period[8] )
2607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002608 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002609}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002613#if defined(MBEDTLS_SSL_CLI_C)
2614void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002615{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002616 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002617}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002618#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002619
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002620#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002621
Jerry Yud0766ec2022-09-22 10:46:57 +08002622#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002623void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2624 uint16_t num_tickets )
2625{
Jerry Yud0766ec2022-09-22 10:46:57 +08002626 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002627}
2628#endif
2629
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002630void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2631 mbedtls_ssl_ticket_write_t *f_ticket_write,
2632 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2633 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002634{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002635 conf->f_ticket_write = f_ticket_write;
2636 conf->f_ticket_parse = f_ticket_parse;
2637 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002638}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002639#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002641
Hanno Becker7e6c1782021-06-08 09:24:55 +01002642void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2643 mbedtls_ssl_export_keys_t *f_export_keys,
2644 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002645{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002646 ssl->f_export_keys = f_export_keys;
2647 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002648}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002649
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002650#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002651void mbedtls_ssl_conf_async_private_cb(
2652 mbedtls_ssl_config *conf,
2653 mbedtls_ssl_async_sign_t *f_async_sign,
2654 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2655 mbedtls_ssl_async_resume_t *f_async_resume,
2656 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002657 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002658{
2659 conf->f_async_sign_start = f_async_sign;
2660 conf->f_async_decrypt_start = f_async_decrypt;
2661 conf->f_async_resume = f_async_resume;
2662 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002663 conf->p_async_config_data = async_config_data;
2664}
2665
Gilles Peskine8f97af72018-04-26 11:46:10 +02002666void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2667{
2668 return( conf->p_async_config_data );
2669}
2670
Gilles Peskine1febfef2018-04-30 11:54:39 +02002671void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002672{
2673 if( ssl->handshake == NULL )
2674 return( NULL );
2675 else
2676 return( ssl->handshake->user_async_ctx );
2677}
2678
Gilles Peskine1febfef2018-04-30 11:54:39 +02002679void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002680 void *ctx )
2681{
2682 if( ssl->handshake != NULL )
2683 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002684}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002685#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687/*
2688 * SSL get accessors
2689 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002690uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002691{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002692 if( ssl->session != NULL )
2693 return( ssl->session->verify_result );
2694
2695 if( ssl->session_negotiate != NULL )
2696 return( ssl->session_negotiate->verify_result );
2697
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002698 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699}
2700
Glenn Strauss8f526902022-01-13 00:04:49 -05002701int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2702{
2703 if( ssl == NULL || ssl->session == NULL )
2704 return( 0 );
2705
2706 return( ssl->session->ciphersuite );
2707}
2708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002710{
Paul Bakker926c8e42013-03-06 10:23:34 +01002711 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002712 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002715}
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002720 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002721 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002722 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002723 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002724 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002725 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002726 default:
2727 return( "unknown (DTLS)" );
2728 }
2729 }
2730#endif
2731
Glenn Strauss60bfe602022-03-14 19:04:24 -04002732 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002733 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002734 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002735 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002736 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002737 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002738 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002739 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002740 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002741}
2742
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002743#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002744size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2745{
David Horstmann95d516f2021-05-04 18:36:56 +01002746 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002747 size_t read_mfl;
2748
2749 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2750 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2751 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2752 {
2753 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2754 }
2755
2756 /* Check if a smaller max length was negotiated */
2757 if( ssl->session_out != NULL )
2758 {
2759 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2760 if( read_mfl < max_len )
2761 {
2762 max_len = read_mfl;
2763 }
2764 }
2765
2766 // During a handshake, use the value being negotiated
2767 if( ssl->session_negotiate != NULL )
2768 {
2769 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2770 if( read_mfl < max_len )
2771 {
2772 max_len = read_mfl;
2773 }
2774 }
2775
2776 return( max_len );
2777}
2778
2779size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002780{
2781 size_t max_len;
2782
2783 /*
2784 * Assume mfl_code is correct since it was checked when set
2785 */
Angus Grattond8213d02016-05-25 20:56:48 +10002786 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002787
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002788 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002789 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002790 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002791 {
Angus Grattond8213d02016-05-25 20:56:48 +10002792 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002793 }
2794
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002795 /* During a handshake, use the value being negotiated */
2796 if( ssl->session_negotiate != NULL &&
2797 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2798 {
2799 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2800 }
2801
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002802 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002803}
2804#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2805
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002807size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002808{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002809 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2811 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2812 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2813 return ( 0 );
2814
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002815 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2816 return( ssl->mtu );
2817
2818 if( ssl->mtu == 0 )
2819 return( ssl->handshake->mtu );
2820
2821 return( ssl->mtu < ssl->handshake->mtu ?
2822 ssl->mtu : ssl->handshake->mtu );
2823}
2824#endif /* MBEDTLS_SSL_PROTO_DTLS */
2825
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002826int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2827{
2828 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2829
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002830#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2831 !defined(MBEDTLS_SSL_PROTO_DTLS)
2832 (void) ssl;
2833#endif
2834
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002835#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002836 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002837
2838 if( max_len > mfl )
2839 max_len = mfl;
2840#endif
2841
2842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002843 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002844 {
Hanno Becker89490712020-02-05 10:50:12 +00002845 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002846 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2847 const size_t overhead = (size_t) ret;
2848
2849 if( ret < 0 )
2850 return( ret );
2851
2852 if( mtu <= overhead )
2853 {
2854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2855 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2856 }
2857
2858 if( max_len > mtu - overhead )
2859 max_len = mtu - overhead;
2860 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002861#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002862
Hanno Becker0defedb2018-08-10 12:35:02 +01002863#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2864 !defined(MBEDTLS_SSL_PROTO_DTLS)
2865 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002866#endif
2867
2868 return( (int) max_len );
2869}
2870
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002871int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2872{
2873 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2874
2875#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2876 (void) ssl;
2877#endif
2878
2879#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2880 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2881
2882 if( max_len > mfl )
2883 max_len = mfl;
2884#endif
2885
2886 return( (int) max_len );
2887}
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#if defined(MBEDTLS_X509_CRT_PARSE_C)
2890const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002891{
2892 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002893 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002894
Hanno Beckere6824572019-02-07 13:18:46 +00002895#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002896 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002897#else
2898 return( NULL );
2899#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002904int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2905 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002906{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002907 int ret;
2908
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002909 if( ssl == NULL ||
2910 dst == NULL ||
2911 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002912 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002915 }
2916
Hanno Beckere810bbc2021-05-14 16:01:05 +01002917 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2918 * idempotent: Each session can only be exported once.
2919 *
2920 * (This is in preparation for TLS 1.3 support where we will
2921 * need the ability to export multiple sessions (aka tickets),
2922 * which will be achieved by calling mbedtls_ssl_get_session()
2923 * multiple times until it fails.)
2924 *
2925 * Check whether we have already exported the current session,
2926 * and fail if so.
2927 */
2928 if( ssl->session->exported == 1 )
2929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2930
2931 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2932 if( ret != 0 )
2933 return( ret );
2934
2935 /* Remember that we've exported the session. */
2936 ssl->session->exported = 1;
2937 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002940
Paul Bakker5121ce52009-01-03 21:22:43 +00002941/*
Hanno Beckera835da52019-05-16 12:39:07 +01002942 * Define ticket header determining Mbed TLS version
2943 * and structure of the ticket.
2944 */
2945
Hanno Becker94ef3b32019-05-16 12:50:45 +01002946/*
Hanno Becker50b59662019-05-28 14:30:45 +01002947 * Define bitflag determining compile-time settings influencing
2948 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002949 */
2950
Hanno Becker50b59662019-05-28 14:30:45 +01002951#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002952#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002953#else
Hanno Becker3e088662019-05-29 11:10:18 +01002954#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002955#endif /* MBEDTLS_HAVE_TIME */
2956
2957#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002958#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002959#else
Hanno Becker3e088662019-05-29 11:10:18 +01002960#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002961#endif /* MBEDTLS_X509_CRT_PARSE_C */
2962
2963#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002964#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002965#else
Hanno Becker3e088662019-05-29 11:10:18 +01002966#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002967#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2968
2969#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002970#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002971#else
Hanno Becker3e088662019-05-29 11:10:18 +01002972#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002973#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2974
Hanno Becker94ef3b32019-05-16 12:50:45 +01002975#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002976#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002977#else
Hanno Becker3e088662019-05-29 11:10:18 +01002978#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2980
Hanno Becker94ef3b32019-05-16 12:50:45 +01002981#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2982#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2983#else
2984#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2985#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2986
Hanno Becker3e088662019-05-29 11:10:18 +01002987#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2988#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2989#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2990#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002991#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2992#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002993
Hanno Becker50b59662019-05-28 14:30:45 +01002994#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002995 ( (uint16_t) ( \
2996 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2997 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2998 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2999 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003000 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003001 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003002
Hanno Beckerf8787072019-05-16 12:41:07 +01003003static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003004 MBEDTLS_VERSION_MAJOR,
3005 MBEDTLS_VERSION_MINOR,
3006 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003007 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3008 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003009};
Hanno Beckera835da52019-05-16 12:39:07 +01003010
3011/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003012 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003013 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003014 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003015 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003016 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003017 * opaque mbedtls_version[3]; // library version: major, minor, patch
3018 * opaque session_format[2]; // library-version specific 16-bit field
3019 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003020 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003021 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003022 * Note: When updating the format, remember to keep
3023 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003024 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003025 * // In this version, `session_format` determines
3026 * // the setting of those compile-time
3027 * // configuration options which influence
3028 * // the structure of mbedtls_ssl_session.
3029 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003030 * uint8_t minor_ver; // Protocol minor version. Possible values:
3031 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003032 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003033 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003034 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003035 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003036 * serialized_session_tls12 data;
3037 *
3038 * };
3039 *
3040 * } serialized_session;
3041 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003042 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003043
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003044MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003045static int ssl_session_save( const mbedtls_ssl_session *session,
3046 unsigned char omit_header,
3047 unsigned char *buf,
3048 size_t buf_len,
3049 size_t *olen )
3050{
3051 unsigned char *p = buf;
3052 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003053 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003054#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3055 size_t out_len;
3056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3057#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003058 if( session == NULL )
3059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3060
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003061 if( !omit_header )
3062 {
3063 /*
3064 * Add Mbed TLS version identifier
3065 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003066 used += sizeof( ssl_serialized_session_header );
3067
3068 if( used <= buf_len )
3069 {
3070 memcpy( p, ssl_serialized_session_header,
3071 sizeof( ssl_serialized_session_header ) );
3072 p += sizeof( ssl_serialized_session_header );
3073 }
3074 }
3075
3076 /*
3077 * TLS version identifier
3078 */
3079 used += 1;
3080 if( used <= buf_len )
3081 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003082 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003083 }
3084
3085 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003086 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003087 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003088 {
3089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003090 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003091 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003092 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003093#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3094
Jerry Yu251a12e2022-07-13 15:15:48 +08003095#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3096 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003097 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3098 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003099 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003100 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003101 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003102#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3103
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003104 default:
3105 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3106 }
3107
3108 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003109 if( used > buf_len )
3110 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003111
3112 return( 0 );
3113}
3114
3115/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003116 * Public wrapper for ssl_session_save()
3117 */
3118int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3119 unsigned char *buf,
3120 size_t buf_len,
3121 size_t *olen )
3122{
3123 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3124}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003125
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003126/*
3127 * Deserialize session, see mbedtls_ssl_session_save() for format.
3128 *
3129 * This internal version is wrapped by a public function that cleans up in
3130 * case of error, and has an extra option omit_header.
3131 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003132MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003133static int ssl_session_load( mbedtls_ssl_session *session,
3134 unsigned char omit_header,
3135 const unsigned char *buf,
3136 size_t len )
3137{
3138 const unsigned char *p = buf;
3139 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003140 size_t remaining_len;
3141
3142
3143 if( session == NULL )
3144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003145
3146 if( !omit_header )
3147 {
3148 /*
3149 * Check Mbed TLS version identifier
3150 */
3151
3152 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3154
3155 if( memcmp( p, ssl_serialized_session_header,
3156 sizeof( ssl_serialized_session_header ) ) != 0 )
3157 {
3158 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3159 }
3160 p += sizeof( ssl_serialized_session_header );
3161 }
3162
3163 /*
3164 * TLS version identifier
3165 */
3166 if( 1 > (size_t)( end - p ) )
3167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003168 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003169
3170 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003171 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003172 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003173 {
3174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003175 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003176 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003177#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3178
Jerry Yu438ddd82022-07-07 06:55:50 +00003179#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3180 case MBEDTLS_SSL_VERSION_TLS1_3:
3181 return( ssl_tls13_session_load( session, p, remaining_len ) );
3182#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3183
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003184 default:
3185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3186 }
3187}
3188
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003189/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003190 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003191 */
3192int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3193 const unsigned char *buf,
3194 size_t len )
3195{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003196 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003197
3198 if( ret != 0 )
3199 mbedtls_ssl_session_free( session );
3200
3201 return( ret );
3202}
3203
3204/*
Paul Bakker1961b702013-01-25 14:49:24 +01003205 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003207MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003208static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3209{
3210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3211
Ronald Cron66dbf912022-02-02 15:33:46 +01003212 /*
3213 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003214 * that were written into the output buffer by the previous handshake step,
3215 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003216 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3217 * We proceed to the next handshake step only when all data from the
3218 * previous one have been sent to the peer, thus we make sure that this is
3219 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3220 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3221 * we have to wait before to go ahead.
3222 * In the case of TLS 1.3, handshake step handlers do not send data to the
3223 * peer. Data are only sent here and through
3224 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003225 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003226 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003227 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3228 return( ret );
3229
3230#if defined(MBEDTLS_SSL_PROTO_DTLS)
3231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3232 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3233 {
3234 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3235 return( ret );
3236 }
3237#endif /* MBEDTLS_SSL_PROTO_DTLS */
3238
3239 return( ret );
3240}
3241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003243{
Hanno Becker41934dd2021-08-07 19:13:43 +01003244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
Hanno Becker41934dd2021-08-07 19:13:43 +01003246 if( ssl == NULL ||
3247 ssl->conf == NULL ||
3248 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003249 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003250 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003252 }
3253
3254 ret = ssl_prepare_handshake_step( ssl );
3255 if( ret != 0 )
3256 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003257
Jerry Yue7047812021-09-13 19:26:39 +08003258 ret = mbedtls_ssl_handle_pending_alert( ssl );
3259 if( ret != 0 )
3260 goto cleanup;
3261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003263 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003264 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3266 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003267
Ronald Cron9f0fba32022-02-10 16:45:15 +01003268 switch( ssl->state )
3269 {
3270 case MBEDTLS_SSL_HELLO_REQUEST:
3271 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3272 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003273
Ronald Cron9f0fba32022-02-10 16:45:15 +01003274 case MBEDTLS_SSL_CLIENT_HELLO:
3275 ret = mbedtls_ssl_write_client_hello( ssl );
3276 break;
3277
3278 default:
3279#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003280 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003281 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3282 else
3283 ret = mbedtls_ssl_handshake_client_step( ssl );
3284#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3285 ret = mbedtls_ssl_handshake_client_step( ssl );
3286#else
3287 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3288#endif
3289 }
Jerry Yub9930e72021-08-06 17:11:51 +08003290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003294 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003295#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003296 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003297 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003299
3300#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3301 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3302 ret = mbedtls_ssl_handshake_server_step( ssl );
3303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003305#endif
3306
Jerry Yue7047812021-09-13 19:26:39 +08003307 if( ret != 0 )
3308 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003309 /* handshake_step return error. And it is same
3310 * with alert_reason.
3311 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003312 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003313 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003314 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003315 goto cleanup;
3316 }
3317 }
3318
3319cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003320 return( ret );
3321}
3322
3323/*
3324 * Perform the SSL handshake
3325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003327{
3328 int ret = 0;
3329
Hanno Beckera817ea42020-10-20 15:20:23 +01003330 /* Sanity checks */
3331
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003332 if( ssl == NULL || ssl->conf == NULL )
3333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3334
Hanno Beckera817ea42020-10-20 15:20:23 +01003335#if defined(MBEDTLS_SSL_PROTO_DTLS)
3336 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3337 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3338 {
3339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3340 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3342 }
3343#endif /* MBEDTLS_SSL_PROTO_DTLS */
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003346
Hanno Beckera817ea42020-10-20 15:20:23 +01003347 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003348 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003351
3352 if( ret != 0 )
3353 break;
3354 }
3355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 return( ret );
3359}
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_RENEGOTIATION)
3362#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003363/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003364 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003365 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003366MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003368{
Janos Follath865b3eb2019-12-16 11:46:15 +00003369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003372
3373 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3375 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003376
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003377 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003378 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003380 return( ret );
3381 }
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003384
3385 return( 0 );
3386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003388
3389/*
3390 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 * - any side: calling mbedtls_ssl_renegotiate(),
3392 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3393 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003394 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003395 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003397 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003398int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003399{
Janos Follath865b3eb2019-12-16 11:46:15 +00003400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003403
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003404 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3405 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003406
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003407 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3408 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003412 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003414 ssl->handshake->out_msg_seq = 1;
3415 else
3416 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003417 }
3418#endif
3419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3421 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003426 return( ret );
3427 }
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003430
3431 return( 0 );
3432}
3433
3434/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003435 * Renegotiate current connection on client,
3436 * or request renegotiation on server
3437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003441
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003442 if( ssl == NULL || ssl->conf == NULL )
3443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003446 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003447 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003448 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003449 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003453
3454 /* Did we already try/start sending HelloRequest? */
3455 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003457
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003458 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003463 /*
3464 * On client, either start the renegotiation process or,
3465 * if already in progress, continue the handshake
3466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003468 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003469 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003471
Hanno Becker40cdaa12020-02-05 10:48:27 +00003472 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475 return( ret );
3476 }
3477 }
3478 else
3479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003483 return( ret );
3484 }
3485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003487
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003488 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003491
Gilles Peskine9b562d52018-04-25 20:32:43 +02003492void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003493{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003494 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3495
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003496 if( handshake == NULL )
3497 return;
3498
Brett Warrene0edc842021-08-17 09:53:13 +01003499#if defined(MBEDTLS_ECP_C)
3500#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3501 if ( ssl->handshake->group_list_heap_allocated )
3502 mbedtls_free( (void*) handshake->group_list );
3503 handshake->group_list = NULL;
3504#endif /* MBEDTLS_DEPRECATED_REMOVED */
3505#endif /* MBEDTLS_ECP_C */
3506
Jerry Yuf017ee42022-01-12 15:49:48 +08003507#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3508#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3509 if ( ssl->handshake->sig_algs_heap_allocated )
3510 mbedtls_free( (void*) handshake->sig_algs );
3511 handshake->sig_algs = NULL;
3512#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003513#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3514 if( ssl->handshake->certificate_request_context )
3515 {
3516 mbedtls_free( (void*) handshake->certificate_request_context );
3517 }
3518#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003519#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3520
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003521#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3522 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3523 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003524 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003525 handshake->async_in_progress = 0;
3526 }
3527#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3528
Andrzej Kurek25f27152022-08-17 16:09:31 -04003529#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003530#if defined(MBEDTLS_USE_PSA_CRYPTO)
3531 psa_hash_abort( &handshake->fin_sha256_psa );
3532#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003533 mbedtls_sha256_free( &handshake->fin_sha256 );
3534#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003535#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003536#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003538 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003539#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003540 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003541#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003542#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#if defined(MBEDTLS_DHM_C)
3545 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003546#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003547#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003549#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003550#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003551 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003552#if defined(MBEDTLS_SSL_CLI_C)
3553 mbedtls_free( handshake->ecjpake_cache );
3554 handshake->ecjpake_cache = NULL;
3555 handshake->ecjpake_cache_len = 0;
3556#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003557#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003558
Janos Follath4ae5c292016-02-10 11:27:43 +00003559#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3560 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003561 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003563#endif
3564
Gilles Peskineeccd8882020-03-10 12:19:08 +01003565#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003566#if defined(MBEDTLS_USE_PSA_CRYPTO)
3567 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3568 {
3569 /* The maintenance of the external PSK key slot is the
3570 * user's responsibility. */
3571 if( ssl->handshake->psk_opaque_is_internal )
3572 {
3573 psa_destroy_key( ssl->handshake->psk_opaque );
3574 ssl->handshake->psk_opaque_is_internal = 0;
3575 }
3576 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3577 }
Neil Armstronge952a302022-05-03 10:22:14 +02003578#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003579 if( handshake->psk != NULL )
3580 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003581 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003582 mbedtls_free( handshake->psk );
3583 }
Neil Armstronge952a302022-05-03 10:22:14 +02003584#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003585#endif
3586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3588 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003589 /*
3590 * Free only the linked list wrapper, not the keys themselves
3591 * since the belong to the SNI callback
3592 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003593 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003595
Gilles Peskineeccd8882020-03-10 12:19:08 +01003596#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003597 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003598 if( handshake->ecrs_peer_cert != NULL )
3599 {
3600 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3601 mbedtls_free( handshake->ecrs_peer_cert );
3602 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003603#endif
3604
Hanno Becker75173122019-02-06 16:18:31 +00003605#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3606 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3607 mbedtls_pk_free( &handshake->peer_pubkey );
3608#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3609
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003610#if defined(MBEDTLS_SSL_CLI_C) && \
3611 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3612 mbedtls_free( handshake->cookie );
3613#endif /* MBEDTLS_SSL_CLI_C &&
3614 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003615
3616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003617 mbedtls_ssl_flight_free( handshake->flight );
3618 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003619#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003620
Ronald Cronf12b81d2022-03-15 10:42:41 +01003621#if defined(MBEDTLS_ECDH_C) && \
3622 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003623 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003624 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003625#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3626
Ronald Cron6f135e12021-12-08 16:57:54 +01003627#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003628 mbedtls_ssl_transform_free( handshake->transform_handshake );
3629 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003630 mbedtls_free( handshake->transform_earlydata );
3631 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003632#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003633
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003634
3635#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3636 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3637 * processes datagrams and the fact that a datagram is allowed to have
3638 * several records in it, it is possible that the I/O buffers are not
3639 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003640 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3641 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003642#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003643
Jerry Yuba9c7272021-10-30 11:54:10 +08003644 /* mbedtls_platform_zeroize MUST be last one in this function */
3645 mbedtls_platform_zeroize( handshake,
3646 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003647}
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003650{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003651 if( session == NULL )
3652 return;
3653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003655 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003656#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003657
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003658#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003660#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003661
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003662 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003663}
3664
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003665#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003666
3667#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3668#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3669#else
3670#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3671#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3672
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003673#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003674
3675#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3676#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3677#else
3678#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3679#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3680
3681#if defined(MBEDTLS_SSL_ALPN)
3682#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3683#else
3684#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3685#endif /* MBEDTLS_SSL_ALPN */
3686
3687#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3688#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3689#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3691
3692#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3693 ( (uint32_t) ( \
3694 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3695 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3696 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3697 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3698 0u ) )
3699
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003700static unsigned char ssl_serialized_context_header[] = {
3701 MBEDTLS_VERSION_MAJOR,
3702 MBEDTLS_VERSION_MINOR,
3703 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003704 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3705 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3706 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3707 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3708 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003709};
3710
Paul Bakker5121ce52009-01-03 21:22:43 +00003711/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003712 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003713 *
3714 * The format of the serialized data is:
3715 * (in the presentation language of TLS, RFC 8446 section 3)
3716 *
3717 * // header
3718 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003719 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003720 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003721 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003722 * Note: When updating the format, remember to keep these
3723 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003724 *
3725 * // session sub-structure
3726 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3727 * // transform sub-structure
3728 * uint8 random[64]; // ServerHello.random+ClientHello.random
3729 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3730 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3731 * // fields from ssl_context
3732 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3733 * uint64 in_window_top; // DTLS: last validated record seq_num
3734 * uint64 in_window; // DTLS: bitmask for replay protection
3735 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3736 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3737 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3738 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3739 *
3740 * Note that many fields of the ssl_context or sub-structures are not
3741 * serialized, as they fall in one of the following categories:
3742 *
3743 * 1. forced value (eg in_left must be 0)
3744 * 2. pointer to dynamically-allocated memory (eg session, transform)
3745 * 3. value can be re-derived from other data (eg session keys from MS)
3746 * 4. value was temporary (eg content of input buffer)
3747 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003748 */
3749int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3750 unsigned char *buf,
3751 size_t buf_len,
3752 size_t *olen )
3753{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003754 unsigned char *p = buf;
3755 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003756 size_t session_len;
3757 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003758
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003759 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003760 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3761 * this function's documentation.
3762 *
3763 * These are due to assumptions/limitations in the implementation. Some of
3764 * them are likely to stay (no handshake in progress) some might go away
3765 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003766 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003767 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003768 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003769 {
3770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003772 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003773 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003774 {
3775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003777 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003778 /* Double-check that sub-structures are indeed ready */
3779 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003780 {
3781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003783 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003784 /* There must be no pending incoming or outgoing data */
3785 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003789 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003790 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003791 {
3792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003794 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003795 /* Protocol must be DLTS, not TLS */
3796 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003797 {
3798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003800 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003801 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003802 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003806 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003807 /* We must be using an AEAD ciphersuite */
3808 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003809 {
3810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003812 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813 /* Renegotiation must not be enabled */
3814#if defined(MBEDTLS_SSL_RENEGOTIATION)
3815 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003816 {
3817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003819 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003820#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003821
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003822 /*
3823 * Version and format identifier
3824 */
3825 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003826
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003827 if( used <= buf_len )
3828 {
3829 memcpy( p, ssl_serialized_context_header,
3830 sizeof( ssl_serialized_context_header ) );
3831 p += sizeof( ssl_serialized_context_header );
3832 }
3833
3834 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003835 * Session (length + data)
3836 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003837 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003838 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3839 return( ret );
3840
3841 used += 4 + session_len;
3842 if( used <= buf_len )
3843 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003844 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3845 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003846
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003847 ret = ssl_session_save( ssl->session, 1,
3848 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003849 if( ret != 0 )
3850 return( ret );
3851
3852 p += session_len;
3853 }
3854
3855 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003856 * Transform
3857 */
3858 used += sizeof( ssl->transform->randbytes );
3859 if( used <= buf_len )
3860 {
3861 memcpy( p, ssl->transform->randbytes,
3862 sizeof( ssl->transform->randbytes ) );
3863 p += sizeof( ssl->transform->randbytes );
3864 }
3865
3866#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3867 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3868 if( used <= buf_len )
3869 {
3870 *p++ = ssl->transform->in_cid_len;
3871 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3872 p += ssl->transform->in_cid_len;
3873
3874 *p++ = ssl->transform->out_cid_len;
3875 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3876 p += ssl->transform->out_cid_len;
3877 }
3878#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3879
3880 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003881 * Saved fields from top-level ssl_context structure
3882 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003883 used += 4;
3884 if( used <= buf_len )
3885 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003886 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3887 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003888 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003889
3890#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3891 used += 16;
3892 if( used <= buf_len )
3893 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003894 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3895 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003896
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003897 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3898 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003899 }
3900#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3901
3902#if defined(MBEDTLS_SSL_PROTO_DTLS)
3903 used += 1;
3904 if( used <= buf_len )
3905 {
3906 *p++ = ssl->disable_datagram_packing;
3907 }
3908#endif /* MBEDTLS_SSL_PROTO_DTLS */
3909
Jerry Yuae0b2e22021-10-08 15:21:19 +08003910 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003911 if( used <= buf_len )
3912 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003913 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3914 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003915 }
3916
3917#if defined(MBEDTLS_SSL_PROTO_DTLS)
3918 used += 2;
3919 if( used <= buf_len )
3920 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003921 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3922 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003923 }
3924#endif /* MBEDTLS_SSL_PROTO_DTLS */
3925
3926#if defined(MBEDTLS_SSL_ALPN)
3927 {
3928 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003929 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003930 : 0;
3931
3932 used += 1 + alpn_len;
3933 if( used <= buf_len )
3934 {
3935 *p++ = alpn_len;
3936
3937 if( ssl->alpn_chosen != NULL )
3938 {
3939 memcpy( p, ssl->alpn_chosen, alpn_len );
3940 p += alpn_len;
3941 }
3942 }
3943 }
3944#endif /* MBEDTLS_SSL_ALPN */
3945
3946 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003947 * Done
3948 */
3949 *olen = used;
3950
3951 if( used > buf_len )
3952 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003953
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003954 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3955
Hanno Becker43aefe22020-02-05 10:44:56 +00003956 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003957}
3958
3959/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003960 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003961 *
3962 * This internal version is wrapped by a public function that cleans up in
3963 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003964 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003965MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003966static int ssl_context_load( mbedtls_ssl_context *ssl,
3967 const unsigned char *buf,
3968 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003969{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003970 const unsigned char *p = buf;
3971 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003972 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003974
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003975 /*
3976 * The context should have been freshly setup or reset.
3977 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003978 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003979 * renegotiating, or if the user mistakenly loaded a session first.)
3980 */
3981 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3982 ssl->session != NULL )
3983 {
3984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3985 }
3986
3987 /*
3988 * We can't check that the config matches the initial one, but we can at
3989 * least check it matches the requirements for serializing.
3990 */
3991 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003992 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3993 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003995 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003996#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003997 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003998 {
3999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4000 }
4001
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004002 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4003
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004004 /*
4005 * Check version identifier
4006 */
4007 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4009
4010 if( memcmp( p, ssl_serialized_context_header,
4011 sizeof( ssl_serialized_context_header ) ) != 0 )
4012 {
4013 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4014 }
4015 p += sizeof( ssl_serialized_context_header );
4016
4017 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004018 * Session
4019 */
4020 if( (size_t)( end - p ) < 4 )
4021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4022
4023 session_len = ( (size_t) p[0] << 24 ) |
4024 ( (size_t) p[1] << 16 ) |
4025 ( (size_t) p[2] << 8 ) |
4026 ( (size_t) p[3] );
4027 p += 4;
4028
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004029 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004030 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004031 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004032 ssl->session_in = ssl->session;
4033 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004034 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004035
4036 if( (size_t)( end - p ) < session_len )
4037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4038
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004039 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004040 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004041 {
4042 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004044 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004045
4046 p += session_len;
4047
4048 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004049 * Transform
4050 */
4051
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004052 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004053 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004054 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004055 ssl->transform_in = ssl->transform;
4056 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004057 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004058
4059 /* Read random bytes and populate structure */
4060 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004063 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004064 ssl->session->ciphersuite,
4065 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004066#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004067 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004068#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004069 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4070 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004071 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004072 ssl->conf->endpoint,
4073 ssl );
4074 if( ret != 0 )
4075 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004076#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004077 p += sizeof( ssl->transform->randbytes );
4078
4079#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4080 /* Read connection IDs and store them */
4081 if( (size_t)( end - p ) < 1 )
4082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4083
4084 ssl->transform->in_cid_len = *p++;
4085
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004086 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4088
4089 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4090 p += ssl->transform->in_cid_len;
4091
4092 ssl->transform->out_cid_len = *p++;
4093
4094 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4096
4097 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4098 p += ssl->transform->out_cid_len;
4099#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4100
4101 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004102 * Saved fields from top-level ssl_context structure
4103 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004104 if( (size_t)( end - p ) < 4 )
4105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4106
4107 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4108 ( (uint32_t) p[1] << 16 ) |
4109 ( (uint32_t) p[2] << 8 ) |
4110 ( (uint32_t) p[3] );
4111 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004112
4113#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4114 if( (size_t)( end - p ) < 16 )
4115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4116
4117 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4118 ( (uint64_t) p[1] << 48 ) |
4119 ( (uint64_t) p[2] << 40 ) |
4120 ( (uint64_t) p[3] << 32 ) |
4121 ( (uint64_t) p[4] << 24 ) |
4122 ( (uint64_t) p[5] << 16 ) |
4123 ( (uint64_t) p[6] << 8 ) |
4124 ( (uint64_t) p[7] );
4125 p += 8;
4126
4127 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4128 ( (uint64_t) p[1] << 48 ) |
4129 ( (uint64_t) p[2] << 40 ) |
4130 ( (uint64_t) p[3] << 32 ) |
4131 ( (uint64_t) p[4] << 24 ) |
4132 ( (uint64_t) p[5] << 16 ) |
4133 ( (uint64_t) p[6] << 8 ) |
4134 ( (uint64_t) p[7] );
4135 p += 8;
4136#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4137
4138#if defined(MBEDTLS_SSL_PROTO_DTLS)
4139 if( (size_t)( end - p ) < 1 )
4140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4141
4142 ssl->disable_datagram_packing = *p++;
4143#endif /* MBEDTLS_SSL_PROTO_DTLS */
4144
Jerry Yud9a94fe2021-09-28 18:58:59 +08004145 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004147 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4148 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004149
4150#if defined(MBEDTLS_SSL_PROTO_DTLS)
4151 if( (size_t)( end - p ) < 2 )
4152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4153
4154 ssl->mtu = ( p[0] << 8 ) | p[1];
4155 p += 2;
4156#endif /* MBEDTLS_SSL_PROTO_DTLS */
4157
4158#if defined(MBEDTLS_SSL_ALPN)
4159 {
4160 uint8_t alpn_len;
4161 const char **cur;
4162
4163 if( (size_t)( end - p ) < 1 )
4164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4165
4166 alpn_len = *p++;
4167
4168 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4169 {
4170 /* alpn_chosen should point to an item in the configured list */
4171 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4172 {
4173 if( strlen( *cur ) == alpn_len &&
4174 memcmp( p, cur, alpn_len ) == 0 )
4175 {
4176 ssl->alpn_chosen = *cur;
4177 break;
4178 }
4179 }
4180 }
4181
4182 /* can only happen on conf mismatch */
4183 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4185
4186 p += alpn_len;
4187 }
4188#endif /* MBEDTLS_SSL_ALPN */
4189
4190 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004191 * Forced fields from top-level ssl_context structure
4192 *
4193 * Most of them already set to the correct value by mbedtls_ssl_init() and
4194 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4195 */
4196 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004197 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004198
Hanno Becker361b10d2019-08-30 10:42:49 +01004199 /* Adjust pointers for header fields of outgoing records to
4200 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004201 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004202
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004203#if defined(MBEDTLS_SSL_PROTO_DTLS)
4204 ssl->in_epoch = 1;
4205#endif
4206
4207 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4208 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004209 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4210 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004211 if( ssl->handshake != NULL )
4212 {
4213 mbedtls_ssl_handshake_free( ssl );
4214 mbedtls_free( ssl->handshake );
4215 ssl->handshake = NULL;
4216 }
4217
4218 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004219 * Done - should have consumed entire buffer
4220 */
4221 if( p != end )
4222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004223
4224 return( 0 );
4225}
4226
4227/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004228 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004229 */
4230int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4231 const unsigned char *buf,
4232 size_t len )
4233{
4234 int ret = ssl_context_load( context, buf, len );
4235
4236 if( ret != 0 )
4237 mbedtls_ssl_free( context );
4238
4239 return( ret );
4240}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004241#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004242
4243/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 * Free an SSL context
4245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004248 if( ssl == NULL )
4249 return;
4250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004252
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004253 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 {
sander-visserb8aa2072020-05-06 22:05:13 +02004255#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4256 size_t out_buf_len = ssl->out_buf_len;
4257#else
4258 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4259#endif
4260
Darryl Greenb33cc762019-11-28 14:29:44 +00004261 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004263 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 }
4265
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004266 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
sander-visserb8aa2072020-05-06 22:05:13 +02004268#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4269 size_t in_buf_len = ssl->in_buf_len;
4270#else
4271 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4272#endif
4273
Darryl Greenb33cc762019-11-28 14:29:44 +00004274 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004276 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004277 }
4278
Paul Bakker48916f92012-09-16 19:57:18 +00004279 if( ssl->transform )
4280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 mbedtls_ssl_transform_free( ssl->transform );
4282 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004283 }
4284
4285 if( ssl->handshake )
4286 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004287 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4289 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 mbedtls_free( ssl->handshake );
4292 mbedtls_free( ssl->transform_negotiate );
4293 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004294 }
4295
Ronald Cron6f135e12021-12-08 16:57:54 +01004296#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004297 mbedtls_ssl_transform_free( ssl->transform_application );
4298 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004299#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004300
Paul Bakkerc0463502013-02-14 11:19:38 +01004301 if( ssl->session )
4302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 mbedtls_ssl_session_free( ssl->session );
4304 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004305 }
4306
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004307#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004308 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004310 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004314
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004315#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004317#endif
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004320
Paul Bakker86f04f42013-02-14 11:20:09 +01004321 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004322 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004323}
4324
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004325/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004326 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004327 */
4328void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4329{
4330 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4331}
4332
Gilles Peskineae270bf2021-06-02 00:05:29 +02004333/* The selection should be the same as mbedtls_x509_crt_profile_default in
4334 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004335 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004336 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004337 * about this list.
4338 */
Brett Warrene0edc842021-08-17 09:53:13 +01004339static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004340#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004341 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004342#endif
4343#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004344 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004345#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004346#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004347 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004348#endif
4349#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004350 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004351#endif
4352#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004353 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004354#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004355#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004356 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004357#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004358#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004359 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004360#endif
4361#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004362 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004363#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004364 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004365};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004366
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004367static int ssl_preset_suiteb_ciphersuites[] = {
4368 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4369 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4370 0
4371};
4372
Gilles Peskineeccd8882020-03-10 12:19:08 +01004373#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004374
Jerry Yu909df7b2022-01-22 11:56:27 +08004375/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004376 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004377 * rules SHOULD be upheld.
4378 * - No duplicate entries.
4379 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004380 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004381 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004382 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004383static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004384
Andrzej Kurek25f27152022-08-17 16:09:31 -04004385#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 +08004386 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4387 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004388#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004389 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004390
Andrzej Kurek25f27152022-08-17 16:09:31 -04004391#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 +08004392 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4393 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004394#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004395 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4396
Andrzej Kurek25f27152022-08-17 16:09:31 -04004397#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 +08004398 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4399 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004400#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004401 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004402
Andrzej Kurek25f27152022-08-17 16:09:31 -04004403#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 +08004404 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004405#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 +08004406
Andrzej Kurek25f27152022-08-17 16:09:31 -04004407#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 +08004408 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004409#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 +08004410
Andrzej Kurek25f27152022-08-17 16:09:31 -04004411#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 +08004412 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004413#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 +08004414
Andrzej Kurek25f27152022-08-17 16:09:31 -04004415#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 +08004416 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4417#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4418
Andrzej Kurek25f27152022-08-17 16:09:31 -04004419#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 +08004420 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4421#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4422
Andrzej Kurek25f27152022-08-17 16:09:31 -04004423#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 +08004424 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4425#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4426
Gabor Mezei15b95a62022-05-09 16:37:58 +02004427 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004428};
4429
4430/* NOTICE: see above */
4431#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4432static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004433#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004434#if defined(MBEDTLS_ECDSA_C)
4435 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004436#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004437#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4438 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4439#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004440#if defined(MBEDTLS_RSA_C)
4441 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4442#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004443#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004444#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004445#if defined(MBEDTLS_ECDSA_C)
4446 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004447#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004448#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4449 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4450#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004451#if defined(MBEDTLS_RSA_C)
4452 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4453#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004454#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004455#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004456#if defined(MBEDTLS_ECDSA_C)
4457 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004458#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004459#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4460 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4461#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004462#if defined(MBEDTLS_RSA_C)
4463 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4464#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004465#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004466 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004467};
4468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4469/* NOTICE: see above */
4470static uint16_t ssl_preset_suiteb_sig_algs[] = {
4471
Andrzej Kurek25f27152022-08-17 16:09:31 -04004472#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 +08004473 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4474 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004475#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004476 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4477
Andrzej Kurek25f27152022-08-17 16:09:31 -04004478#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 +08004479 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4480 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004481#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004482 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004483
Andrzej Kurek25f27152022-08-17 16:09:31 -04004484#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 +08004485 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004486#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 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004490#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004491
Gabor Mezei15b95a62022-05-09 16:37:58 +02004492 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004493};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004494
Jerry Yu909df7b2022-01-22 11:56:27 +08004495/* NOTICE: see above */
4496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4497static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004499#if defined(MBEDTLS_ECDSA_C)
4500 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004501#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004502#if defined(MBEDTLS_RSA_C)
4503 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4504#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004505#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004506#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004507#if defined(MBEDTLS_ECDSA_C)
4508 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004509#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004510#if defined(MBEDTLS_RSA_C)
4511 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4512#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004513#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004514 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004515};
Jerry Yu909df7b2022-01-22 11:56:27 +08004516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4517
Jerry Yu1a8b4812022-01-20 17:56:50 +08004518#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004519
Brett Warrene0edc842021-08-17 09:53:13 +01004520static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004521#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004522 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004523#endif
4524#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004525 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004526#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004527 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004528};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004529
Jerry Yu1a8b4812022-01-20 17:56:50 +08004530#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004531/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004532 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004533MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004534static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004535{
4536 size_t i, j;
4537 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004538
Gabor Mezei15b95a62022-05-09 16:37:58 +02004539 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004540 {
Jerry Yuf377d642022-01-25 10:43:59 +08004541 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542 {
Jerry Yuf377d642022-01-25 10:43:59 +08004543 if( sig_algs[i] != sig_algs[j] )
4544 continue;
4545 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4546 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4547 sig_algs[i], j, i );
4548 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004549 }
4550 }
4551 return( ret );
4552}
4553
4554#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4555
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004556/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004557 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004558 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004559int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004560 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004561{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004562#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004564#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004565
Jerry Yu1a8b4812022-01-20 17:56:50 +08004566#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004567 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004568 {
4569 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4570 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4571 }
4572
Jerry Yuf377d642022-01-25 10:43:59 +08004573 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004574 {
4575 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4576 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4577 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004578
4579#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004580 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004581 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004582 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004583 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4584 }
4585
Jerry Yuf377d642022-01-25 10:43:59 +08004586 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004587 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004588 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004589 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4590 }
4591#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004592#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4593
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004594 /* Use the functions here so that they are covered in tests,
4595 * but otherwise access member directly for efficiency */
4596 mbedtls_ssl_conf_endpoint( conf, endpoint );
4597 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004598
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004599 /*
4600 * Things that are common to all presets
4601 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004602#if defined(MBEDTLS_SSL_CLI_C)
4603 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4604 {
4605 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4606#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4607 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4608#endif
4609 }
4610#endif
4611
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004612#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4613 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4614#endif
4615
4616#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4617 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4618#endif
4619
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004620#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004621 conf->f_cookie_write = ssl_cookie_write_dummy;
4622 conf->f_cookie_check = ssl_cookie_check_dummy;
4623#endif
4624
4625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4626 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4627#endif
4628
Janos Follath088ce432017-04-10 12:42:31 +01004629#if defined(MBEDTLS_SSL_SRV_C)
4630 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004631 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004632#endif
4633
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004634#if defined(MBEDTLS_SSL_PROTO_DTLS)
4635 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4636 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4637#endif
4638
4639#if defined(MBEDTLS_SSL_RENEGOTIATION)
4640 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004641 memset( conf->renego_period, 0x00, 2 );
4642 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004643#endif
4644
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004645#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004646 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4647 {
4648 const unsigned char dhm_p[] =
4649 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4650 const unsigned char dhm_g[] =
4651 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004652
Hanno Beckere2defad2021-07-24 05:59:17 +01004653 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4654 dhm_p, sizeof( dhm_p ),
4655 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4656 {
4657 return( ret );
4658 }
4659 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004660#endif
4661
Ronald Cron6f135e12021-12-08 16:57:54 +01004662#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004663#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004664 mbedtls_ssl_conf_new_session_tickets(
4665 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4666#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004667 /*
4668 * Allow all TLS 1.3 key exchange modes by default.
4669 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004670 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004671#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004672
XiaokangQian4d3a6042022-04-21 13:46:17 +00004673 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4674 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004676 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004677 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004678#else
XiaokangQian060d8672022-04-21 09:24:56 +00004679 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004680#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004681 }
4682 else
4683 {
4684#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4685 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4686 {
4687 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4688 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4689 }
4690 else
4691 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4692 {
4693 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4694 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4695 }
4696#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4697 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4698 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4699#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4700 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4701 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4702#else
4703 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4704#endif
4705 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004706
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004707 /*
4708 * Preset-specific defaults
4709 */
4710 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004711 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004712 /*
4713 * NSA Suite B
4714 */
4715 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004716
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004717 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004718
4719#if defined(MBEDTLS_X509_CRT_PARSE_C)
4720 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004721#endif
4722
Gilles Peskineeccd8882020-03-10 12:19:08 +01004723#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4725 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4726 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4727 else
4728#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4729 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004730#endif
4731
Brett Warrene0edc842021-08-17 09:53:13 +01004732#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4733 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004734#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004735 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004736 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004737
4738 /*
4739 * Default
4740 */
4741 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004742
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004743 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004744
4745#if defined(MBEDTLS_X509_CRT_PARSE_C)
4746 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4747#endif
4748
Gilles Peskineeccd8882020-03-10 12:19:08 +01004749#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4751 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4752 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4753 else
4754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4755 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004756#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004757
Brett Warrene0edc842021-08-17 09:53:13 +01004758#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4759 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004760#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004761 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004762
4763#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4764 conf->dhm_min_bitlen = 1024;
4765#endif
4766 }
4767
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004768 return( 0 );
4769}
4770
4771/*
4772 * Free mbedtls_ssl_config
4773 */
4774void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4775{
4776#if defined(MBEDTLS_DHM_C)
4777 mbedtls_mpi_free( &conf->dhm_P );
4778 mbedtls_mpi_free( &conf->dhm_G );
4779#endif
4780
Gilles Peskineeccd8882020-03-10 12:19:08 +01004781#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004782#if defined(MBEDTLS_USE_PSA_CRYPTO)
4783 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4784 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004785 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4786 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004787#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004788 if( conf->psk != NULL )
4789 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004790 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004791 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004792 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004793 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004794 }
4795
4796 if( conf->psk_identity != NULL )
4797 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004798 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004799 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004800 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004801 conf->psk_identity_len = 0;
4802 }
4803#endif
4804
4805#if defined(MBEDTLS_X509_CRT_PARSE_C)
4806 ssl_key_cert_free( conf->key_cert );
4807#endif
4808
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004809 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004810}
4811
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004812#if defined(MBEDTLS_PK_C) && \
4813 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004814/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#if defined(MBEDTLS_RSA_C)
4820 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4821 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823#if defined(MBEDTLS_ECDSA_C)
4824 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4825 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004828}
4829
Hanno Becker7e5437a2017-04-28 17:15:26 +01004830unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4831{
4832 switch( type ) {
4833 case MBEDTLS_PK_RSA:
4834 return( MBEDTLS_SSL_SIG_RSA );
4835 case MBEDTLS_PK_ECDSA:
4836 case MBEDTLS_PK_ECKEY:
4837 return( MBEDTLS_SSL_SIG_ECDSA );
4838 default:
4839 return( MBEDTLS_SSL_SIG_ANON );
4840 }
4841}
4842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004844{
4845 switch( sig )
4846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#if defined(MBEDTLS_RSA_C)
4848 case MBEDTLS_SSL_SIG_RSA:
4849 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851#if defined(MBEDTLS_ECDSA_C)
4852 case MBEDTLS_SSL_SIG_ECDSA:
4853 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004854#endif
4855 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004857 }
4858}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004859#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004860
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004861/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004862 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004865{
4866 switch( hash )
4867 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004868#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869 case MBEDTLS_SSL_HASH_MD5:
4870 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004871#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004872#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873 case MBEDTLS_SSL_HASH_SHA1:
4874 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004875#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004876#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877 case MBEDTLS_SSL_HASH_SHA224:
4878 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004879#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004880#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 case MBEDTLS_SSL_HASH_SHA256:
4882 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004883#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004884#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 case MBEDTLS_SSL_HASH_SHA384:
4886 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004887#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 case MBEDTLS_SSL_HASH_SHA512:
4890 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004891#endif
4892 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004894 }
4895}
4896
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004897/*
4898 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4899 */
4900unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4901{
4902 switch( md )
4903 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004904#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004905 case MBEDTLS_MD_MD5:
4906 return( MBEDTLS_SSL_HASH_MD5 );
4907#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004908#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004909 case MBEDTLS_MD_SHA1:
4910 return( MBEDTLS_SSL_HASH_SHA1 );
4911#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004912#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004913 case MBEDTLS_MD_SHA224:
4914 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004915#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004916#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004917 case MBEDTLS_MD_SHA256:
4918 return( MBEDTLS_SSL_HASH_SHA256 );
4919#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004921 case MBEDTLS_MD_SHA384:
4922 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004923#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004924#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004925 case MBEDTLS_MD_SHA512:
4926 return( MBEDTLS_SSL_HASH_SHA512 );
4927#endif
4928 default:
4929 return( MBEDTLS_SSL_HASH_NONE );
4930 }
4931}
4932
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004933/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004934 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004935 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004936 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004937int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004938{
Brett Warrene0edc842021-08-17 09:53:13 +01004939 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004940
Brett Warrene0edc842021-08-17 09:53:13 +01004941 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004942 return( -1 );
4943
Brett Warrene0edc842021-08-17 09:53:13 +01004944 for( ; *group_list != 0; group_list++ )
4945 {
4946 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004947 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004948 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004949
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004950 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004951}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004952
4953#if defined(MBEDTLS_ECP_C)
4954/*
4955 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4956 */
4957int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4958{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004959 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004960 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004961
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004962 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004963 return -1;
4964
4965 uint16_t tls_id = grp_info->tls_id;
4966
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004967 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4968}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004969#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971#if defined(MBEDTLS_X509_CRT_PARSE_C)
4972int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4973 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004974 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004975 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004976{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004977 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004978 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004979 const char *ext_oid;
4980 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004983 {
4984 /* Server part of the key exchange */
4985 switch( ciphersuite->key_exchange )
4986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 case MBEDTLS_KEY_EXCHANGE_RSA:
4988 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004989 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990 break;
4991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4993 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4994 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4995 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004996 break;
4997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4999 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005000 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005001 break;
5002
5003 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 case MBEDTLS_KEY_EXCHANGE_NONE:
5005 case MBEDTLS_KEY_EXCHANGE_PSK:
5006 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5007 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005008 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005009 usage = 0;
5010 }
5011 }
5012 else
5013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5015 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016 }
5017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005019 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005020 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005021 ret = -1;
5022 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5027 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005028 }
5029 else
5030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5032 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005033 }
5034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005036 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005037 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005038 ret = -1;
5039 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005040
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005041 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005044
Jerry Yu148165c2021-09-24 23:20:59 +08005045#if defined(MBEDTLS_USE_PSA_CRYPTO)
5046int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5047 const mbedtls_md_type_t md,
5048 unsigned char *dst,
5049 size_t dst_len,
5050 size_t *olen )
5051{
Ronald Cronf6893e12022-01-07 22:09:01 +01005052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5053 psa_hash_operation_t *hash_operation_to_clone;
5054 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5055
Jerry Yu148165c2021-09-24 23:20:59 +08005056 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005057
5058 switch( md )
5059 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005060#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005061 case MBEDTLS_MD_SHA384:
5062 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5063 break;
5064#endif
5065
Andrzej Kurek25f27152022-08-17 16:09:31 -04005066#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005067 case MBEDTLS_MD_SHA256:
5068 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5069 break;
5070#endif
5071
5072 default:
5073 goto exit;
5074 }
5075
5076 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5077 if( status != PSA_SUCCESS )
5078 goto exit;
5079
5080 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5081 if( status != PSA_SUCCESS )
5082 goto exit;
5083
5084exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005085 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005086}
5087#else /* MBEDTLS_USE_PSA_CRYPTO */
5088
Andrzej Kurek25f27152022-08-17 16:09:31 -04005089#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005090MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005091static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5092 unsigned char *dst,
5093 size_t dst_len,
5094 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005095{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005096 int ret;
5097 mbedtls_sha512_context sha512;
5098
5099 if( dst_len < 48 )
5100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5101
5102 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005103 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005104
5105 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5106 {
5107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5108 goto exit;
5109 }
5110
5111 *olen = 48;
5112
5113exit:
5114
5115 mbedtls_sha512_free( &sha512 );
5116 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005117}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005118#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005119
Andrzej Kurek25f27152022-08-17 16:09:31 -04005120#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005121MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005122static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5123 unsigned char *dst,
5124 size_t dst_len,
5125 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005126{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005127 int ret;
5128 mbedtls_sha256_context sha256;
5129
5130 if( dst_len < 32 )
5131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5132
5133 mbedtls_sha256_init( &sha256 );
5134 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005135
Jerry Yu24c0ec32021-09-09 14:21:07 +08005136 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5137 {
5138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5139 goto exit;
5140 }
5141
5142 *olen = 32;
5143
5144exit:
5145
5146 mbedtls_sha256_free( &sha256 );
5147 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005148}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005149#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005150
Jerry Yu000f9762021-09-14 11:12:51 +08005151int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5152 const mbedtls_md_type_t md,
5153 unsigned char *dst,
5154 size_t dst_len,
5155 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005156{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005157 switch( md )
5158 {
5159
Andrzej Kurek25f27152022-08-17 16:09:31 -04005160#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005161 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005162 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005163#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005164
Andrzej Kurek25f27152022-08-17 16:09:31 -04005165#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005166 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005167 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005168#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005169
5170 default:
5171 break;
5172 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005174}
XiaokangQian647719a2021-12-07 09:16:29 +00005175
Jerry Yu148165c2021-09-24 23:20:59 +08005176#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005177
Gabor Mezei078e8032022-04-27 21:17:56 +02005178#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5179/* mbedtls_ssl_parse_sig_alg_ext()
5180 *
5181 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5182 * value (TLS 1.3 RFC8446):
5183 * enum {
5184 * ....
5185 * ecdsa_secp256r1_sha256( 0x0403 ),
5186 * ecdsa_secp384r1_sha384( 0x0503 ),
5187 * ecdsa_secp521r1_sha512( 0x0603 ),
5188 * ....
5189 * } SignatureScheme;
5190 *
5191 * struct {
5192 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5193 * } SignatureSchemeList;
5194 *
5195 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5196 * value (TLS 1.2 RFC5246):
5197 * enum {
5198 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5199 * sha512(6), (255)
5200 * } HashAlgorithm;
5201 *
5202 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5203 * SignatureAlgorithm;
5204 *
5205 * struct {
5206 * HashAlgorithm hash;
5207 * SignatureAlgorithm signature;
5208 * } SignatureAndHashAlgorithm;
5209 *
5210 * SignatureAndHashAlgorithm
5211 * supported_signature_algorithms<2..2^16-2>;
5212 *
5213 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5214 * generalization of the TLS 1.2 signature algorithm extension.
5215 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5216 * `SignatureScheme` field of TLS 1.3
5217 *
5218 */
5219int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5220 const unsigned char *buf,
5221 const unsigned char *end )
5222{
5223 const unsigned char *p = buf;
5224 size_t supported_sig_algs_len = 0;
5225 const unsigned char *supported_sig_algs_end;
5226 uint16_t sig_alg;
5227 uint32_t common_idx = 0;
5228
5229 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5230 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5231 p += 2;
5232
5233 memset( ssl->handshake->received_sig_algs, 0,
5234 sizeof(ssl->handshake->received_sig_algs) );
5235
5236 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5237 supported_sig_algs_end = p + supported_sig_algs_len;
5238 while( p < supported_sig_algs_end )
5239 {
5240 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5241 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5242 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005243 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5244 sig_alg,
5245 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005246#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005247 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005248 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5249 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5250 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005251 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005252 }
5253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005254
Jerry Yuf3b46b52022-06-19 16:52:27 +08005255 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5256 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005257
5258 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5259 {
5260 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5261 common_idx += 1;
5262 }
5263 }
5264 /* Check that we consumed all the message. */
5265 if( p != end )
5266 {
5267 MBEDTLS_SSL_DEBUG_MSG( 1,
5268 ( "Signature algorithms extension length misaligned" ) );
5269 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5270 MBEDTLS_ERR_SSL_DECODE_ERROR );
5271 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5272 }
5273
5274 if( common_idx == 0 )
5275 {
5276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5277 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5278 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5279 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5280 }
5281
Gabor Mezei15b95a62022-05-09 16:37:58 +02005282 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005283 return( 0 );
5284}
5285
5286#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5287
Jerry Yudc7bd172022-02-17 13:44:15 +08005288#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5289
5290#if defined(MBEDTLS_USE_PSA_CRYPTO)
5291
5292static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5293 mbedtls_svc_key_id_t key,
5294 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005295 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005296 const unsigned char* seed, size_t seed_length,
5297 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005298 const unsigned char* other_secret,
5299 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005300 size_t capacity )
5301{
5302 psa_status_t status;
5303
5304 status = psa_key_derivation_setup( derivation, alg );
5305 if( status != PSA_SUCCESS )
5306 return( status );
5307
5308 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5309 {
5310 status = psa_key_derivation_input_bytes( derivation,
5311 PSA_KEY_DERIVATION_INPUT_SEED,
5312 seed, seed_length );
5313 if( status != PSA_SUCCESS )
5314 return( status );
5315
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005316 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005317 {
5318 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005319 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5320 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005321 if( status != PSA_SUCCESS )
5322 return( status );
5323 }
5324
Jerry Yudc7bd172022-02-17 13:44:15 +08005325 if( mbedtls_svc_key_id_is_null( key ) )
5326 {
5327 status = psa_key_derivation_input_bytes(
5328 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005329 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005330 }
5331 else
5332 {
5333 status = psa_key_derivation_input_key(
5334 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5335 }
5336 if( status != PSA_SUCCESS )
5337 return( status );
5338
5339 status = psa_key_derivation_input_bytes( derivation,
5340 PSA_KEY_DERIVATION_INPUT_LABEL,
5341 label, label_length );
5342 if( status != PSA_SUCCESS )
5343 return( status );
5344 }
5345 else
5346 {
5347 return( PSA_ERROR_NOT_SUPPORTED );
5348 }
5349
5350 status = psa_key_derivation_set_capacity( derivation, capacity );
5351 if( status != PSA_SUCCESS )
5352 return( status );
5353
5354 return( PSA_SUCCESS );
5355}
5356
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005357MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005358static int tls_prf_generic( mbedtls_md_type_t md_type,
5359 const unsigned char *secret, size_t slen,
5360 const char *label,
5361 const unsigned char *random, size_t rlen,
5362 unsigned char *dstbuf, size_t dlen )
5363{
5364 psa_status_t status;
5365 psa_algorithm_t alg;
5366 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5367 psa_key_derivation_operation_t derivation =
5368 PSA_KEY_DERIVATION_OPERATION_INIT;
5369
5370 if( md_type == MBEDTLS_MD_SHA384 )
5371 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5372 else
5373 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5374
5375 /* Normally a "secret" should be long enough to be impossible to
5376 * find by brute force, and in particular should not be empty. But
5377 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5378 * and for this use case it makes sense to have a 0-length "secret".
5379 * Since the key API doesn't allow importing a key of length 0,
5380 * keep master_key=0, which setup_psa_key_derivation() understands
5381 * to mean a 0-length "secret" input. */
5382 if( slen != 0 )
5383 {
5384 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5385 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5386 psa_set_key_algorithm( &key_attributes, alg );
5387 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5388
5389 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5390 if( status != PSA_SUCCESS )
5391 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5392 }
5393
5394 status = setup_psa_key_derivation( &derivation,
5395 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005396 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005397 random, rlen,
5398 (unsigned char const *) label,
5399 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005400 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005401 dlen );
5402 if( status != PSA_SUCCESS )
5403 {
5404 psa_key_derivation_abort( &derivation );
5405 psa_destroy_key( master_key );
5406 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5407 }
5408
5409 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5410 if( status != PSA_SUCCESS )
5411 {
5412 psa_key_derivation_abort( &derivation );
5413 psa_destroy_key( master_key );
5414 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5415 }
5416
5417 status = psa_key_derivation_abort( &derivation );
5418 if( status != PSA_SUCCESS )
5419 {
5420 psa_destroy_key( master_key );
5421 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5422 }
5423
5424 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5425 status = psa_destroy_key( master_key );
5426 if( status != PSA_SUCCESS )
5427 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5428
5429 return( 0 );
5430}
5431
5432#else /* MBEDTLS_USE_PSA_CRYPTO */
5433
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005434MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005435static int tls_prf_generic( mbedtls_md_type_t md_type,
5436 const unsigned char *secret, size_t slen,
5437 const char *label,
5438 const unsigned char *random, size_t rlen,
5439 unsigned char *dstbuf, size_t dlen )
5440{
5441 size_t nb;
5442 size_t i, j, k, md_len;
5443 unsigned char *tmp;
5444 size_t tmp_len = 0;
5445 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5446 const mbedtls_md_info_t *md_info;
5447 mbedtls_md_context_t md_ctx;
5448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5449
5450 mbedtls_md_init( &md_ctx );
5451
5452 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5454
5455 md_len = mbedtls_md_get_size( md_info );
5456
5457 tmp_len = md_len + strlen( label ) + rlen;
5458 tmp = mbedtls_calloc( 1, tmp_len );
5459 if( tmp == NULL )
5460 {
5461 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5462 goto exit;
5463 }
5464
5465 nb = strlen( label );
5466 memcpy( tmp + md_len, label, nb );
5467 memcpy( tmp + md_len + nb, random, rlen );
5468 nb += rlen;
5469
5470 /*
5471 * Compute P_<hash>(secret, label + random)[0..dlen]
5472 */
5473 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5474 goto exit;
5475
5476 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5477 if( ret != 0 )
5478 goto exit;
5479 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5480 if( ret != 0 )
5481 goto exit;
5482 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5483 if( ret != 0 )
5484 goto exit;
5485
5486 for( i = 0; i < dlen; i += md_len )
5487 {
5488 ret = mbedtls_md_hmac_reset ( &md_ctx );
5489 if( ret != 0 )
5490 goto exit;
5491 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5492 if( ret != 0 )
5493 goto exit;
5494 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5495 if( ret != 0 )
5496 goto exit;
5497
5498 ret = mbedtls_md_hmac_reset ( &md_ctx );
5499 if( ret != 0 )
5500 goto exit;
5501 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5502 if( ret != 0 )
5503 goto exit;
5504 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5505 if( ret != 0 )
5506 goto exit;
5507
5508 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5509
5510 for( j = 0; j < k; j++ )
5511 dstbuf[i + j] = h_i[j];
5512 }
5513
5514exit:
5515 mbedtls_md_free( &md_ctx );
5516
5517 mbedtls_platform_zeroize( tmp, tmp_len );
5518 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5519
5520 mbedtls_free( tmp );
5521
5522 return( ret );
5523}
5524#endif /* MBEDTLS_USE_PSA_CRYPTO */
5525
Andrzej Kurek25f27152022-08-17 16:09:31 -04005526#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005527MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005528static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5529 const char *label,
5530 const unsigned char *random, size_t rlen,
5531 unsigned char *dstbuf, size_t dlen )
5532{
5533 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5534 label, random, rlen, dstbuf, dlen ) );
5535}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005536#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005537
Andrzej Kurek25f27152022-08-17 16:09:31 -04005538#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005539MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005540static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5541 const char *label,
5542 const unsigned char *random, size_t rlen,
5543 unsigned char *dstbuf, size_t dlen )
5544{
5545 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5546 label, random, rlen, dstbuf, dlen ) );
5547}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005548#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005549
Jerry Yuf009d862022-02-17 14:01:37 +08005550/*
5551 * Set appropriate PRF function and other SSL / TLS1.2 functions
5552 *
5553 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005554 * - hash associated with the ciphersuite (only used by TLS 1.2)
5555 *
5556 * Outputs:
5557 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5558 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005559MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005560static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005561 mbedtls_md_type_t hash )
5562{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005563#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005564 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005565 {
5566 handshake->tls_prf = tls_prf_sha384;
5567 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5568 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5569 }
5570 else
5571#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005573 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005574 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005575 handshake->tls_prf = tls_prf_sha256;
5576 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5577 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5578 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005579#else
Jerry Yuf009d862022-02-17 14:01:37 +08005580 {
Jerry Yuf009d862022-02-17 14:01:37 +08005581 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005582 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5584 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005585#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005586
5587 return( 0 );
5588}
Jerry Yud6ab2352022-02-17 14:03:43 +08005589
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005590/*
5591 * Compute master secret if needed
5592 *
5593 * Parameters:
5594 * [in/out] handshake
5595 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5596 * (PSA-PSK) ciphersuite_info, psk_opaque
5597 * [out] premaster (cleared)
5598 * [out] master
5599 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5600 * debug: conf->f_dbg, conf->p_dbg
5601 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005602 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005603 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005604MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005605static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5606 unsigned char *master,
5607 const mbedtls_ssl_context *ssl )
5608{
5609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5610
5611 /* cf. RFC 5246, Section 8.1:
5612 * "The master secret is always exactly 48 bytes in length." */
5613 size_t const master_secret_len = 48;
5614
5615#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5616 unsigned char session_hash[48];
5617#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5618
5619 /* The label for the KDF used for key expansion.
5620 * This is either "master secret" or "extended master secret"
5621 * depending on whether the Extended Master Secret extension
5622 * is used. */
5623 char const *lbl = "master secret";
5624
Przemek Stekielae4ed302022-04-05 17:15:55 +02005625 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005626 * - If the Extended Master Secret extension is not used,
5627 * this is ClientHello.Random + ServerHello.Random
5628 * (see Sect. 8.1 in RFC 5246).
5629 * - If the Extended Master Secret extension is used,
5630 * this is the transcript of the handshake so far.
5631 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005632 unsigned char const *seed = handshake->randbytes;
5633 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005634
5635#if !defined(MBEDTLS_DEBUG_C) && \
5636 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5637 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5638 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5639 ssl = NULL; /* make sure we don't use it except for those cases */
5640 (void) ssl;
5641#endif
5642
5643 if( handshake->resume != 0 )
5644 {
5645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5646 return( 0 );
5647 }
5648
5649#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5650 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5651 {
5652 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005653 seed = session_hash;
5654 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005655
5656 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005657 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005658 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005659#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005660
Przemek Stekiel99114f32022-04-22 11:20:09 +02005661#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005662 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005663 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005664 {
5665 /* Perform PSK-to-MS expansion in a single step. */
5666 psa_status_t status;
5667 psa_algorithm_t alg;
5668 mbedtls_svc_key_id_t psk;
5669 psa_key_derivation_operation_t derivation =
5670 PSA_KEY_DERIVATION_OPERATION_INIT;
5671 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5672
5673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5674
5675 psk = mbedtls_ssl_get_opaque_psk( ssl );
5676
5677 if( hash_alg == MBEDTLS_MD_SHA384 )
5678 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5679 else
5680 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5681
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005682 size_t other_secret_len = 0;
5683 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005684
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005685 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005686 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005687 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005688 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005689 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005690 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005691 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005692 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005693 other_secret_len = 48;
5694 other_secret = handshake->premaster + 2;
5695 break;
5696 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005697 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005698 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5699 other_secret = handshake->premaster + 2;
5700 break;
5701 default:
5702 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005703 }
5704
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005705 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005706 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005707 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005708 (unsigned char const *) lbl,
5709 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005710 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005711 master_secret_len );
5712 if( status != PSA_SUCCESS )
5713 {
5714 psa_key_derivation_abort( &derivation );
5715 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5716 }
5717
5718 status = psa_key_derivation_output_bytes( &derivation,
5719 master,
5720 master_secret_len );
5721 if( status != PSA_SUCCESS )
5722 {
5723 psa_key_derivation_abort( &derivation );
5724 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5725 }
5726
5727 status = psa_key_derivation_abort( &derivation );
5728 if( status != PSA_SUCCESS )
5729 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5730 }
5731 else
5732#endif
5733 {
5734 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005735 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005736 master,
5737 master_secret_len );
5738 if( ret != 0 )
5739 {
5740 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5741 return( ret );
5742 }
5743
5744 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5745 handshake->premaster,
5746 handshake->pmslen );
5747
5748 mbedtls_platform_zeroize( handshake->premaster,
5749 sizeof(handshake->premaster) );
5750 }
5751
5752 return( 0 );
5753}
5754
Jerry Yud62f87e2022-02-17 14:09:02 +08005755int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5756{
5757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5758 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5759 ssl->handshake->ciphersuite_info;
5760
5761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5762
5763 /* Set PRF, calc_verify and calc_finished function pointers */
5764 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005765 ciphersuite_info->mac );
5766 if( ret != 0 )
5767 {
5768 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5769 return( ret );
5770 }
5771
5772 /* Compute master secret if needed */
5773 ret = ssl_compute_master( ssl->handshake,
5774 ssl->session_negotiate->master,
5775 ssl );
5776 if( ret != 0 )
5777 {
5778 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5779 return( ret );
5780 }
5781
5782 /* Swap the client and server random values:
5783 * - MS derivation wanted client+server (RFC 5246 8.1)
5784 * - key derivation wants server+client (RFC 5246 6.3) */
5785 {
5786 unsigned char tmp[64];
5787 memcpy( tmp, ssl->handshake->randbytes, 64 );
5788 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5789 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5790 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5791 }
5792
5793 /* Populate transform structure */
5794 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5795 ssl->session_negotiate->ciphersuite,
5796 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005797#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005798 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005799#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005800 ssl->handshake->tls_prf,
5801 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005802 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005803 ssl->conf->endpoint,
5804 ssl );
5805 if( ret != 0 )
5806 {
5807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5808 return( ret );
5809 }
5810
5811 /* We no longer need Server/ClientHello.random values */
5812 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5813 sizeof( ssl->handshake->randbytes ) );
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5816
5817 return( 0 );
5818}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005819
Ronald Cron4dcbca92022-03-07 10:21:40 +01005820int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5821{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005822 switch( md )
5823 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005824#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005825 case MBEDTLS_SSL_HASH_SHA384:
5826 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5827 break;
5828#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005829#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005830 case MBEDTLS_SSL_HASH_SHA256:
5831 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5832 break;
5833#endif
5834 default:
5835 return( -1 );
5836 }
5837
Ronald Cronc2f13a02022-03-07 10:25:24 +01005838 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005839}
5840
Andrzej Kurek25f27152022-08-17 16:09:31 -04005841#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005842void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5843 unsigned char *hash,
5844 size_t *hlen )
5845{
5846#if defined(MBEDTLS_USE_PSA_CRYPTO)
5847 size_t hash_size;
5848 psa_status_t status;
5849 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5850
5851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5852 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5853 if( status != PSA_SUCCESS )
5854 {
5855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5856 return;
5857 }
5858
5859 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5860 if( status != PSA_SUCCESS )
5861 {
5862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5863 return;
5864 }
5865
5866 *hlen = 32;
5867 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5869#else
5870 mbedtls_sha256_context sha256;
5871
5872 mbedtls_sha256_init( &sha256 );
5873
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5875
5876 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5877 mbedtls_sha256_finish( &sha256, hash );
5878
5879 *hlen = 32;
5880
5881 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5883
5884 mbedtls_sha256_free( &sha256 );
5885#endif /* MBEDTLS_USE_PSA_CRYPTO */
5886 return;
5887}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005888#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005889
Andrzej Kurek25f27152022-08-17 16:09:31 -04005890#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005891void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5892 unsigned char *hash,
5893 size_t *hlen )
5894{
5895#if defined(MBEDTLS_USE_PSA_CRYPTO)
5896 size_t hash_size;
5897 psa_status_t status;
5898 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5899
5900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5901 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5902 if( status != PSA_SUCCESS )
5903 {
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5905 return;
5906 }
5907
5908 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5909 if( status != PSA_SUCCESS )
5910 {
5911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5912 return;
5913 }
5914
5915 *hlen = 48;
5916 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5918#else
5919 mbedtls_sha512_context sha512;
5920
5921 mbedtls_sha512_init( &sha512 );
5922
5923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5924
Andrzej Kureka242e832022-08-11 10:03:14 -04005925 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005926 mbedtls_sha512_finish( &sha512, hash );
5927
5928 *hlen = 48;
5929
5930 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5932
5933 mbedtls_sha512_free( &sha512 );
5934#endif /* MBEDTLS_USE_PSA_CRYPTO */
5935 return;
5936}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005937#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005938
Neil Armstrong80f6f322022-05-03 17:56:38 +02005939#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5940 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005941int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5942{
5943 unsigned char *p = ssl->handshake->premaster;
5944 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5945 const unsigned char *psk = NULL;
5946 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005947 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005948
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005949 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005950 {
5951 /*
5952 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005953 * checked before calling this function.
5954 *
5955 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005956 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005957 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005958 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5959 {
5960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5962 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005963 }
5964
5965 /*
5966 * PMS = struct {
5967 * opaque other_secret<0..2^16-1>;
5968 * opaque psk<0..2^16-1>;
5969 * };
5970 * with "other_secret" depending on the particular key exchange
5971 */
5972#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5973 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5974 {
5975 if( end - p < 2 )
5976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5977
5978 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5979 p += 2;
5980
5981 if( end < p || (size_t)( end - p ) < psk_len )
5982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5983
5984 memset( p, 0, psk_len );
5985 p += psk_len;
5986 }
5987 else
5988#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5989#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5990 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5991 {
5992 /*
5993 * other_secret already set by the ClientKeyExchange message,
5994 * and is 48 bytes long
5995 */
5996 if( end - p < 2 )
5997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5998
5999 *p++ = 0;
6000 *p++ = 48;
6001 p += 48;
6002 }
6003 else
6004#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6005#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6006 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6007 {
6008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6009 size_t len;
6010
6011 /* Write length only when we know the actual value */
6012 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6013 p + 2, end - ( p + 2 ), &len,
6014 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6015 {
6016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6017 return( ret );
6018 }
6019 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6020 p += 2 + len;
6021
6022 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6023 }
6024 else
6025#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006026#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006027 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6028 {
6029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6030 size_t zlen;
6031
6032 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6033 p + 2, end - ( p + 2 ),
6034 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6035 {
6036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6037 return( ret );
6038 }
6039
6040 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6041 p += 2 + zlen;
6042
6043 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6044 MBEDTLS_DEBUG_ECDH_Z );
6045 }
6046 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006047#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006048 {
6049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6051 }
6052
6053 /* opaque psk<0..2^16-1>; */
6054 if( end - p < 2 )
6055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6056
6057 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6058 p += 2;
6059
6060 if( end < p || (size_t)( end - p ) < psk_len )
6061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6062
6063 memcpy( p, psk, psk_len );
6064 p += psk_len;
6065
6066 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6067
6068 return( 0 );
6069}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006070#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006071
6072#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006073MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006074static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6075
6076#if defined(MBEDTLS_SSL_PROTO_DTLS)
6077int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6078{
6079 /* If renegotiation is not enforced, retransmit until we would reach max
6080 * timeout if we were using the usual handshake doubling scheme */
6081 if( ssl->conf->renego_max_records < 0 )
6082 {
6083 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6084 unsigned char doublings = 1;
6085
6086 while( ratio != 0 )
6087 {
6088 ++doublings;
6089 ratio >>= 1;
6090 }
6091
6092 if( ++ssl->renego_records_seen > doublings )
6093 {
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6095 return( 0 );
6096 }
6097 }
6098
6099 return( ssl_write_hello_request( ssl ) );
6100}
6101#endif
6102#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006103
Jerry Yud9526692022-02-17 14:23:47 +08006104/*
6105 * Handshake functions
6106 */
6107#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6108/* No certificate support -> dummy functions */
6109int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6110{
6111 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6112 ssl->handshake->ciphersuite_info;
6113
6114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6115
6116 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6117 {
6118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6119 ssl->state++;
6120 return( 0 );
6121 }
6122
6123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6125}
6126
6127int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6128{
6129 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6130 ssl->handshake->ciphersuite_info;
6131
6132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6133
6134 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6135 {
6136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6137 ssl->state++;
6138 return( 0 );
6139 }
6140
6141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6143}
6144
6145#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6146/* Some certificate support -> implement write and parse */
6147
6148int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6149{
6150 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6151 size_t i, n;
6152 const mbedtls_x509_crt *crt;
6153 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6154 ssl->handshake->ciphersuite_info;
6155
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6157
6158 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6161 ssl->state++;
6162 return( 0 );
6163 }
6164
6165#if defined(MBEDTLS_SSL_CLI_C)
6166 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6167 {
6168 if( ssl->handshake->client_auth == 0 )
6169 {
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6171 ssl->state++;
6172 return( 0 );
6173 }
6174 }
6175#endif /* MBEDTLS_SSL_CLI_C */
6176#if defined(MBEDTLS_SSL_SRV_C)
6177 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6178 {
6179 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6180 {
6181 /* Should never happen because we shouldn't have picked the
6182 * ciphersuite if we don't have a certificate. */
6183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6184 }
6185 }
6186#endif
6187
6188 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6189
6190 /*
6191 * 0 . 0 handshake type
6192 * 1 . 3 handshake length
6193 * 4 . 6 length of all certs
6194 * 7 . 9 length of cert. 1
6195 * 10 . n-1 peer certificate
6196 * n . n+2 length of cert. 2
6197 * n+3 . ... upper level cert, etc.
6198 */
6199 i = 7;
6200 crt = mbedtls_ssl_own_cert( ssl );
6201
6202 while( crt != NULL )
6203 {
6204 n = crt->raw.len;
6205 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6208 " > %" MBEDTLS_PRINTF_SIZET,
6209 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6210 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6211 }
6212
6213 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6214 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6215 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6216
6217 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6218 i += n; crt = crt->next;
6219 }
6220
6221 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6222 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6223 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6224
6225 ssl->out_msglen = i;
6226 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6227 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6228
6229 ssl->state++;
6230
6231 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6232 {
6233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6234 return( ret );
6235 }
6236
6237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6238
6239 return( ret );
6240}
6241
6242#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6243
6244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006245MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006246static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6247 unsigned char *crt_buf,
6248 size_t crt_buf_len )
6249{
6250 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6251
6252 if( peer_crt == NULL )
6253 return( -1 );
6254
6255 if( peer_crt->raw.len != crt_buf_len )
6256 return( -1 );
6257
6258 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6259}
6260#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006261MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006262static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6263 unsigned char *crt_buf,
6264 size_t crt_buf_len )
6265{
6266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6267 unsigned char const * const peer_cert_digest =
6268 ssl->session->peer_cert_digest;
6269 mbedtls_md_type_t const peer_cert_digest_type =
6270 ssl->session->peer_cert_digest_type;
6271 mbedtls_md_info_t const * const digest_info =
6272 mbedtls_md_info_from_type( peer_cert_digest_type );
6273 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6274 size_t digest_len;
6275
6276 if( peer_cert_digest == NULL || digest_info == NULL )
6277 return( -1 );
6278
6279 digest_len = mbedtls_md_get_size( digest_info );
6280 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6281 return( -1 );
6282
6283 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6284 if( ret != 0 )
6285 return( -1 );
6286
6287 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6288}
6289#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6290#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6291
6292/*
6293 * Once the certificate message is read, parse it into a cert chain and
6294 * perform basic checks, but leave actual verification to the caller
6295 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006297static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6298 mbedtls_x509_crt *chain )
6299{
6300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6301#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6302 int crt_cnt=0;
6303#endif
6304 size_t i, n;
6305 uint8_t alert;
6306
6307 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6308 {
6309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6311 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6312 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6313 }
6314
6315 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6316 {
6317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6318 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6319 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6320 }
6321
6322 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6323 {
6324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6325 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6326 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6327 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6328 }
6329
6330 i = mbedtls_ssl_hs_hdr_len( ssl );
6331
6332 /*
6333 * Same message structure as in mbedtls_ssl_write_certificate()
6334 */
6335 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6336
6337 if( ssl->in_msg[i] != 0 ||
6338 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6339 {
6340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6341 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6342 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6343 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6344 }
6345
6346 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6347 i += 3;
6348
6349 /* Iterate through and parse the CRTs in the provided chain. */
6350 while( i < ssl->in_hslen )
6351 {
6352 /* Check that there's room for the next CRT's length fields. */
6353 if ( i + 3 > ssl->in_hslen ) {
6354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6355 mbedtls_ssl_send_alert_message( ssl,
6356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6357 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6358 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6359 }
6360 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6361 * anything beyond 2**16 ~ 64K. */
6362 if( ssl->in_msg[i] != 0 )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6365 mbedtls_ssl_send_alert_message( ssl,
6366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6367 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6368 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6369 }
6370
6371 /* Read length of the next CRT in the chain. */
6372 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6373 | (unsigned int) ssl->in_msg[i + 2];
6374 i += 3;
6375
6376 if( n < 128 || i + n > ssl->in_hslen )
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_DECODE_ERROR );
6382 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6383 }
6384
6385 /* Check if we're handling the first CRT in the chain. */
6386#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6387 if( crt_cnt++ == 0 &&
6388 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6389 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6390 {
6391 /* During client-side renegotiation, check that the server's
6392 * end-CRTs hasn't changed compared to the initial handshake,
6393 * mitigating the triple handshake attack. On success, reuse
6394 * the original end-CRT instead of parsing it again. */
6395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6396 if( ssl_check_peer_crt_unchanged( ssl,
6397 &ssl->in_msg[i],
6398 n ) != 0 )
6399 {
6400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6401 mbedtls_ssl_send_alert_message( ssl,
6402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6403 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6404 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6405 }
6406
6407 /* Now we can safely free the original chain. */
6408 ssl_clear_peer_cert( ssl->session );
6409 }
6410#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6411
6412 /* Parse the next certificate in the chain. */
6413#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6414 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6415#else
6416 /* If we don't need to store the CRT chain permanently, parse
6417 * it in-place from the input buffer instead of making a copy. */
6418 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6419#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6420 switch( ret )
6421 {
6422 case 0: /*ok*/
6423 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6424 /* Ignore certificate with an unknown algorithm: maybe a
6425 prior certificate was already trusted. */
6426 break;
6427
6428 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6429 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6430 goto crt_parse_der_failed;
6431
6432 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6433 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6434 goto crt_parse_der_failed;
6435
6436 default:
6437 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6438 crt_parse_der_failed:
6439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6440 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6441 return( ret );
6442 }
6443
6444 i += n;
6445 }
6446
6447 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6448 return( 0 );
6449}
6450
6451#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006452MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006453static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6454{
6455 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6456 return( -1 );
6457
6458 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6459 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6460 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6461 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6462 {
Ronald Cron19385882022-06-15 16:26:13 +02006463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006464 return( 0 );
6465 }
6466 return( -1 );
6467}
6468#endif /* MBEDTLS_SSL_SRV_C */
6469
6470/* Check if a certificate message is expected.
6471 * Return either
6472 * - SSL_CERTIFICATE_EXPECTED, or
6473 * - SSL_CERTIFICATE_SKIP
6474 * indicating whether a Certificate message is expected or not.
6475 */
6476#define SSL_CERTIFICATE_EXPECTED 0
6477#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006478MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006479static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6480 int authmode )
6481{
6482 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6483 ssl->handshake->ciphersuite_info;
6484
6485 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6486 return( SSL_CERTIFICATE_SKIP );
6487
6488#if defined(MBEDTLS_SSL_SRV_C)
6489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6490 {
6491 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6492 return( SSL_CERTIFICATE_SKIP );
6493
6494 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6495 {
6496 ssl->session_negotiate->verify_result =
6497 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6498 return( SSL_CERTIFICATE_SKIP );
6499 }
6500 }
6501#else
6502 ((void) authmode);
6503#endif /* MBEDTLS_SSL_SRV_C */
6504
6505 return( SSL_CERTIFICATE_EXPECTED );
6506}
6507
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006508MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006509static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6510 int authmode,
6511 mbedtls_x509_crt *chain,
6512 void *rs_ctx )
6513{
6514 int ret = 0;
6515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6516 ssl->handshake->ciphersuite_info;
6517 int have_ca_chain = 0;
6518
6519 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6520 void *p_vrfy;
6521
6522 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6523 return( 0 );
6524
6525 if( ssl->f_vrfy != NULL )
6526 {
6527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6528 f_vrfy = ssl->f_vrfy;
6529 p_vrfy = ssl->p_vrfy;
6530 }
6531 else
6532 {
6533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6534 f_vrfy = ssl->conf->f_vrfy;
6535 p_vrfy = ssl->conf->p_vrfy;
6536 }
6537
6538 /*
6539 * Main check: verify certificate
6540 */
6541#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6542 if( ssl->conf->f_ca_cb != NULL )
6543 {
6544 ((void) rs_ctx);
6545 have_ca_chain = 1;
6546
6547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6548 ret = mbedtls_x509_crt_verify_with_ca_cb(
6549 chain,
6550 ssl->conf->f_ca_cb,
6551 ssl->conf->p_ca_cb,
6552 ssl->conf->cert_profile,
6553 ssl->hostname,
6554 &ssl->session_negotiate->verify_result,
6555 f_vrfy, p_vrfy );
6556 }
6557 else
6558#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6559 {
6560 mbedtls_x509_crt *ca_chain;
6561 mbedtls_x509_crl *ca_crl;
6562
6563#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6564 if( ssl->handshake->sni_ca_chain != NULL )
6565 {
6566 ca_chain = ssl->handshake->sni_ca_chain;
6567 ca_crl = ssl->handshake->sni_ca_crl;
6568 }
6569 else
6570#endif
6571 {
6572 ca_chain = ssl->conf->ca_chain;
6573 ca_crl = ssl->conf->ca_crl;
6574 }
6575
6576 if( ca_chain != NULL )
6577 have_ca_chain = 1;
6578
6579 ret = mbedtls_x509_crt_verify_restartable(
6580 chain,
6581 ca_chain, ca_crl,
6582 ssl->conf->cert_profile,
6583 ssl->hostname,
6584 &ssl->session_negotiate->verify_result,
6585 f_vrfy, p_vrfy, rs_ctx );
6586 }
6587
6588 if( ret != 0 )
6589 {
6590 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6591 }
6592
6593#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6594 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6595 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6596#endif
6597
6598 /*
6599 * Secondary checks: always done, but change 'ret' only if it was 0
6600 */
6601
6602#if defined(MBEDTLS_ECP_C)
6603 {
6604 const mbedtls_pk_context *pk = &chain->pk;
6605
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006606 /* If certificate uses an EC key, make sure the curve is OK.
6607 * This is a public key, so it can't be opaque, so can_do() is a good
6608 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006609 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006610 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006611 /* and in the unlikely case the above assumption no longer holds
6612 * we are making sure that pk_ec() here does not return a NULL
6613 */
6614 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6615 if( ec == NULL )
6616 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6619 }
Jerry Yud9526692022-02-17 14:23:47 +08006620
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006621 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6622 {
6623 ssl->session_negotiate->verify_result |=
6624 MBEDTLS_X509_BADCERT_BAD_KEY;
6625
6626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6627 if( ret == 0 )
6628 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6629 }
Jerry Yud9526692022-02-17 14:23:47 +08006630 }
6631 }
6632#endif /* MBEDTLS_ECP_C */
6633
6634 if( mbedtls_ssl_check_cert_usage( chain,
6635 ciphersuite_info,
6636 ! ssl->conf->endpoint,
6637 &ssl->session_negotiate->verify_result ) != 0 )
6638 {
6639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6640 if( ret == 0 )
6641 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6642 }
6643
6644 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6645 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6646 * with details encoded in the verification flags. All other kinds
6647 * of error codes, including those from the user provided f_vrfy
6648 * functions, are treated as fatal and lead to a failure of
6649 * ssl_parse_certificate even if verification was optional. */
6650 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6651 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6652 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6653 {
6654 ret = 0;
6655 }
6656
6657 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6658 {
6659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6660 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6661 }
6662
6663 if( ret != 0 )
6664 {
6665 uint8_t alert;
6666
6667 /* The certificate may have been rejected for several reasons.
6668 Pick one and send the corresponding alert. Which alert to send
6669 may be a subject of debate in some cases. */
6670 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6671 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6672 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6673 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6674 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6675 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6676 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6677 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6678 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6679 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6680 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6681 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6682 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6683 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6685 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6687 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6689 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6690 else
6691 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6692 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6693 alert );
6694 }
6695
6696#if defined(MBEDTLS_DEBUG_C)
6697 if( ssl->session_negotiate->verify_result != 0 )
6698 {
6699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6700 (unsigned int) ssl->session_negotiate->verify_result ) );
6701 }
6702 else
6703 {
6704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6705 }
6706#endif /* MBEDTLS_DEBUG_C */
6707
6708 return( ret );
6709}
6710
6711#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006712MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006713static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6714 unsigned char *start, size_t len )
6715{
6716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6717 /* Remember digest of the peer's end-CRT. */
6718 ssl->session_negotiate->peer_cert_digest =
6719 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6720 if( ssl->session_negotiate->peer_cert_digest == NULL )
6721 {
6722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6723 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6724 mbedtls_ssl_send_alert_message( ssl,
6725 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6726 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6727
6728 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6729 }
6730
6731 ret = mbedtls_md( mbedtls_md_info_from_type(
6732 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6733 start, len,
6734 ssl->session_negotiate->peer_cert_digest );
6735
6736 ssl->session_negotiate->peer_cert_digest_type =
6737 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6738 ssl->session_negotiate->peer_cert_digest_len =
6739 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6740
6741 return( ret );
6742}
6743
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006744MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006745static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6746 unsigned char *start, size_t len )
6747{
6748 unsigned char *end = start + len;
6749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6750
6751 /* Make a copy of the peer's raw public key. */
6752 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6753 ret = mbedtls_pk_parse_subpubkey( &start, end,
6754 &ssl->handshake->peer_pubkey );
6755 if( ret != 0 )
6756 {
6757 /* We should have parsed the public key before. */
6758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6759 }
6760
6761 return( 0 );
6762}
6763#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6764
6765int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6766{
6767 int ret = 0;
6768 int crt_expected;
6769#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6770 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6771 ? ssl->handshake->sni_authmode
6772 : ssl->conf->authmode;
6773#else
6774 const int authmode = ssl->conf->authmode;
6775#endif
6776 void *rs_ctx = NULL;
6777 mbedtls_x509_crt *chain = NULL;
6778
6779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6780
6781 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6782 if( crt_expected == SSL_CERTIFICATE_SKIP )
6783 {
6784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6785 goto exit;
6786 }
6787
6788#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6789 if( ssl->handshake->ecrs_enabled &&
6790 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6791 {
6792 chain = ssl->handshake->ecrs_peer_cert;
6793 ssl->handshake->ecrs_peer_cert = NULL;
6794 goto crt_verify;
6795 }
6796#endif
6797
6798 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6799 {
6800 /* mbedtls_ssl_read_record may have sent an alert already. We
6801 let it decide whether to alert. */
6802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6803 goto exit;
6804 }
6805
6806#if defined(MBEDTLS_SSL_SRV_C)
6807 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6808 {
6809 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6810
6811 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6812 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6813
6814 goto exit;
6815 }
6816#endif /* MBEDTLS_SSL_SRV_C */
6817
6818 /* Clear existing peer CRT structure in case we tried to
6819 * reuse a session but it failed, and allocate a new one. */
6820 ssl_clear_peer_cert( ssl->session_negotiate );
6821
6822 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6823 if( chain == NULL )
6824 {
6825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6826 sizeof( mbedtls_x509_crt ) ) );
6827 mbedtls_ssl_send_alert_message( ssl,
6828 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6829 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6830
6831 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6832 goto exit;
6833 }
6834 mbedtls_x509_crt_init( chain );
6835
6836 ret = ssl_parse_certificate_chain( ssl, chain );
6837 if( ret != 0 )
6838 goto exit;
6839
6840#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6841 if( ssl->handshake->ecrs_enabled)
6842 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6843
6844crt_verify:
6845 if( ssl->handshake->ecrs_enabled)
6846 rs_ctx = &ssl->handshake->ecrs_ctx;
6847#endif
6848
6849 ret = ssl_parse_certificate_verify( ssl, authmode,
6850 chain, rs_ctx );
6851 if( ret != 0 )
6852 goto exit;
6853
6854#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6855 {
6856 unsigned char *crt_start, *pk_start;
6857 size_t crt_len, pk_len;
6858
6859 /* We parse the CRT chain without copying, so
6860 * these pointers point into the input buffer,
6861 * and are hence still valid after freeing the
6862 * CRT chain. */
6863
6864 crt_start = chain->raw.p;
6865 crt_len = chain->raw.len;
6866
6867 pk_start = chain->pk_raw.p;
6868 pk_len = chain->pk_raw.len;
6869
6870 /* Free the CRT structures before computing
6871 * digest and copying the peer's public key. */
6872 mbedtls_x509_crt_free( chain );
6873 mbedtls_free( chain );
6874 chain = NULL;
6875
6876 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6877 if( ret != 0 )
6878 goto exit;
6879
6880 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6881 if( ret != 0 )
6882 goto exit;
6883 }
6884#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6885 /* Pass ownership to session structure. */
6886 ssl->session_negotiate->peer_cert = chain;
6887 chain = NULL;
6888#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6889
6890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6891
6892exit:
6893
6894 if( ret == 0 )
6895 ssl->state++;
6896
6897#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6898 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6899 {
6900 ssl->handshake->ecrs_peer_cert = chain;
6901 chain = NULL;
6902 }
6903#endif
6904
6905 if( chain != NULL )
6906 {
6907 mbedtls_x509_crt_free( chain );
6908 mbedtls_free( chain );
6909 }
6910
6911 return( ret );
6912}
6913#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6914
Andrzej Kurek25f27152022-08-17 16:09:31 -04006915#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006916static void ssl_calc_finished_tls_sha256(
6917 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6918{
6919 int len = 12;
6920 const char *sender;
6921 unsigned char padbuf[32];
6922#if defined(MBEDTLS_USE_PSA_CRYPTO)
6923 size_t hash_size;
6924 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6925 psa_status_t status;
6926#else
6927 mbedtls_sha256_context sha256;
6928#endif
6929
6930 mbedtls_ssl_session *session = ssl->session_negotiate;
6931 if( !session )
6932 session = ssl->session;
6933
6934 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6935 ? "client finished"
6936 : "server finished";
6937
6938#if defined(MBEDTLS_USE_PSA_CRYPTO)
6939 sha256_psa = psa_hash_operation_init();
6940
6941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6942
6943 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6944 if( status != PSA_SUCCESS )
6945 {
6946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6947 return;
6948 }
6949
6950 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6951 if( status != PSA_SUCCESS )
6952 {
6953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6954 return;
6955 }
6956 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6957#else
6958
6959 mbedtls_sha256_init( &sha256 );
6960
6961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6962
6963 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6964
6965 /*
6966 * TLSv1.2:
6967 * hash = PRF( master, finished_label,
6968 * Hash( handshake ) )[0.11]
6969 */
6970
6971#if !defined(MBEDTLS_SHA256_ALT)
6972 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6973 sha256.state, sizeof( sha256.state ) );
6974#endif
6975
6976 mbedtls_sha256_finish( &sha256, padbuf );
6977 mbedtls_sha256_free( &sha256 );
6978#endif /* MBEDTLS_USE_PSA_CRYPTO */
6979
6980 ssl->handshake->tls_prf( session->master, 48, sender,
6981 padbuf, 32, buf, len );
6982
6983 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6984
6985 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6986
6987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6988}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006989#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006990
Jerry Yub7ba49e2022-02-17 14:25:53 +08006991
Andrzej Kurek25f27152022-08-17 16:09:31 -04006992#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006993static void ssl_calc_finished_tls_sha384(
6994 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6995{
6996 int len = 12;
6997 const char *sender;
6998 unsigned char padbuf[48];
6999#if defined(MBEDTLS_USE_PSA_CRYPTO)
7000 size_t hash_size;
7001 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7002 psa_status_t status;
7003#else
7004 mbedtls_sha512_context sha512;
7005#endif
7006
7007 mbedtls_ssl_session *session = ssl->session_negotiate;
7008 if( !session )
7009 session = ssl->session;
7010
7011 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7012 ? "client finished"
7013 : "server finished";
7014
7015#if defined(MBEDTLS_USE_PSA_CRYPTO)
7016 sha384_psa = psa_hash_operation_init();
7017
7018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7019
7020 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7021 if( status != PSA_SUCCESS )
7022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7024 return;
7025 }
7026
7027 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7028 if( status != PSA_SUCCESS )
7029 {
7030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7031 return;
7032 }
7033 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7034#else
7035 mbedtls_sha512_init( &sha512 );
7036
7037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7038
Andrzej Kureka242e832022-08-11 10:03:14 -04007039 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007040
7041 /*
7042 * TLSv1.2:
7043 * hash = PRF( master, finished_label,
7044 * Hash( handshake ) )[0.11]
7045 */
7046
7047#if !defined(MBEDTLS_SHA512_ALT)
7048 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7049 sha512.state, sizeof( sha512.state ) );
7050#endif
7051 mbedtls_sha512_finish( &sha512, padbuf );
7052
7053 mbedtls_sha512_free( &sha512 );
7054#endif
7055
7056 ssl->handshake->tls_prf( session->master, 48, sender,
7057 padbuf, 48, buf, len );
7058
7059 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7060
7061 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7062
7063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7064}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007065#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007066
Jerry Yuaef00152022-02-17 14:27:31 +08007067void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7068{
7069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7070
7071 /*
7072 * Free our handshake params
7073 */
7074 mbedtls_ssl_handshake_free( ssl );
7075 mbedtls_free( ssl->handshake );
7076 ssl->handshake = NULL;
7077
7078 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007079 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007080 */
7081 if( ssl->transform )
7082 {
7083 mbedtls_ssl_transform_free( ssl->transform );
7084 mbedtls_free( ssl->transform );
7085 }
7086 ssl->transform = ssl->transform_negotiate;
7087 ssl->transform_negotiate = NULL;
7088
7089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7090}
7091
Jerry Yu2a9fff52022-02-17 14:28:51 +08007092void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7093{
7094 int resume = ssl->handshake->resume;
7095
7096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7097
7098#if defined(MBEDTLS_SSL_RENEGOTIATION)
7099 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7100 {
7101 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7102 ssl->renego_records_seen = 0;
7103 }
7104#endif
7105
7106 /*
7107 * Free the previous session and switch in the current one
7108 */
7109 if( ssl->session )
7110 {
7111#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7112 /* RFC 7366 3.1: keep the EtM state */
7113 ssl->session_negotiate->encrypt_then_mac =
7114 ssl->session->encrypt_then_mac;
7115#endif
7116
7117 mbedtls_ssl_session_free( ssl->session );
7118 mbedtls_free( ssl->session );
7119 }
7120 ssl->session = ssl->session_negotiate;
7121 ssl->session_negotiate = NULL;
7122
7123 /*
7124 * Add cache entry
7125 */
7126 if( ssl->conf->f_set_cache != NULL &&
7127 ssl->session->id_len != 0 &&
7128 resume == 0 )
7129 {
7130 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7131 ssl->session->id,
7132 ssl->session->id_len,
7133 ssl->session ) != 0 )
7134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7135 }
7136
7137#if defined(MBEDTLS_SSL_PROTO_DTLS)
7138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7139 ssl->handshake->flight != NULL )
7140 {
7141 /* Cancel handshake timer */
7142 mbedtls_ssl_set_timer( ssl, 0 );
7143
7144 /* Keep last flight around in case we need to resend it:
7145 * we need the handshake and transform structures for that */
7146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7147 }
7148 else
7149#endif
7150 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7151
7152 ssl->state++;
7153
7154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7155}
7156
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007157int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7158{
7159 int ret, hash_len;
7160
7161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7162
7163 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7164
7165 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7166
7167 /*
7168 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7169 * may define some other value. Currently (early 2016), no defined
7170 * ciphersuite does this (and this is unlikely to change as activity has
7171 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7172 */
7173 hash_len = 12;
7174
7175#if defined(MBEDTLS_SSL_RENEGOTIATION)
7176 ssl->verify_data_len = hash_len;
7177 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7178#endif
7179
7180 ssl->out_msglen = 4 + hash_len;
7181 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7182 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7183
7184 /*
7185 * In case of session resuming, invert the client and server
7186 * ChangeCipherSpec messages order.
7187 */
7188 if( ssl->handshake->resume != 0 )
7189 {
7190#if defined(MBEDTLS_SSL_CLI_C)
7191 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7192 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7193#endif
7194#if defined(MBEDTLS_SSL_SRV_C)
7195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7196 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7197#endif
7198 }
7199 else
7200 ssl->state++;
7201
7202 /*
7203 * Switch to our negotiated transform and session parameters for outbound
7204 * data.
7205 */
7206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7207
7208#if defined(MBEDTLS_SSL_PROTO_DTLS)
7209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7210 {
7211 unsigned char i;
7212
7213 /* Remember current epoch settings for resending */
7214 ssl->handshake->alt_transform_out = ssl->transform_out;
7215 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7216 sizeof( ssl->handshake->alt_out_ctr ) );
7217
7218 /* Set sequence_number to zero */
7219 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7220
7221
7222 /* Increment epoch */
7223 for( i = 2; i > 0; i-- )
7224 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7225 break;
7226
7227 /* The loop goes to its end iff the counter is wrapping */
7228 if( i == 0 )
7229 {
7230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7231 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7232 }
7233 }
7234 else
7235#endif /* MBEDTLS_SSL_PROTO_DTLS */
7236 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7237
7238 ssl->transform_out = ssl->transform_negotiate;
7239 ssl->session_out = ssl->session_negotiate;
7240
7241#if defined(MBEDTLS_SSL_PROTO_DTLS)
7242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7243 mbedtls_ssl_send_flight_completed( ssl );
7244#endif
7245
7246 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7247 {
7248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7249 return( ret );
7250 }
7251
7252#if defined(MBEDTLS_SSL_PROTO_DTLS)
7253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7254 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7255 {
7256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7257 return( ret );
7258 }
7259#endif
7260
7261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7262
7263 return( 0 );
7264}
7265
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007266#define SSL_MAX_HASH_LEN 12
7267
7268int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7269{
7270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7271 unsigned int hash_len = 12;
7272 unsigned char buf[SSL_MAX_HASH_LEN];
7273
7274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7275
7276 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7277
7278 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7279 {
7280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7281 goto exit;
7282 }
7283
7284 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7285 {
7286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7287 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7288 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7289 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7290 goto exit;
7291 }
7292
7293 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7294 {
7295 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7296 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7297 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7298 goto exit;
7299 }
7300
7301 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7302 {
7303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7304 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7306 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7307 goto exit;
7308 }
7309
7310 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7311 buf, hash_len ) != 0 )
7312 {
7313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7315 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7316 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7317 goto exit;
7318 }
7319
7320#if defined(MBEDTLS_SSL_RENEGOTIATION)
7321 ssl->verify_data_len = hash_len;
7322 memcpy( ssl->peer_verify_data, buf, hash_len );
7323#endif
7324
7325 if( ssl->handshake->resume != 0 )
7326 {
7327#if defined(MBEDTLS_SSL_CLI_C)
7328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7329 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7330#endif
7331#if defined(MBEDTLS_SSL_SRV_C)
7332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7333 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7334#endif
7335 }
7336 else
7337 ssl->state++;
7338
7339#if defined(MBEDTLS_SSL_PROTO_DTLS)
7340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7341 mbedtls_ssl_recv_flight_completed( ssl );
7342#endif
7343
7344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7345
7346exit:
7347 mbedtls_platform_zeroize( buf, hash_len );
7348 return( ret );
7349}
7350
Jerry Yu392112c2022-02-17 14:34:10 +08007351#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7352/*
7353 * Helper to get TLS 1.2 PRF from ciphersuite
7354 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7355 */
7356static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7357{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007358#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007359 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7360 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7361
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007362 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007363 return( tls_prf_sha384 );
7364#else
7365 (void) ciphersuite_id;
7366#endif
7367 return( tls_prf_sha256 );
7368}
7369#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007370
7371static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7372{
7373 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007374#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007375 if( tls_prf == tls_prf_sha384 )
7376 {
7377 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7378 }
7379 else
7380#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007381#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007382 if( tls_prf == tls_prf_sha256 )
7383 {
7384 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7385 }
7386 else
7387#endif
7388 return( MBEDTLS_SSL_TLS_PRF_NONE );
7389}
7390
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007391/*
7392 * Populate a transform structure with session keys and all the other
7393 * necessary information.
7394 *
7395 * Parameters:
7396 * - [in/out]: transform: structure to populate
7397 * [in] must be just initialised with mbedtls_ssl_transform_init()
7398 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7399 * - [in] ciphersuite
7400 * - [in] master
7401 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007402 * - [in] tls_prf: pointer to PRF to use for key derivation
7403 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007404 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007405 * - [in] endpoint: client or server
7406 * - [in] ssl: used for:
7407 * - ssl->conf->{f,p}_export_keys
7408 * [in] optionally used for:
7409 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7410 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007411MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007412static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7413 int ciphersuite,
7414 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007415#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007416 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007417#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007418 ssl_tls_prf_t tls_prf,
7419 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007420 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007421 unsigned endpoint,
7422 const mbedtls_ssl_context *ssl )
7423{
7424 int ret = 0;
7425 unsigned char keyblk[256];
7426 unsigned char *key1;
7427 unsigned char *key2;
7428 unsigned char *mac_enc;
7429 unsigned char *mac_dec;
7430 size_t mac_key_len = 0;
7431 size_t iv_copy_len;
7432 size_t keylen;
7433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007434 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007435#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007436 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007437 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007438#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007439
7440#if defined(MBEDTLS_USE_PSA_CRYPTO)
7441 psa_key_type_t key_type;
7442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7443 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007444 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007445 size_t key_bits;
7446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7447#endif
7448
7449#if !defined(MBEDTLS_DEBUG_C) && \
7450 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7451 if( ssl->f_export_keys == NULL )
7452 {
7453 ssl = NULL; /* make sure we don't use it except for these cases */
7454 (void) ssl;
7455 }
7456#endif
7457
7458 /*
7459 * Some data just needs copying into the structure
7460 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007461#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007462 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007463#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007464 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007465
7466#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7467 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7468#endif
7469
7470#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007471 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007472 {
7473 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7474 * generation separate. This should never happen. */
7475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7476 }
7477#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7478
7479 /*
7480 * Get various info structures
7481 */
7482 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7483 if( ciphersuite_info == NULL )
7484 {
7485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7486 ciphersuite ) );
7487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7488 }
7489
Neil Armstrongab555e02022-04-04 11:07:59 +02007490 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007491#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007492 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007493#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007494 ciphersuite_info );
7495
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007496 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7497 transform->taglen =
7498 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7499
7500#if defined(MBEDTLS_USE_PSA_CRYPTO)
7501 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7502 transform->taglen,
7503 &alg,
7504 &key_type,
7505 &key_bits ) ) != PSA_SUCCESS )
7506 {
7507 ret = psa_ssl_status_to_mbedtls( status );
7508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7509 goto end;
7510 }
7511#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007512 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7513 if( cipher_info == NULL )
7514 {
7515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7516 ciphersuite_info->cipher ) );
7517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7518 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007519#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007520
Neil Armstronge4512952022-03-08 09:08:22 +01007521#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007522 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007523 if( mac_alg == 0 )
7524 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007526 (unsigned) ciphersuite_info->mac ) );
7527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7528 }
7529#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007530 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7531 if( md_info == NULL )
7532 {
7533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7534 (unsigned) ciphersuite_info->mac ) );
7535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7536 }
Neil Armstronge4512952022-03-08 09:08:22 +01007537#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007538
7539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7540 /* Copy own and peer's CID if the use of the CID
7541 * extension has been negotiated. */
7542 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7543 {
7544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7545
7546 transform->in_cid_len = ssl->own_cid_len;
7547 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7548 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7549 transform->in_cid_len );
7550
7551 transform->out_cid_len = ssl->handshake->peer_cid_len;
7552 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7553 ssl->handshake->peer_cid_len );
7554 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7555 transform->out_cid_len );
7556 }
7557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7558
7559 /*
7560 * Compute key block using the PRF
7561 */
7562 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7563 if( ret != 0 )
7564 {
7565 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7566 return( ret );
7567 }
7568
7569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7570 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7571 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7572 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7573 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7574
7575 /*
7576 * Determine the appropriate key, IV and MAC length.
7577 */
7578
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007579#if defined(MBEDTLS_USE_PSA_CRYPTO)
7580 keylen = PSA_BITS_TO_BYTES(key_bits);
7581#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007582 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007583#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007584
7585#if defined(MBEDTLS_GCM_C) || \
7586 defined(MBEDTLS_CCM_C) || \
7587 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007588 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007589 {
7590 size_t explicit_ivlen;
7591
7592 transform->maclen = 0;
7593 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007594
7595 /* All modes haves 96-bit IVs, but the length of the static parts vary
7596 * with mode and version:
7597 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7598 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7599 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7600 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7601 * sequence number).
7602 */
7603 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007604#if defined(MBEDTLS_USE_PSA_CRYPTO)
7605 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7606#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007607 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007608#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007609 transform->fixed_ivlen = 12;
7610 else
7611 transform->fixed_ivlen = 4;
7612
7613 /* Minimum length of encrypted record */
7614 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7615 transform->minlen = explicit_ivlen + transform->taglen;
7616 }
7617 else
7618#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7619#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007620 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7621 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7622 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623 {
Neil Armstronge4512952022-03-08 09:08:22 +01007624#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007625 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007626#else
7627 size_t block_size = cipher_info->block_size;
7628#endif /* MBEDTLS_USE_PSA_CRYPTO */
7629
7630#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007631 /* Get MAC length */
7632 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7633#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007634 /* Initialize HMAC contexts */
7635 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7636 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7637 {
7638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7639 goto end;
7640 }
7641
7642 /* Get MAC length */
7643 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007644#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007645 transform->maclen = mac_key_len;
7646
7647 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007648#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007649 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007650#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007651 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007652#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007653
7654 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007655 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007656 transform->minlen = transform->maclen;
7657 else
7658 {
7659 /*
7660 * GenericBlockCipher:
7661 * 1. if EtM is in use: one block plus MAC
7662 * otherwise: * first multiple of blocklen greater than maclen
7663 * 2. IV
7664 */
7665#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007666 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667 {
7668 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007669 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 }
7671 else
7672#endif
7673 {
7674 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007675 + block_size
7676 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007677 }
7678
Glenn Strauss07c64162022-03-14 12:34:51 -04007679 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007680 {
7681 transform->minlen += transform->ivlen;
7682 }
7683 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007684 {
7685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7686 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7687 goto end;
7688 }
7689 }
7690 }
7691 else
7692#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7693 {
7694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7696 }
7697
7698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7699 (unsigned) keylen,
7700 (unsigned) transform->minlen,
7701 (unsigned) transform->ivlen,
7702 (unsigned) transform->maclen ) );
7703
7704 /*
7705 * Finally setup the cipher contexts, IVs and MAC secrets.
7706 */
7707#if defined(MBEDTLS_SSL_CLI_C)
7708 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7709 {
7710 key1 = keyblk + mac_key_len * 2;
7711 key2 = keyblk + mac_key_len * 2 + keylen;
7712
7713 mac_enc = keyblk;
7714 mac_dec = keyblk + mac_key_len;
7715
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007716 iv_copy_len = ( transform->fixed_ivlen ) ?
7717 transform->fixed_ivlen : transform->ivlen;
7718 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7719 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7720 iv_copy_len );
7721 }
7722 else
7723#endif /* MBEDTLS_SSL_CLI_C */
7724#if defined(MBEDTLS_SSL_SRV_C)
7725 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7726 {
7727 key1 = keyblk + mac_key_len * 2 + keylen;
7728 key2 = keyblk + mac_key_len * 2;
7729
7730 mac_enc = keyblk + mac_key_len;
7731 mac_dec = keyblk;
7732
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007733 iv_copy_len = ( transform->fixed_ivlen ) ?
7734 transform->fixed_ivlen : transform->ivlen;
7735 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7736 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7737 iv_copy_len );
7738 }
7739 else
7740#endif /* MBEDTLS_SSL_SRV_C */
7741 {
7742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7743 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7744 goto end;
7745 }
7746
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007747 if( ssl != NULL && ssl->f_export_keys != NULL )
7748 {
7749 ssl->f_export_keys( ssl->p_export_keys,
7750 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7751 master, 48,
7752 randbytes + 32,
7753 randbytes,
7754 tls_prf_get_type( tls_prf ) );
7755 }
7756
7757#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007758 transform->psa_alg = alg;
7759
7760 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7761 {
7762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7763 psa_set_key_algorithm( &attributes, alg );
7764 psa_set_key_type( &attributes, key_type );
7765
7766 if( ( status = psa_import_key( &attributes,
7767 key1,
7768 PSA_BITS_TO_BYTES( key_bits ),
7769 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7770 {
7771 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7772 ret = psa_ssl_status_to_mbedtls( status );
7773 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7774 goto end;
7775 }
7776
7777 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7778
7779 if( ( status = psa_import_key( &attributes,
7780 key2,
7781 PSA_BITS_TO_BYTES( key_bits ),
7782 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7783 {
7784 ret = psa_ssl_status_to_mbedtls( status );
7785 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7786 goto end;
7787 }
7788 }
7789#else
7790 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7791 cipher_info ) ) != 0 )
7792 {
7793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7794 goto end;
7795 }
7796
7797 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7798 cipher_info ) ) != 0 )
7799 {
7800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7801 goto end;
7802 }
7803
7804 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7805 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7806 MBEDTLS_ENCRYPT ) ) != 0 )
7807 {
7808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7809 goto end;
7810 }
7811
7812 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7813 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7814 MBEDTLS_DECRYPT ) ) != 0 )
7815 {
7816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7817 goto end;
7818 }
7819
7820#if defined(MBEDTLS_CIPHER_MODE_CBC)
7821 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7822 {
7823 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7824 MBEDTLS_PADDING_NONE ) ) != 0 )
7825 {
7826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7827 goto end;
7828 }
7829
7830 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7831 MBEDTLS_PADDING_NONE ) ) != 0 )
7832 {
7833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7834 goto end;
7835 }
7836 }
7837#endif /* MBEDTLS_CIPHER_MODE_CBC */
7838#endif /* MBEDTLS_USE_PSA_CRYPTO */
7839
Neil Armstrong29c0c042022-03-17 17:47:28 +01007840#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7841 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7842 For AEAD-based ciphersuites, there is nothing to do here. */
7843 if( mac_key_len != 0 )
7844 {
7845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007846 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007847
7848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007849 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007850 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7851
7852 if( ( status = psa_import_key( &attributes,
7853 mac_enc, mac_key_len,
7854 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7855 {
7856 ret = psa_ssl_status_to_mbedtls( status );
7857 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7858 goto end;
7859 }
7860
Ronald Cronfb39f152022-03-25 14:36:28 +01007861 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007862 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7863#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7864 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7865#endif
7866 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007867 /* mbedtls_ct_hmac() requires the key to be exportable */
7868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7869 PSA_KEY_USAGE_VERIFY_HASH );
7870 else
7871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7872
7873 if( ( status = psa_import_key( &attributes,
7874 mac_dec, mac_key_len,
7875 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7876 {
7877 ret = psa_ssl_status_to_mbedtls( status );
7878 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7879 goto end;
7880 }
7881#else
7882 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7883 if( ret != 0 )
7884 goto end;
7885 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7886 if( ret != 0 )
7887 goto end;
7888#endif /* MBEDTLS_USE_PSA_CRYPTO */
7889 }
7890#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7891
7892 ((void) mac_dec);
7893 ((void) mac_enc);
7894
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007895end:
7896 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7897 return( ret );
7898}
7899
Jerry Yuee40f9d2022-02-17 14:55:16 +08007900#if defined(MBEDTLS_USE_PSA_CRYPTO)
7901int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7902 unsigned char *hash, size_t *hashlen,
7903 unsigned char *data, size_t data_len,
7904 mbedtls_md_type_t md_alg )
7905{
7906 psa_status_t status;
7907 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007908 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007909
7910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7911
7912 if( ( status = psa_hash_setup( &hash_operation,
7913 hash_alg ) ) != PSA_SUCCESS )
7914 {
7915 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7916 goto exit;
7917 }
7918
7919 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7920 64 ) ) != PSA_SUCCESS )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7923 goto exit;
7924 }
7925
7926 if( ( status = psa_hash_update( &hash_operation,
7927 data, data_len ) ) != PSA_SUCCESS )
7928 {
7929 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7930 goto exit;
7931 }
7932
7933 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7934 hashlen ) ) != PSA_SUCCESS )
7935 {
7936 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7937 goto exit;
7938 }
7939
7940exit:
7941 if( status != PSA_SUCCESS )
7942 {
7943 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7944 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7945 switch( status )
7946 {
7947 case PSA_ERROR_NOT_SUPPORTED:
7948 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7949 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7950 case PSA_ERROR_BUFFER_TOO_SMALL:
7951 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7952 case PSA_ERROR_INSUFFICIENT_MEMORY:
7953 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7954 default:
7955 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7956 }
7957 }
7958 return( 0 );
7959}
7960
7961#else
7962
7963int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7964 unsigned char *hash, size_t *hashlen,
7965 unsigned char *data, size_t data_len,
7966 mbedtls_md_type_t md_alg )
7967{
7968 int ret = 0;
7969 mbedtls_md_context_t ctx;
7970 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7971 *hashlen = mbedtls_md_get_size( md_info );
7972
7973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7974
7975 mbedtls_md_init( &ctx );
7976
7977 /*
7978 * digitally-signed struct {
7979 * opaque client_random[32];
7980 * opaque server_random[32];
7981 * ServerDHParams params;
7982 * };
7983 */
7984 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7985 {
7986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7987 goto exit;
7988 }
7989 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7990 {
7991 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7992 goto exit;
7993 }
7994 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7995 {
7996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7997 goto exit;
7998 }
7999 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8000 {
8001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8002 goto exit;
8003 }
8004 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8005 {
8006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8007 goto exit;
8008 }
8009
8010exit:
8011 mbedtls_md_free( &ctx );
8012
8013 if( ret != 0 )
8014 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8015 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8016
8017 return( ret );
8018}
8019#endif /* MBEDTLS_USE_PSA_CRYPTO */
8020
Jerry Yud9d91da2022-02-17 14:57:06 +08008021#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8022
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008023/* Find the preferred hash for a given signature algorithm. */
8024unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8025 mbedtls_ssl_context *ssl,
8026 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008027{
Gabor Mezei078e8032022-04-27 21:17:56 +02008028 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008029 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008030
8031 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008032 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008033
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008034 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008035 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008036 unsigned int hash_alg_received =
8037 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8038 received_sig_algs[i] );
8039 unsigned int sig_alg_received =
8040 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8041 received_sig_algs[i] );
8042
8043 if( sig_alg == sig_alg_received )
8044 {
8045#if defined(MBEDTLS_USE_PSA_CRYPTO)
8046 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8047 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008048 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008049 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008050
Neil Armstrong96eceb82022-06-30 18:05:05 +02008051 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8052 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8053 PSA_ALG_ECDSA( psa_hash_alg ),
8054 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008055 continue;
8056
Neil Armstrong96eceb82022-06-30 18:05:05 +02008057 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008058 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8059 PSA_ALG_RSA_PKCS1V15_SIGN(
8060 psa_hash_alg ),
8061 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008062 continue;
8063 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008064#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008065
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008066 return( hash_alg_received );
8067 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008068 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008069
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008070 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008071}
8072
8073#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8074
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008075/* Serialization of TLS 1.2 sessions:
8076 *
8077 * struct {
8078 * uint64 start_time;
8079 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008080 * uint8 session_id_len; // at most 32
8081 * opaque session_id[32];
8082 * opaque master[48]; // fixed length in the standard
8083 * uint32 verify_result;
8084 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8085 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8086 * uint32 ticket_lifetime;
8087 * uint8 mfl_code; // up to 255 according to standard
8088 * uint8 encrypt_then_mac; // 0 or 1
8089 * } serialized_session_tls12;
8090 *
8091 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008092static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008093 unsigned char *buf,
8094 size_t buf_len )
8095{
8096 unsigned char *p = buf;
8097 size_t used = 0;
8098
8099#if defined(MBEDTLS_HAVE_TIME)
8100 uint64_t start;
8101#endif
8102#if defined(MBEDTLS_X509_CRT_PARSE_C)
8103#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8104 size_t cert_len;
8105#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8106#endif /* MBEDTLS_X509_CRT_PARSE_C */
8107
8108 /*
8109 * Time
8110 */
8111#if defined(MBEDTLS_HAVE_TIME)
8112 used += 8;
8113
8114 if( used <= buf_len )
8115 {
8116 start = (uint64_t) session->start;
8117
8118 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8119 p += 8;
8120 }
8121#endif /* MBEDTLS_HAVE_TIME */
8122
8123 /*
8124 * Basic mandatory fields
8125 */
8126 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008127 + 1 /* id_len */
8128 + sizeof( session->id )
8129 + sizeof( session->master )
8130 + 4; /* verify_result */
8131
8132 if( used <= buf_len )
8133 {
8134 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8135 p += 2;
8136
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008137 *p++ = MBEDTLS_BYTE_0( session->id_len );
8138 memcpy( p, session->id, 32 );
8139 p += 32;
8140
8141 memcpy( p, session->master, 48 );
8142 p += 48;
8143
8144 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8145 p += 4;
8146 }
8147
8148 /*
8149 * Peer's end-entity certificate
8150 */
8151#if defined(MBEDTLS_X509_CRT_PARSE_C)
8152#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8153 if( session->peer_cert == NULL )
8154 cert_len = 0;
8155 else
8156 cert_len = session->peer_cert->raw.len;
8157
8158 used += 3 + cert_len;
8159
8160 if( used <= buf_len )
8161 {
8162 *p++ = MBEDTLS_BYTE_2( cert_len );
8163 *p++ = MBEDTLS_BYTE_1( cert_len );
8164 *p++ = MBEDTLS_BYTE_0( cert_len );
8165
8166 if( session->peer_cert != NULL )
8167 {
8168 memcpy( p, session->peer_cert->raw.p, cert_len );
8169 p += cert_len;
8170 }
8171 }
8172#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8173 if( session->peer_cert_digest != NULL )
8174 {
8175 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8176 if( used <= buf_len )
8177 {
8178 *p++ = (unsigned char) session->peer_cert_digest_type;
8179 *p++ = (unsigned char) session->peer_cert_digest_len;
8180 memcpy( p, session->peer_cert_digest,
8181 session->peer_cert_digest_len );
8182 p += session->peer_cert_digest_len;
8183 }
8184 }
8185 else
8186 {
8187 used += 2;
8188 if( used <= buf_len )
8189 {
8190 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8191 *p++ = 0;
8192 }
8193 }
8194#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8195#endif /* MBEDTLS_X509_CRT_PARSE_C */
8196
8197 /*
8198 * Session ticket if any, plus associated data
8199 */
8200#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8201 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8202
8203 if( used <= buf_len )
8204 {
8205 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8206 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8207 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8208
8209 if( session->ticket != NULL )
8210 {
8211 memcpy( p, session->ticket, session->ticket_len );
8212 p += session->ticket_len;
8213 }
8214
8215 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8216 p += 4;
8217 }
8218#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8219
8220 /*
8221 * Misc extension-related info
8222 */
8223#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8224 used += 1;
8225
8226 if( used <= buf_len )
8227 *p++ = session->mfl_code;
8228#endif
8229
8230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8231 used += 1;
8232
8233 if( used <= buf_len )
8234 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8235#endif
8236
8237 return( used );
8238}
8239
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008240MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008241static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008242 const unsigned char *buf,
8243 size_t len )
8244{
8245#if defined(MBEDTLS_HAVE_TIME)
8246 uint64_t start;
8247#endif
8248#if defined(MBEDTLS_X509_CRT_PARSE_C)
8249#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8250 size_t cert_len;
8251#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8252#endif /* MBEDTLS_X509_CRT_PARSE_C */
8253
8254 const unsigned char *p = buf;
8255 const unsigned char * const end = buf + len;
8256
8257 /*
8258 * Time
8259 */
8260#if defined(MBEDTLS_HAVE_TIME)
8261 if( 8 > (size_t)( end - p ) )
8262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8263
8264 start = ( (uint64_t) p[0] << 56 ) |
8265 ( (uint64_t) p[1] << 48 ) |
8266 ( (uint64_t) p[2] << 40 ) |
8267 ( (uint64_t) p[3] << 32 ) |
8268 ( (uint64_t) p[4] << 24 ) |
8269 ( (uint64_t) p[5] << 16 ) |
8270 ( (uint64_t) p[6] << 8 ) |
8271 ( (uint64_t) p[7] );
8272 p += 8;
8273
8274 session->start = (time_t) start;
8275#endif /* MBEDTLS_HAVE_TIME */
8276
8277 /*
8278 * Basic mandatory fields
8279 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008280 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008281 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8282
8283 session->ciphersuite = ( p[0] << 8 ) | p[1];
8284 p += 2;
8285
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008286 session->id_len = *p++;
8287 memcpy( session->id, p, 32 );
8288 p += 32;
8289
8290 memcpy( session->master, p, 48 );
8291 p += 48;
8292
8293 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8294 ( (uint32_t) p[1] << 16 ) |
8295 ( (uint32_t) p[2] << 8 ) |
8296 ( (uint32_t) p[3] );
8297 p += 4;
8298
8299 /* Immediately clear invalid pointer values that have been read, in case
8300 * we exit early before we replaced them with valid ones. */
8301#if defined(MBEDTLS_X509_CRT_PARSE_C)
8302#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8303 session->peer_cert = NULL;
8304#else
8305 session->peer_cert_digest = NULL;
8306#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8307#endif /* MBEDTLS_X509_CRT_PARSE_C */
8308#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8309 session->ticket = NULL;
8310#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8311
8312 /*
8313 * Peer certificate
8314 */
8315#if defined(MBEDTLS_X509_CRT_PARSE_C)
8316#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8317 /* Deserialize CRT from the end of the ticket. */
8318 if( 3 > (size_t)( end - p ) )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8320
8321 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8322 p += 3;
8323
8324 if( cert_len != 0 )
8325 {
8326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8327
8328 if( cert_len > (size_t)( end - p ) )
8329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8330
8331 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8332
8333 if( session->peer_cert == NULL )
8334 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8335
8336 mbedtls_x509_crt_init( session->peer_cert );
8337
8338 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8339 p, cert_len ) ) != 0 )
8340 {
8341 mbedtls_x509_crt_free( session->peer_cert );
8342 mbedtls_free( session->peer_cert );
8343 session->peer_cert = NULL;
8344 return( ret );
8345 }
8346
8347 p += cert_len;
8348 }
8349#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8350 /* Deserialize CRT digest from the end of the ticket. */
8351 if( 2 > (size_t)( end - p ) )
8352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8353
8354 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8355 session->peer_cert_digest_len = (size_t) *p++;
8356
8357 if( session->peer_cert_digest_len != 0 )
8358 {
8359 const mbedtls_md_info_t *md_info =
8360 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8361 if( md_info == NULL )
8362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8363 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8365
8366 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8368
8369 session->peer_cert_digest =
8370 mbedtls_calloc( 1, session->peer_cert_digest_len );
8371 if( session->peer_cert_digest == NULL )
8372 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8373
8374 memcpy( session->peer_cert_digest, p,
8375 session->peer_cert_digest_len );
8376 p += session->peer_cert_digest_len;
8377 }
8378#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8379#endif /* MBEDTLS_X509_CRT_PARSE_C */
8380
8381 /*
8382 * Session ticket and associated data
8383 */
8384#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8385 if( 3 > (size_t)( end - p ) )
8386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8387
8388 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8389 p += 3;
8390
8391 if( session->ticket_len != 0 )
8392 {
8393 if( session->ticket_len > (size_t)( end - p ) )
8394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8395
8396 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8397 if( session->ticket == NULL )
8398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8399
8400 memcpy( session->ticket, p, session->ticket_len );
8401 p += session->ticket_len;
8402 }
8403
8404 if( 4 > (size_t)( end - p ) )
8405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8406
8407 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8408 ( (uint32_t) p[1] << 16 ) |
8409 ( (uint32_t) p[2] << 8 ) |
8410 ( (uint32_t) p[3] );
8411 p += 4;
8412#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8413
8414 /*
8415 * Misc extension-related info
8416 */
8417#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8418 if( 1 > (size_t)( end - p ) )
8419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8420
8421 session->mfl_code = *p++;
8422#endif
8423
8424#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8425 if( 1 > (size_t)( end - p ) )
8426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8427
8428 session->encrypt_then_mac = *p++;
8429#endif
8430
8431 /* Done, should have consumed entire buffer */
8432 if( p != end )
8433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8434
8435 return( 0 );
8436}
Jerry Yudc7bd172022-02-17 13:44:15 +08008437#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8438
XiaokangQian75d40ef2022-04-20 11:05:24 +00008439int mbedtls_ssl_validate_ciphersuite(
8440 const mbedtls_ssl_context *ssl,
8441 const mbedtls_ssl_ciphersuite_t *suite_info,
8442 mbedtls_ssl_protocol_version min_tls_version,
8443 mbedtls_ssl_protocol_version max_tls_version )
8444{
8445 (void) ssl;
8446
8447 if( suite_info == NULL )
8448 return( -1 );
8449
8450 if( ( suite_info->min_tls_version > max_tls_version ) ||
8451 ( suite_info->max_tls_version < min_tls_version ) )
8452 {
8453 return( -1 );
8454 }
8455
XiaokangQian060d8672022-04-21 09:24:56 +00008456#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008457#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8458 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8459 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8460 {
8461 return( -1 );
8462 }
8463#endif
8464
8465 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8466#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8467 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8468 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8469 {
8470 return( -1 );
8471 }
8472#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8473#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8474
8475 return( 0 );
8476}
8477
XiaokangQianeaf36512022-04-24 09:07:44 +00008478#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8479/*
8480 * Function for writing a signature algorithm extension.
8481 *
8482 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8483 * value (TLS 1.3 RFC8446):
8484 * enum {
8485 * ....
8486 * ecdsa_secp256r1_sha256( 0x0403 ),
8487 * ecdsa_secp384r1_sha384( 0x0503 ),
8488 * ecdsa_secp521r1_sha512( 0x0603 ),
8489 * ....
8490 * } SignatureScheme;
8491 *
8492 * struct {
8493 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8494 * } SignatureSchemeList;
8495 *
8496 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8497 * value (TLS 1.2 RFC5246):
8498 * enum {
8499 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8500 * sha512(6), (255)
8501 * } HashAlgorithm;
8502 *
8503 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8504 * SignatureAlgorithm;
8505 *
8506 * struct {
8507 * HashAlgorithm hash;
8508 * SignatureAlgorithm signature;
8509 * } SignatureAndHashAlgorithm;
8510 *
8511 * SignatureAndHashAlgorithm
8512 * supported_signature_algorithms<2..2^16-2>;
8513 *
8514 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8515 * generalization of the TLS 1.2 signature algorithm extension.
8516 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8517 * `SignatureScheme` field of TLS 1.3
8518 *
8519 */
8520int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8521 const unsigned char *end, size_t *out_len )
8522{
8523 unsigned char *p = buf;
8524 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8525 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8526
8527 *out_len = 0;
8528
8529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8530
8531 /* Check if we have space for header and length field:
8532 * - extension_type (2 bytes)
8533 * - extension_data_length (2 bytes)
8534 * - supported_signature_algorithms_length (2 bytes)
8535 */
8536 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8537 p += 6;
8538
8539 /*
8540 * Write supported_signature_algorithms
8541 */
8542 supported_sig_alg = p;
8543 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8544 if( sig_alg == NULL )
8545 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8546
8547 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8548 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8550 *sig_alg,
8551 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008552 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8553 continue;
8554 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8555 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8556 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008558 *sig_alg,
8559 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008560 }
8561
8562 /* Length of supported_signature_algorithms */
8563 supported_sig_alg_len = p - supported_sig_alg;
8564 if( supported_sig_alg_len == 0 )
8565 {
8566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8568 }
8569
XiaokangQianeaf36512022-04-24 09:07:44 +00008570 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008571 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008572 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8573
XiaokangQianeaf36512022-04-24 09:07:44 +00008574 *out_len = p - buf;
8575
8576#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8577 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8578#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8579 return( 0 );
8580}
8581#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8582
XiaokangQian40a35232022-05-07 09:02:40 +00008583#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008584/*
8585 * mbedtls_ssl_parse_server_name_ext
8586 *
8587 * Structure of server_name extension:
8588 *
8589 * enum {
8590 * host_name(0), (255)
8591 * } NameType;
8592 * opaque HostName<1..2^16-1>;
8593 *
8594 * struct {
8595 * NameType name_type;
8596 * select (name_type) {
8597 * case host_name: HostName;
8598 * } name;
8599 * } ServerName;
8600 * struct {
8601 * ServerName server_name_list<1..2^16-1>
8602 * } ServerNameList;
8603 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008604MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008605int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8606 const unsigned char *buf,
8607 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008608{
8609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8610 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008611 size_t server_name_list_len, hostname_len;
8612 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008613
XiaokangQianf2a94202022-05-20 06:44:24 +00008614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008615
8616 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008617 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008618 p += 2;
8619
XiaokangQian9b2b7712022-05-17 02:57:00 +00008620 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8621 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008622 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008623 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008624 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008625 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008626 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8627 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008628
8629 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8630 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008631 /* sni_name is intended to be used only during the parsing of the
8632 * ClientHello message (it is reset to NULL before the end of
8633 * the message parsing). Thus it is ok to just point to the
8634 * reception buffer and not make a copy of it.
8635 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008636 ssl->handshake->sni_name = p + 3;
8637 ssl->handshake->sni_name_len = hostname_len;
8638 if( ssl->conf->f_sni == NULL )
8639 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008640 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008641 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008642 if( ret != 0 )
8643 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008645 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8646 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008647 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8648 }
8649 return( 0 );
8650 }
8651
8652 p += hostname_len + 3;
8653 }
8654
8655 return( 0 );
8656}
8657#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8658
XiaokangQianacb39922022-06-17 10:18:48 +00008659#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008660MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008661int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8662 const unsigned char *buf,
8663 const unsigned char *end )
8664{
8665 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008666 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008667 const unsigned char *protocol_name_list;
8668 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008669 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008670
8671 /* If ALPN not configured, just ignore the extension */
8672 if( ssl->conf->alpn_list == NULL )
8673 return( 0 );
8674
8675 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008676 * RFC7301, section 3.1
8677 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008678 *
XiaokangQianc7403452022-06-23 03:24:12 +00008679 * struct {
8680 * ProtocolName protocol_name_list<2..2^16-1>
8681 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008682 */
8683
XiaokangQianc7403452022-06-23 03:24:12 +00008684 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008685 * protocol_name_list_len 2 bytes
8686 * protocol_name_len 1 bytes
8687 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008688 */
XiaokangQianacb39922022-06-17 10:18:48 +00008689 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8690
XiaokangQianc7403452022-06-23 03:24:12 +00008691 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008692 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008693 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008694 protocol_name_list = p;
8695 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008696
8697 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008698 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008699 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008700 protocol_name_len = *p++;
8701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8702 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008703 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008704 {
8705 MBEDTLS_SSL_PEND_FATAL_ALERT(
8706 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8707 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008708 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008709 }
8710
8711 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008712 }
8713
8714 /* Use our order of preference */
8715 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8716 {
8717 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 p = protocol_name_list;
8719 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008720 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008721 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008722 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008723 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008724 {
8725 ssl->alpn_chosen = *alpn;
8726 return( 0 );
8727 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008728
8729 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008730 }
8731 }
8732
XiaokangQian95d5f542022-06-24 02:29:26 +00008733 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008734 MBEDTLS_SSL_PEND_FATAL_ALERT(
8735 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8736 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8737 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8738}
8739
8740int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8741 unsigned char *buf,
8742 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008743 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008744{
8745 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008746 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008747 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008748
8749 if( ssl->alpn_chosen == NULL )
8750 {
8751 return( 0 );
8752 }
8753
XiaokangQian95d5f542022-06-24 02:29:26 +00008754 protocol_name_len = strlen( ssl->alpn_chosen );
8755 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008756
8757 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8758 /*
8759 * 0 . 1 ext identifier
8760 * 2 . 3 ext length
8761 * 4 . 5 protocol list length
8762 * 6 . 6 protocol name length
8763 * 7 . 7+n protocol name
8764 */
8765 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8766
XiaokangQian95d5f542022-06-24 02:29:26 +00008767 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008768
XiaokangQian95d5f542022-06-24 02:29:26 +00008769 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8770 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008771 /* Note: the length of the chosen protocol has been checked to be less
8772 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8773 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008774 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008775
XiaokangQian95d5f542022-06-24 02:29:26 +00008776 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008777 return ( 0 );
8778}
8779#endif /* MBEDTLS_SSL_ALPN */
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#endif /* MBEDTLS_SSL_TLS_C */