blob: 361dc8bf94d473a58ce08d8ab89a3e9fc27da8b9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -040057#include "legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
300 return( 0 );
301}
302
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500303#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
306{
307 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
308 if( resized_buffer == NULL )
309 return -1;
310
311 /* We want to copy len_new bytes when downsizing the buffer, and
312 * len_old bytes when upsizing, so we choose the smaller of two sizes,
313 * to fit one buffer into another. Size checks, ensuring that no data is
314 * lost, are done outside of this function. */
315 memcpy( resized_buffer, *buffer,
316 ( len_new < *len_old ) ? len_new : *len_old );
317 mbedtls_platform_zeroize( *buffer, *len_old );
318 mbedtls_free( *buffer );
319
320 *buffer = resized_buffer;
321 *len_old = len_new;
322
323 return 0;
324}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325
326static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500327 size_t in_buf_new_len,
328 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200329{
330 int modified = 0;
331 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
332 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
333 if( ssl->in_buf != NULL )
334 {
335 written_in = ssl->in_msg - ssl->in_buf;
336 iv_offset_in = ssl->in_iv - ssl->in_buf;
337 len_offset_in = ssl->in_len - ssl->in_buf;
338 if( downsizing ?
339 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
340 ssl->in_buf_len < in_buf_new_len )
341 {
342 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
345 }
346 else
347 {
Paul Elliottb7449902021-03-10 18:14:58 +0000348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
349 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 modified = 1;
351 }
352 }
353 }
354
355 if( ssl->out_buf != NULL )
356 {
357 written_out = ssl->out_msg - ssl->out_buf;
358 iv_offset_out = ssl->out_iv - ssl->out_buf;
359 len_offset_out = ssl->out_len - ssl->out_buf;
360 if( downsizing ?
361 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
362 ssl->out_buf_len < out_buf_new_len )
363 {
364 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
365 {
366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
367 }
368 else
369 {
Paul Elliottb7449902021-03-10 18:14:58 +0000370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
371 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 modified = 1;
373 }
374 }
375 }
376 if( modified )
377 {
378 /* Update pointers here to avoid doing it twice. */
379 mbedtls_ssl_reset_in_out_pointers( ssl );
380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Jerry Yudb8c48a2022-01-27 14:54:54 +0800393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
396typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
397 const char *label,
398 const unsigned char *random, size_t rlen,
399 unsigned char *dstbuf, size_t dlen );
400
401static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
402
403#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
404
405/* Type for the TLS PRF */
406typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
407 const unsigned char *, size_t,
408 unsigned char *, size_t);
409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800411static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
412 int ciphersuite,
413 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800415 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200416#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800417 ssl_tls_prf_t tls_prf,
418 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400419 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800420 unsigned endpoint,
421 const mbedtls_ssl_context *ssl );
422
Andrzej Kurek25f27152022-08-17 16:09:31 -0400423#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800425static int tls_prf_sha256( const unsigned char *secret, size_t slen,
426 const char *label,
427 const unsigned char *random, size_t rlen,
428 unsigned char *dstbuf, size_t dlen );
429static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
430static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
431
Andrzej Kurek25f27152022-08-17 16:09:31 -0400432#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_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 Kurek25f27152022-08-17 16:09:31 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_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 Kurek25f27152022-08-17 16:09:31 -0400459#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_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 Kurek25f27152022-08-17 16:09:31 -0400463#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_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 Kurek25f27152022-08-17 16:09:31 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA */
481#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 Kurek25f27152022-08-17 16:09:31 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 default:
488 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
489 }
490
491 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
495static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
498 if( session->peer_cert != NULL )
499 {
500 mbedtls_x509_crt_free( session->peer_cert );
501 mbedtls_free( session->peer_cert );
502 session->peer_cert = NULL;
503 }
504#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
505 if( session->peer_cert_digest != NULL )
506 {
507 /* Zeroization is not necessary. */
508 mbedtls_free( session->peer_cert_digest );
509 session->peer_cert_digest = NULL;
510 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
511 session->peer_cert_digest_len = 0;
512 }
513#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
514}
515#endif /* MBEDTLS_X509_CRT_PARSE_C */
516
517void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
519{
520 ((void) ciphersuite_info);
521
Andrzej Kurek25f27152022-08-17 16:09:31 -0400522#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800523 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
524 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
525 else
526#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400527#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800528 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
529 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
530 else
531#endif
532 {
533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
534 return;
535 }
536}
537
XiaokangQianadab9a62022-07-18 07:41:26 +0000538void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
539 unsigned hs_type,
540 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100541{
542 unsigned char hs_hdr[4];
543
544 /* Build HS header for checksum update. */
545 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
546 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
547 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
548 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
549
550 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
551}
552
553void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
554 unsigned hs_type,
555 unsigned char const *msg,
556 size_t msg_len )
557{
558 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
559 ssl->handshake->update_checksum( ssl, msg, msg_len );
560}
561
Jerry Yuc73c6182022-02-08 20:29:25 +0800562void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
563{
564 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400565#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800566#if defined(MBEDTLS_USE_PSA_CRYPTO)
567 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
568 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
569#else
570 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
571#endif
572#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800574#if defined(MBEDTLS_USE_PSA_CRYPTO)
575 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
576 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
577#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400578 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800579#endif
580#endif
581}
582
583static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
584 const unsigned char *buf, size_t len )
585{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800587#if defined(MBEDTLS_USE_PSA_CRYPTO)
588 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
589#else
590 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
591#endif
592#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
596#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400597 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#endif
599#endif
600}
601
Andrzej Kurek25f27152022-08-17 16:09:31 -0400602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800603static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
604 const unsigned char *buf, size_t len )
605{
606#if defined(MBEDTLS_USE_PSA_CRYPTO)
607 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
608#else
609 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
610#endif
611}
612#endif
613
Andrzej Kurek25f27152022-08-17 16:09:31 -0400614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800615static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
616 const unsigned char *buf, size_t len )
617{
618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
620#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400621 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800622#endif
623}
624#endif
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200629
Andrzej Kurek25f27152022-08-17 16:09:31 -0400630#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 handshake->fin_sha256_psa = psa_hash_operation_init();
633 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
634#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200636 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500641 handshake->fin_sha384_psa = psa_hash_operation_init();
642 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500643#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400644 mbedtls_sha512_init( &handshake->fin_sha384 );
645 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648
649 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_DHM_C)
652 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200653#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200654#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200658 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200659#if defined(MBEDTLS_SSL_CLI_C)
660 handshake->ecjpake_cache = NULL;
661 handshake->ecjpake_cache_len = 0;
662#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200663#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200664
Gilles Peskineeccd8882020-03-10 12:19:08 +0100665#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200666 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200667#endif
668
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
670 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
671#endif
Hanno Becker75173122019-02-06 16:18:31 +0000672
673#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
674 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
675 mbedtls_pk_init( &handshake->peer_pubkey );
676#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200677}
678
Hanno Beckera18d1322018-01-03 14:27:32 +0000679void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200682
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100683#if defined(MBEDTLS_USE_PSA_CRYPTO)
684 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
685 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100686#else
687 mbedtls_cipher_init( &transform->cipher_ctx_enc );
688 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100689#endif
690
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000691#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100692#if defined(MBEDTLS_USE_PSA_CRYPTO)
693 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
694 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100695#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_init( &transform->md_ctx_enc );
697 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000698#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100699#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200700}
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705}
706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200707MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000709{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000711 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200716 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717
718 /*
719 * Either the pointers are now NULL or cleared properly and can be freed.
720 * Now allocate missing structures.
721 */
722 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200724 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200725 }
Paul Bakker48916f92012-09-16 19:57:18 +0000726
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200728 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200729 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 }
Paul Bakker48916f92012-09-16 19:57:18 +0000731
Paul Bakker82788fb2014-10-20 13:59:19 +0200732 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500736#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
737 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400738
Andrzej Kurek4a063792020-10-21 15:08:44 +0200739 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
740 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500741#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200743 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000744 if( ssl->handshake == NULL ||
745 ssl->transform_negotiate == NULL ||
746 ssl->session_negotiate == NULL )
747 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_free( ssl->handshake );
751 mbedtls_free( ssl->transform_negotiate );
752 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753
754 ssl->handshake = NULL;
755 ssl->transform_negotiate = NULL;
756 ssl->session_negotiate = NULL;
757
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 }
760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000763 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200764 ssl_handshake_params_init( ssl->handshake );
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
768 {
769 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200770
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
772 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
773 else
774 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200775
Hanno Becker0f57a652020-02-05 10:37:26 +0000776 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200777 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200778#endif
779
Brett Warrene0edc842021-08-17 09:53:13 +0100780/*
781 * curve_list is translated to IANA TLS group identifiers here because
782 * mbedtls_ssl_conf_curves returns void and so can't return
783 * any error codes.
784 */
785#if defined(MBEDTLS_ECP_C)
786#if !defined(MBEDTLS_DEPRECATED_REMOVED)
787 /* Heap allocate and translate curve_list from internal to IANA group ids */
788 if ( ssl->conf->curve_list != NULL )
789 {
790 size_t length;
791 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
792
793 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
794 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
795
796 /* Leave room for zero termination */
797 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
798 if ( group_list == NULL )
799 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
800
801 for( size_t i = 0; i < length; i++ )
802 {
803 const mbedtls_ecp_curve_info *info =
804 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
805 if ( info == NULL )
806 {
807 mbedtls_free( group_list );
808 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
809 }
810 group_list[i] = info->tls_id;
811 }
812
813 group_list[length] = 0;
814
815 ssl->handshake->group_list = group_list;
816 ssl->handshake->group_list_heap_allocated = 1;
817 }
818 else
819 {
820 ssl->handshake->group_list = ssl->conf->group_list;
821 ssl->handshake->group_list_heap_allocated = 0;
822 }
823#endif /* MBEDTLS_DEPRECATED_REMOVED */
824#endif /* MBEDTLS_ECP_C */
825
Jerry Yuf017ee42022-01-12 15:49:48 +0800826#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
827#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800829 /* Heap allocate and translate sig_hashes from internal hash identifiers to
830 signature algorithms IANA identifiers. */
831 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800832 ssl->conf->sig_hashes != NULL )
833 {
834 const int *md;
835 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800836 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800837 uint16_t *p;
838
Jerry Yub476a442022-01-21 18:14:45 +0800839#if defined(static_assert)
840 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
841 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
842 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800843#endif
844
Jerry Yuf017ee42022-01-12 15:49:48 +0800845 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
846 {
847 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
848 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800849#if defined(MBEDTLS_ECDSA_C)
850 sig_algs_len += sizeof( uint16_t );
851#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800852
Jerry Yub476a442022-01-21 18:14:45 +0800853#if defined(MBEDTLS_RSA_C)
854 sig_algs_len += sizeof( uint16_t );
855#endif
856 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
857 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800858 }
859
Jerry Yub476a442022-01-21 18:14:45 +0800860 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800861 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
862
Jerry Yub476a442022-01-21 18:14:45 +0800863 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
864 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 if( ssl->handshake->sig_algs == NULL )
866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
867
868 p = (uint16_t *)ssl->handshake->sig_algs;
869 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
870 {
871 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
872 if( hash == MBEDTLS_SSL_HASH_NONE )
873 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800874#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800875 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
876 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800877#endif
878#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
880 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800881#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200883 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800884 ssl->handshake->sig_algs_heap_allocated = 1;
885 }
886 else
Jerry Yua69269a2022-01-17 21:06:01 +0800887#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800888 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 ssl->handshake->sig_algs_heap_allocated = 0;
890 }
Jerry Yucc539102022-06-27 16:27:35 +0800891#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800892#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000893 return( 0 );
894}
895
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200896#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200897/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200898MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200899static int ssl_cookie_write_dummy( void *ctx,
900 unsigned char **p, unsigned char *end,
901 const unsigned char *cli_id, size_t cli_id_len )
902{
903 ((void) ctx);
904 ((void) p);
905 ((void) end);
906 ((void) cli_id);
907 ((void) cli_id_len);
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200910}
911
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200912MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200913static int ssl_cookie_check_dummy( void *ctx,
914 const unsigned char *cookie, size_t cookie_len,
915 const unsigned char *cli_id, size_t cli_id_len )
916{
917 ((void) ctx);
918 ((void) cookie);
919 ((void) cookie_len);
920 ((void) cli_id);
921 ((void) cli_id_len);
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200925#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200926
Paul Bakker5121ce52009-01-03 21:22:43 +0000927/*
928 * Initialize an SSL context
929 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200930void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
931{
932 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
933}
934
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200935MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800936static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
937{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100938 const mbedtls_ssl_config *conf = ssl->conf;
939
Ronald Cron6f135e12021-12-08 16:57:54 +0100940#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100941 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
942 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000943 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
944 {
945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
947 }
948
Jerry Yu60835a82021-08-04 10:13:52 +0800949 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
950 return( 0 );
951 }
952#endif
953
954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100955 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800956 {
957 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
958 return( 0 );
959 }
960#endif
961
Ronald Cron6f135e12021-12-08 16:57:54 +0100962#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100963 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800964 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000965 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
966 {
967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
968 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
969 }
970
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000971 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
972 {
973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977
Ronald Crone1d3f062022-02-10 14:50:54 +0100978 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
979 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800980 }
981#endif
982
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
984 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
985}
986
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200987MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800988static int ssl_conf_check(const mbedtls_ssl_context *ssl)
989{
990 int ret;
991 ret = ssl_conf_version_check( ssl );
992 if( ret != 0 )
993 return( ret );
994
995 /* Space for further checks */
996
997 return( 0 );
998}
999
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001000/*
1001 * Setup an SSL context
1002 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001003
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001004int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001005 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006{
Janos Follath865b3eb2019-12-16 11:46:15 +00001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001008 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1009 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001011 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001012
Jerry Yu60835a82021-08-04 10:13:52 +08001013 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1014 return( ret );
1015
Paul Bakker62f2dee2012-09-28 07:31:51 +00001016 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001017 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001018 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001019
1020 /* Set to NULL in case of an error condition */
1021 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001022
Darryl Greenb33cc762019-11-28 14:29:44 +00001023#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1024 ssl->in_buf_len = in_buf_len;
1025#endif
1026 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001027 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001030 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001031 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001032 }
1033
Darryl Greenb33cc762019-11-28 14:29:44 +00001034#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1035 ssl->out_buf_len = out_buf_len;
1036#endif
1037 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001038 if( ssl->out_buf == NULL )
1039 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001041 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001042 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 }
1044
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001045 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001046
Johan Pascalb62bb512015-12-03 21:56:45 +01001047#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001048 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001049#endif
1050
Paul Bakker48916f92012-09-16 19:57:18 +00001051 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001052 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
1054 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001055
1056error:
1057 mbedtls_free( ssl->in_buf );
1058 mbedtls_free( ssl->out_buf );
1059
1060 ssl->conf = NULL;
1061
Darryl Greenb33cc762019-11-28 14:29:44 +00001062#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1063 ssl->in_buf_len = 0;
1064 ssl->out_buf_len = 0;
1065#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001066 ssl->in_buf = NULL;
1067 ssl->out_buf = NULL;
1068
1069 ssl->in_hdr = NULL;
1070 ssl->in_ctr = NULL;
1071 ssl->in_len = NULL;
1072 ssl->in_iv = NULL;
1073 ssl->in_msg = NULL;
1074
1075 ssl->out_hdr = NULL;
1076 ssl->out_ctr = NULL;
1077 ssl->out_len = NULL;
1078 ssl->out_iv = NULL;
1079 ssl->out_msg = NULL;
1080
k-stachowiak9f7798e2018-07-31 16:52:32 +02001081 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082}
1083
1084/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001085 * Reset an initialized and used SSL context for re-use while retaining
1086 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001087 *
1088 * If partial is non-zero, keep data in the input buffer and client ID.
1089 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001090 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001091void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1092 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001093{
Darryl Greenb33cc762019-11-28 14:29:44 +00001094#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1095 size_t in_buf_len = ssl->in_buf_len;
1096 size_t out_buf_len = ssl->out_buf_len;
1097#else
1098 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1099 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1100#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001101
Hanno Beckerb0302c42021-08-03 09:39:42 +01001102#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1103 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001104#endif
1105
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001106 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001107 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001108
Hanno Beckerb0302c42021-08-03 09:39:42 +01001109 mbedtls_ssl_reset_in_out_pointers( ssl );
1110
1111 /* Reset incoming message parsing */
1112 ssl->in_offt = NULL;
1113 ssl->nb_zero = 0;
1114 ssl->in_msgtype = 0;
1115 ssl->in_msglen = 0;
1116 ssl->in_hslen = 0;
1117 ssl->keep_current_message = 0;
1118 ssl->transform_in = NULL;
1119
1120#if defined(MBEDTLS_SSL_PROTO_DTLS)
1121 ssl->next_record_offset = 0;
1122 ssl->in_epoch = 0;
1123#endif
1124
1125 /* Keep current datagram if partial == 1 */
1126 if( partial == 0 )
1127 {
1128 ssl->in_left = 0;
1129 memset( ssl->in_buf, 0, in_buf_len );
1130 }
1131
Ronald Cronad8c17b2022-06-10 17:18:09 +02001132 ssl->send_alert = 0;
1133
Hanno Beckerb0302c42021-08-03 09:39:42 +01001134 /* Reset outgoing message writing */
1135 ssl->out_msgtype = 0;
1136 ssl->out_msglen = 0;
1137 ssl->out_left = 0;
1138 memset( ssl->out_buf, 0, out_buf_len );
1139 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1140 ssl->transform_out = NULL;
1141
1142#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1143 mbedtls_ssl_dtls_replay_reset( ssl );
1144#endif
1145
XiaokangQian2b01dc32022-01-21 02:53:13 +00001146#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001147 if( ssl->transform )
1148 {
1149 mbedtls_ssl_transform_free( ssl->transform );
1150 mbedtls_free( ssl->transform );
1151 ssl->transform = NULL;
1152 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1154
XiaokangQian2b01dc32022-01-21 02:53:13 +00001155#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1156 mbedtls_ssl_transform_free( ssl->transform_application );
1157 mbedtls_free( ssl->transform_application );
1158 ssl->transform_application = NULL;
1159
1160 if( ssl->handshake != NULL )
1161 {
1162 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1163 mbedtls_free( ssl->handshake->transform_earlydata );
1164 ssl->handshake->transform_earlydata = NULL;
1165
1166 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1167 mbedtls_free( ssl->handshake->transform_handshake );
1168 ssl->handshake->transform_handshake = NULL;
1169 }
1170
XiaokangQian2b01dc32022-01-21 02:53:13 +00001171#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001172}
1173
1174int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1175{
1176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1177
1178 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1179
XiaokangQian78b1fa72022-01-19 06:56:30 +00001180 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001181
1182 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_SSL_RENEGOTIATION)
1184 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001185 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001186
1187 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1189 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001192
Hanno Beckerb0302c42021-08-03 09:39:42 +01001193 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001194 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001195 if( ssl->session )
1196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_ssl_session_free( ssl->session );
1198 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001199 ssl->session = NULL;
1200 }
1201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001203 ssl->alpn_chosen = NULL;
1204#endif
1205
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001206#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001207#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001208 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001209#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001210 {
1211 mbedtls_free( ssl->cli_id );
1212 ssl->cli_id = NULL;
1213 ssl->cli_id_len = 0;
1214 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001215#endif
1216
Paul Bakker48916f92012-09-16 19:57:18 +00001217 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1218 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001219
1220 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001221}
1222
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001223/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001224 * Reset an initialized and used SSL context for re-use while retaining
1225 * all application-set variables, function pointers and data.
1226 */
1227int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1228{
Hanno Becker43aefe22020-02-05 10:44:56 +00001229 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001230}
1231
1232/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 * SSL set accessors
1234 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001235void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001236{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001237 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238}
1239
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001240void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001241{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001242 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001243}
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001246void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001247{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001248 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001249}
1250#endif
1251
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001252void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001254 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001255}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001258
Hanno Becker1841b0a2018-08-24 11:13:57 +01001259void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1260 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001261{
1262 ssl->disable_datagram_packing = !allow_packing;
1263}
1264
1265void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1266 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001268 conf->hs_timeout_min = min;
1269 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001270}
1271#endif
1272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001273void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001276}
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001279void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001280 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001281 void *p_vrfy )
1282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001283 conf->f_vrfy = f_vrfy;
1284 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001287
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001288void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001289 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 void *p_rng )
1291{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001292 conf->f_rng = f_rng;
1293 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001296void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001297 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 void *p_dbg )
1299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001300 conf->f_dbg = f_dbg;
1301 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001305 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001306 mbedtls_ssl_send_t *f_send,
1307 mbedtls_ssl_recv_t *f_recv,
1308 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001309{
1310 ssl->p_bio = p_bio;
1311 ssl->f_send = f_send;
1312 ssl->f_recv = f_recv;
1313 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001314}
1315
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001316#if defined(MBEDTLS_SSL_PROTO_DTLS)
1317void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1318{
1319 ssl->mtu = mtu;
1320}
1321#endif
1322
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001323void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001324{
1325 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001326}
1327
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001328void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1329 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001330 mbedtls_ssl_set_timer_t *f_set_timer,
1331 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001332{
1333 ssl->p_timer = p_timer;
1334 ssl->f_set_timer = f_set_timer;
1335 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001336
1337 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001338 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001339}
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001342void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001343 void *p_cache,
1344 mbedtls_ssl_cache_get_t *f_get_cache,
1345 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001347 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001348 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001349 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_SSL_CLI_C)
1354int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001355{
Janos Follath865b3eb2019-12-16 11:46:15 +00001356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001357
1358 if( ssl == NULL ||
1359 session == NULL ||
1360 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001361 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001364 }
1365
Hanno Beckere810bbc2021-05-14 16:01:05 +01001366 if( ssl->handshake->resume == 1 )
1367 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1368
Hanno Becker52055ae2019-02-06 14:30:46 +00001369 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1370 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371 return( ret );
1372
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001374
1375 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001379void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1380 const int *ciphersuites )
1381{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001382 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383}
1384
Ronald Cron6f135e12021-12-08 16:57:54 +01001385#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001386void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001387 const int kex_modes )
1388{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001389 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001390}
Ronald Cron6f135e12021-12-08 16:57:54 +01001391#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001394void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001395 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001396{
1397 conf->cert_profile = profile;
1398}
1399
Glenn Strauss36872db2022-01-22 05:06:31 -05001400static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1401{
1402 mbedtls_ssl_key_cert *cur = key_cert, *next;
1403
1404 while( cur != NULL )
1405 {
1406 next = cur->next;
1407 mbedtls_free( cur );
1408 cur = next;
1409 }
1410}
1411
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001412/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001413MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001414static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1415 mbedtls_x509_crt *cert,
1416 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001417{
niisato8ee24222018-06-25 19:05:48 +09001418 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001419
Glenn Strauss36872db2022-01-22 05:06:31 -05001420 if( cert == NULL )
1421 {
1422 /* Free list if cert is null */
1423 ssl_key_cert_free( *head );
1424 *head = NULL;
1425 return( 0 );
1426 }
1427
niisato8ee24222018-06-25 19:05:48 +09001428 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1429 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001431
niisato8ee24222018-06-25 19:05:48 +09001432 new_cert->cert = cert;
1433 new_cert->key = key;
1434 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001435
Glenn Strauss36872db2022-01-22 05:06:31 -05001436 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001437 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001438 {
niisato8ee24222018-06-25 19:05:48 +09001439 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001440 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001441 else
1442 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001443 mbedtls_ssl_key_cert *cur = *head;
1444 while( cur->next != NULL )
1445 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001446 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001447 }
1448
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001449 return( 0 );
1450}
1451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001452int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001453 mbedtls_x509_crt *own_cert,
1454 mbedtls_pk_context *pk_key )
1455{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001456 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001457}
1458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001460 mbedtls_x509_crt *ca_chain,
1461 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001463 conf->ca_chain = ca_chain;
1464 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001465
1466#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1467 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1468 * cannot be used together. */
1469 conf->f_ca_cb = NULL;
1470 conf->p_ca_cb = NULL;
1471#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001472}
Hanno Becker5adaad92019-03-27 16:54:37 +00001473
1474#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1475void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001476 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001477 void *p_ca_cb )
1478{
1479 conf->f_ca_cb = f_ca_cb;
1480 conf->p_ca_cb = p_ca_cb;
1481
1482 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1483 * cannot be used together. */
1484 conf->ca_chain = NULL;
1485 conf->ca_crl = NULL;
1486}
1487#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001489
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001490#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001491const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1492 size_t *name_len )
1493{
1494 *name_len = ssl->handshake->sni_name_len;
1495 return( ssl->handshake->sni_name );
1496}
1497
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001498int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1499 mbedtls_x509_crt *own_cert,
1500 mbedtls_pk_context *pk_key )
1501{
1502 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1503 own_cert, pk_key ) );
1504}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001505
1506void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1507 mbedtls_x509_crt *ca_chain,
1508 mbedtls_x509_crl *ca_crl )
1509{
1510 ssl->handshake->sni_ca_chain = ca_chain;
1511 ssl->handshake->sni_ca_crl = ca_crl;
1512}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001513
Glenn Strauss999ef702022-03-11 01:37:23 -05001514#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1515void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1516 const mbedtls_x509_crt *crt)
1517{
1518 ssl->handshake->dn_hints = crt;
1519}
1520#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1521
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001522void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1523 int authmode )
1524{
1525 ssl->handshake->sni_authmode = authmode;
1526}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001527#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1528
Hanno Becker8927c832019-04-03 12:52:50 +01001529#if defined(MBEDTLS_X509_CRT_PARSE_C)
1530void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1531 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1532 void *p_vrfy )
1533{
1534 ssl->f_vrfy = f_vrfy;
1535 ssl->p_vrfy = p_vrfy;
1536}
1537#endif
1538
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001539#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001540/*
1541 * Set EC J-PAKE password for current handshake
1542 */
1543int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1544 const unsigned char *pw,
1545 size_t pw_len )
1546{
1547 mbedtls_ecjpake_role role;
1548
Janos Follath8eb64132016-06-03 15:40:57 +01001549 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1551
1552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1553 role = MBEDTLS_ECJPAKE_SERVER;
1554 else
1555 role = MBEDTLS_ECJPAKE_CLIENT;
1556
1557 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1558 role,
1559 MBEDTLS_MD_SHA256,
1560 MBEDTLS_ECP_DP_SECP256R1,
1561 pw, pw_len ) );
1562}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001563#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001564
Gilles Peskineeccd8882020-03-10 12:19:08 +01001565#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001566
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001567MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001568static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1569{
1570#if defined(MBEDTLS_USE_PSA_CRYPTO)
1571 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1572 return( 1 );
1573#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001574 if( conf->psk != NULL )
1575 return( 1 );
1576
1577 return( 0 );
1578}
1579
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001580static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1581{
1582 /* Remove reference to existing PSK, if any. */
1583#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001584 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001585 {
1586 /* The maintenance of the PSK key slot is the
1587 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001588 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001589 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001590#endif /* MBEDTLS_USE_PSA_CRYPTO */
1591 if( conf->psk != NULL )
1592 {
1593 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1594
1595 mbedtls_free( conf->psk );
1596 conf->psk = NULL;
1597 conf->psk_len = 0;
1598 }
1599
1600 /* Remove reference to PSK identity, if any. */
1601 if( conf->psk_identity != NULL )
1602 {
1603 mbedtls_free( conf->psk_identity );
1604 conf->psk_identity = NULL;
1605 conf->psk_identity_len = 0;
1606 }
1607}
1608
Hanno Becker7390c712018-11-15 13:33:04 +00001609/* This function assumes that PSK identity in the SSL config is unset.
1610 * It checks that the provided identity is well-formed and attempts
1611 * to make a copy of it in the SSL config.
1612 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001613MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001614static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1615 unsigned char const *psk_identity,
1616 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001617{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001618 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001619 if( psk_identity == NULL ||
1620 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001621 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001622 {
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1624 }
1625
Hanno Becker7390c712018-11-15 13:33:04 +00001626 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1627 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001628 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001629
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001630 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001631 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001632
1633 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001634}
1635
Hanno Becker7390c712018-11-15 13:33:04 +00001636int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1637 const unsigned char *psk, size_t psk_len,
1638 const unsigned char *psk_identity, size_t psk_identity_len )
1639{
Janos Follath865b3eb2019-12-16 11:46:15 +00001640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001641
1642 /* We currently only support one PSK, raw or opaque. */
1643 if( ssl_conf_psk_is_configured( conf ) )
1644 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001645
1646 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001647 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001649 if( psk_len == 0 )
1650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1651 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1653
Hanno Becker7390c712018-11-15 13:33:04 +00001654 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1655 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1656 conf->psk_len = psk_len;
1657 memcpy( conf->psk, psk, conf->psk_len );
1658
1659 /* Check and set PSK Identity */
1660 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1661 if( ret != 0 )
1662 ssl_conf_remove_psk( conf );
1663
1664 return( ret );
1665}
1666
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001667static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1668{
1669#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001670 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001671 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001672 /* The maintenance of the external PSK key slot is the
1673 * user's responsibility. */
1674 if( ssl->handshake->psk_opaque_is_internal )
1675 {
1676 psa_destroy_key( ssl->handshake->psk_opaque );
1677 ssl->handshake->psk_opaque_is_internal = 0;
1678 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001679 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001680 }
Neil Armstronge952a302022-05-03 10:22:14 +02001681#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001682 if( ssl->handshake->psk != NULL )
1683 {
1684 mbedtls_platform_zeroize( ssl->handshake->psk,
1685 ssl->handshake->psk_len );
1686 mbedtls_free( ssl->handshake->psk );
1687 ssl->handshake->psk_len = 0;
1688 }
Neil Armstronge952a302022-05-03 10:22:14 +02001689#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001690}
1691
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001692int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1693 const unsigned char *psk, size_t psk_len )
1694{
Neil Armstrong501c9322022-05-03 09:35:09 +02001695#if defined(MBEDTLS_USE_PSA_CRYPTO)
1696 psa_key_attributes_t key_attributes = psa_key_attributes_init();
1697 psa_status_t status;
1698 psa_algorithm_t alg;
1699 mbedtls_svc_key_id_t key;
1700#endif /* MBEDTLS_USE_PSA_CRYPTO */
1701
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001702 if( psk == NULL || ssl->handshake == NULL )
1703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1704
1705 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1707
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001708 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001709
Neil Armstrong501c9322022-05-03 09:35:09 +02001710#if defined(MBEDTLS_USE_PSA_CRYPTO)
1711 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384)
1712 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1713 else
1714 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1715
Jerry Yu568ec252022-07-22 21:27:34 +08001716#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1c105562022-07-10 06:32:38 +00001717 psa_set_key_usage_flags( &key_attributes,
1718 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
Jerry Yu568ec252022-07-22 21:27:34 +08001719#else
Neil Armstrong501c9322022-05-03 09:35:09 +02001720 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
Jerry Yu568ec252022-07-22 21:27:34 +08001721#endif
Neil Armstrong501c9322022-05-03 09:35:09 +02001722 psa_set_key_algorithm( &key_attributes, alg );
1723 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1724
1725 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1726 if( status != PSA_SUCCESS )
1727 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1728
1729 /* Allow calling psa_destroy_key() on psk remove */
1730 ssl->handshake->psk_opaque_is_internal = 1;
1731 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1732#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001733 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001734 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001735
1736 ssl->handshake->psk_len = psk_len;
1737 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1738
1739 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001740#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001741}
1742
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001743#if defined(MBEDTLS_USE_PSA_CRYPTO)
1744int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001745 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001746 const unsigned char *psk_identity,
1747 size_t psk_identity_len )
1748{
Janos Follath865b3eb2019-12-16 11:46:15 +00001749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001750
1751 /* We currently only support one PSK, raw or opaque. */
1752 if( ssl_conf_psk_is_configured( conf ) )
1753 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001754
Hanno Becker7390c712018-11-15 13:33:04 +00001755 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001756 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001759
1760 /* Check and set PSK Identity */
1761 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1762 psk_identity_len );
1763 if( ret != 0 )
1764 ssl_conf_remove_psk( conf );
1765
1766 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001767}
1768
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001769int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1770 mbedtls_svc_key_id_t psk )
1771{
1772 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1773 ( ssl->handshake == NULL ) )
1774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1775
1776 ssl_remove_psk( ssl );
1777 ssl->handshake->psk_opaque = psk;
1778 return( 0 );
1779}
1780#endif /* MBEDTLS_USE_PSA_CRYPTO */
1781
1782void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1783 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1784 size_t),
1785 void *p_psk )
1786{
1787 conf->f_psk = f_psk;
1788 conf->p_psk = p_psk;
1789}
1790#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1791
1792#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001793static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1794 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001795{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001796#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001797 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001798 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001799#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001800 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001801 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001802 return( MBEDTLS_SSL_MODE_STREAM );
1803}
1804
1805#else /* MBEDTLS_USE_PSA_CRYPTO */
1806
1807static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1808 mbedtls_cipher_mode_t mode )
1809{
1810#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1811 if( mode == MBEDTLS_MODE_CBC )
1812 return( MBEDTLS_SSL_MODE_CBC );
1813#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1814
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001815#if defined(MBEDTLS_GCM_C) || \
1816 defined(MBEDTLS_CCM_C) || \
1817 defined(MBEDTLS_CHACHAPOLY_C)
1818 if( mode == MBEDTLS_MODE_GCM ||
1819 mode == MBEDTLS_MODE_CCM ||
1820 mode == MBEDTLS_MODE_CHACHAPOLY )
1821 return( MBEDTLS_SSL_MODE_AEAD );
1822#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001823
1824 return( MBEDTLS_SSL_MODE_STREAM );
1825}
Gilles Peskine301711e2022-04-26 16:57:05 +02001826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001827
Gilles Peskinee108d982022-04-26 16:50:40 +02001828static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1829 mbedtls_ssl_mode_t base_mode,
1830 int encrypt_then_mac )
1831{
1832#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1833 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1834 base_mode == MBEDTLS_SSL_MODE_CBC )
1835 {
1836 return( MBEDTLS_SSL_MODE_CBC_ETM );
1837 }
1838#else
1839 (void) encrypt_then_mac;
1840#endif
1841 return( base_mode );
1842}
1843
Neil Armstrongab555e02022-04-04 11:07:59 +02001844mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001845 const mbedtls_ssl_transform *transform )
1846{
Gilles Peskinee108d982022-04-26 16:50:40 +02001847 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001849 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001850#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001851 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1852#endif
1853 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001854
Gilles Peskinee108d982022-04-26 16:50:40 +02001855 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001856#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001857 encrypt_then_mac = transform->encrypt_then_mac;
1858#endif
1859 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001860}
1861
Neil Armstrongab555e02022-04-04 11:07:59 +02001862mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001863#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001864 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001865#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001866 const mbedtls_ssl_ciphersuite_t *suite )
1867{
Gilles Peskinee108d982022-04-26 16:50:40 +02001868 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1869
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001870#if defined(MBEDTLS_USE_PSA_CRYPTO)
1871 psa_status_t status;
1872 psa_algorithm_t alg;
1873 psa_key_type_t type;
1874 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001875 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1876 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001877 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001878#else
1879 const mbedtls_cipher_info_t *cipher =
1880 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001881 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001882 {
1883 base_mode =
1884 mbedtls_ssl_get_base_mode(
1885 mbedtls_cipher_info_get_mode( cipher ) );
1886 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001887#endif /* MBEDTLS_USE_PSA_CRYPTO */
1888
Gilles Peskinee108d982022-04-26 16:50:40 +02001889#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1890 int encrypt_then_mac = 0;
1891#endif
1892 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001893}
1894
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001895#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001896
1897#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001898/* Serialization of TLS 1.3 sessions:
1899 *
1900 * struct {
1901 * uint64 ticket_received;
1902 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001903 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001904 * } ClientOnlyData;
1905 *
1906 * struct {
1907 * uint8 endpoint;
1908 * uint8 ciphersuite[2];
1909 * uint32 ticket_age_add;
1910 * uint8 ticket_flags;
1911 * opaque resumption_key<0..255>;
1912 * select ( endpoint ) {
1913 * case client: ClientOnlyData;
1914 * case server: uint64 start_time;
1915 * };
1916 * } serialized_session_tls13;
1917 *
1918 */
1919#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001920MBEDTLS_CHECK_RETURN_CRITICAL
1921static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1922 unsigned char *buf,
1923 size_t buf_len,
1924 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001925{
1926 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001927 size_t needed = 1 /* endpoint */
1928 + 2 /* ciphersuite */
1929 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001930 + 1 /* ticket_flags */
1931 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001932 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001933
1934 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1936 needed += session->resumption_key_len; /* resumption_key */
1937
Jerry Yu438ddd82022-07-07 06:55:50 +00001938#if defined(MBEDTLS_HAVE_TIME)
1939 needed += 8; /* start_time or ticket_received */
1940#endif
1941
1942#if defined(MBEDTLS_SSL_CLI_C)
1943 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1944 {
1945 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001946 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001947
1948 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001949 if( session->ticket_len > SIZE_MAX - needed )
1950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001951
Jerry Yue28d9742022-08-18 15:44:03 +08001952 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001953 }
1954#endif /* MBEDTLS_SSL_CLI_C */
1955
Jerry Yue36fdd62022-08-17 21:31:36 +08001956 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001957 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001958 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001959
1960 p[0] = session->endpoint;
1961 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1962 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1963 p[7] = session->ticket_flags;
1964
1965 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001966 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001967 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001968 memcpy( p, session->resumption_key, session->resumption_key_len );
1969 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001970
1971#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1972 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1973 {
1974 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1975 p += 8;
1976 }
1977#endif /* MBEDTLS_HAVE_TIME */
1978
1979#if defined(MBEDTLS_SSL_CLI_C)
1980 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1981 {
1982#if defined(MBEDTLS_HAVE_TIME)
1983 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
1984 p += 8;
1985#endif
1986 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
1987 p += 4;
1988
1989 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
1990 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08001991
1992 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00001993 {
1994 memcpy( p, session->ticket, session->ticket_len );
1995 p += session->ticket_len;
1996 }
1997 }
1998#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08001999 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002000}
2001
2002MBEDTLS_CHECK_RETURN_CRITICAL
2003static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2004 const unsigned char *buf,
2005 size_t len )
2006{
2007 const unsigned char *p = buf;
2008 const unsigned char *end = buf + len;
2009
2010 if( end - p < 9 )
2011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2012 session->endpoint = p[0];
2013 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2014 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2015 session->ticket_flags = p[7];
2016
2017 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002018 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002019 p += 9;
2020
Jerry Yubc7c1a42022-07-21 22:57:37 +08002021 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2023
Jerry Yubc7c1a42022-07-21 22:57:37 +08002024 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002026 memcpy( session->resumption_key, p, session->resumption_key_len );
2027 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002028
2029#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2030 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2031 {
2032 if( end - p < 8 )
2033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2034 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2035 p += 8;
2036 }
2037#endif /* MBEDTLS_HAVE_TIME */
2038
2039#if defined(MBEDTLS_SSL_CLI_C)
2040 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2041 {
2042#if defined(MBEDTLS_HAVE_TIME)
2043 if( end - p < 8 )
2044 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2045 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2046 p += 8;
2047#endif
2048 if( end - p < 4 )
2049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2050 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2051 p += 4;
2052
2053 if( end - p < 2 )
2054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2055 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2056 p += 2;
2057
2058 if( end - p < ( long int )session->ticket_len )
2059 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2060 if( session->ticket_len > 0 )
2061 {
2062 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2063 if( session->ticket == NULL )
2064 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2065 memcpy( session->ticket, p, session->ticket_len );
2066 p += session->ticket_len;
2067 }
2068 }
2069#endif /* MBEDTLS_SSL_CLI_C */
2070
2071 return( 0 );
2072
2073}
2074#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002075MBEDTLS_CHECK_RETURN_CRITICAL
2076static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2077 unsigned char *buf,
2078 size_t buf_len,
2079 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002080{
2081 ((void) session);
2082 ((void) buf);
2083 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002084 *olen = 0;
2085 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002086}
Jerry Yu438ddd82022-07-07 06:55:50 +00002087
2088static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2089 unsigned char *buf,
2090 size_t buf_len )
2091{
2092 ((void) session);
2093 ((void) buf);
2094 ((void) buf_len);
2095 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2096}
2097#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002098#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2099
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002100psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002101 size_t taglen,
2102 psa_algorithm_t *alg,
2103 psa_key_type_t *key_type,
2104 size_t *key_size )
2105{
2106 switch ( mbedtls_cipher_type )
2107 {
2108 case MBEDTLS_CIPHER_AES_128_CBC:
2109 *alg = PSA_ALG_CBC_NO_PADDING;
2110 *key_type = PSA_KEY_TYPE_AES;
2111 *key_size = 128;
2112 break;
2113 case MBEDTLS_CIPHER_AES_128_CCM:
2114 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2115 *key_type = PSA_KEY_TYPE_AES;
2116 *key_size = 128;
2117 break;
2118 case MBEDTLS_CIPHER_AES_128_GCM:
2119 *alg = PSA_ALG_GCM;
2120 *key_type = PSA_KEY_TYPE_AES;
2121 *key_size = 128;
2122 break;
2123 case MBEDTLS_CIPHER_AES_192_CCM:
2124 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2125 *key_type = PSA_KEY_TYPE_AES;
2126 *key_size = 192;
2127 break;
2128 case MBEDTLS_CIPHER_AES_192_GCM:
2129 *alg = PSA_ALG_GCM;
2130 *key_type = PSA_KEY_TYPE_AES;
2131 *key_size = 192;
2132 break;
2133 case MBEDTLS_CIPHER_AES_256_CBC:
2134 *alg = PSA_ALG_CBC_NO_PADDING;
2135 *key_type = PSA_KEY_TYPE_AES;
2136 *key_size = 256;
2137 break;
2138 case MBEDTLS_CIPHER_AES_256_CCM:
2139 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2140 *key_type = PSA_KEY_TYPE_AES;
2141 *key_size = 256;
2142 break;
2143 case MBEDTLS_CIPHER_AES_256_GCM:
2144 *alg = PSA_ALG_GCM;
2145 *key_type = PSA_KEY_TYPE_AES;
2146 *key_size = 256;
2147 break;
2148 case MBEDTLS_CIPHER_ARIA_128_CBC:
2149 *alg = PSA_ALG_CBC_NO_PADDING;
2150 *key_type = PSA_KEY_TYPE_ARIA;
2151 *key_size = 128;
2152 break;
2153 case MBEDTLS_CIPHER_ARIA_128_CCM:
2154 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2155 *key_type = PSA_KEY_TYPE_ARIA;
2156 *key_size = 128;
2157 break;
2158 case MBEDTLS_CIPHER_ARIA_128_GCM:
2159 *alg = PSA_ALG_GCM;
2160 *key_type = PSA_KEY_TYPE_ARIA;
2161 *key_size = 128;
2162 break;
2163 case MBEDTLS_CIPHER_ARIA_192_CCM:
2164 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2165 *key_type = PSA_KEY_TYPE_ARIA;
2166 *key_size = 192;
2167 break;
2168 case MBEDTLS_CIPHER_ARIA_192_GCM:
2169 *alg = PSA_ALG_GCM;
2170 *key_type = PSA_KEY_TYPE_ARIA;
2171 *key_size = 192;
2172 break;
2173 case MBEDTLS_CIPHER_ARIA_256_CBC:
2174 *alg = PSA_ALG_CBC_NO_PADDING;
2175 *key_type = PSA_KEY_TYPE_ARIA;
2176 *key_size = 256;
2177 break;
2178 case MBEDTLS_CIPHER_ARIA_256_CCM:
2179 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2180 *key_type = PSA_KEY_TYPE_ARIA;
2181 *key_size = 256;
2182 break;
2183 case MBEDTLS_CIPHER_ARIA_256_GCM:
2184 *alg = PSA_ALG_GCM;
2185 *key_type = PSA_KEY_TYPE_ARIA;
2186 *key_size = 256;
2187 break;
2188 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2189 *alg = PSA_ALG_CBC_NO_PADDING;
2190 *key_type = PSA_KEY_TYPE_CAMELLIA;
2191 *key_size = 128;
2192 break;
2193 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2194 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2195 *key_type = PSA_KEY_TYPE_CAMELLIA;
2196 *key_size = 128;
2197 break;
2198 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2199 *alg = PSA_ALG_GCM;
2200 *key_type = PSA_KEY_TYPE_CAMELLIA;
2201 *key_size = 128;
2202 break;
2203 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2204 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2205 *key_type = PSA_KEY_TYPE_CAMELLIA;
2206 *key_size = 192;
2207 break;
2208 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2209 *alg = PSA_ALG_GCM;
2210 *key_type = PSA_KEY_TYPE_CAMELLIA;
2211 *key_size = 192;
2212 break;
2213 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2214 *alg = PSA_ALG_CBC_NO_PADDING;
2215 *key_type = PSA_KEY_TYPE_CAMELLIA;
2216 *key_size = 256;
2217 break;
2218 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2219 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2220 *key_type = PSA_KEY_TYPE_CAMELLIA;
2221 *key_size = 256;
2222 break;
2223 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2224 *alg = PSA_ALG_GCM;
2225 *key_type = PSA_KEY_TYPE_CAMELLIA;
2226 *key_size = 256;
2227 break;
2228 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2229 *alg = PSA_ALG_CHACHA20_POLY1305;
2230 *key_type = PSA_KEY_TYPE_CHACHA20;
2231 *key_size = 256;
2232 break;
2233 case MBEDTLS_CIPHER_NULL:
2234 *alg = MBEDTLS_SSL_NULL_CIPHER;
2235 *key_type = 0;
2236 *key_size = 0;
2237 break;
2238 default:
2239 return PSA_ERROR_NOT_SUPPORTED;
2240 }
2241
2242 return PSA_SUCCESS;
2243}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002244#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002245
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002246#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002247int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2248 const unsigned char *dhm_P, size_t P_len,
2249 const unsigned char *dhm_G, size_t G_len )
2250{
Janos Follath865b3eb2019-12-16 11:46:15 +00002251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002252
Glenn Strausscee11292021-12-20 01:43:17 -05002253 mbedtls_mpi_free( &conf->dhm_P );
2254 mbedtls_mpi_free( &conf->dhm_G );
2255
Hanno Beckera90658f2017-10-04 15:29:08 +01002256 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2257 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2258 {
2259 mbedtls_mpi_free( &conf->dhm_P );
2260 mbedtls_mpi_free( &conf->dhm_G );
2261 return( ret );
2262 }
2263
2264 return( 0 );
2265}
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002267int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002268{
Janos Follath865b3eb2019-12-16 11:46:15 +00002269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002270
Glenn Strausscee11292021-12-20 01:43:17 -05002271 mbedtls_mpi_free( &conf->dhm_P );
2272 mbedtls_mpi_free( &conf->dhm_G );
2273
Gilles Peskinee5702482021-06-11 21:59:08 +02002274 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2275 &conf->dhm_P ) ) != 0 ||
2276 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2277 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002278 {
2279 mbedtls_mpi_free( &conf->dhm_P );
2280 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002281 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002282 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002283
2284 return( 0 );
2285}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002286#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002287
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002288#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2289/*
2290 * Set the minimum length for Diffie-Hellman parameters
2291 */
2292void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2293 unsigned int bitlen )
2294{
2295 conf->dhm_min_bitlen = bitlen;
2296}
2297#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2298
Gilles Peskineeccd8882020-03-10 12:19:08 +01002299#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002300#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002301/*
2302 * Set allowed/preferred hashes for handshake signatures
2303 */
2304void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2305 const int *hashes )
2306{
2307 conf->sig_hashes = hashes;
2308}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002309#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002310
Jerry Yuf017ee42022-01-12 15:49:48 +08002311/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002312void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2313 const uint16_t* sig_algs )
2314{
Jerry Yuf017ee42022-01-12 15:49:48 +08002315#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2316 conf->sig_hashes = NULL;
2317#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2318 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002319}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002320#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002321
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002322#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002323#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002324/*
2325 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002326 *
2327 * mbedtls_ssl_setup() takes the provided list
2328 * and translates it to a list of IANA TLS group identifiers,
2329 * stored in ssl->handshake->group_list.
2330 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002331 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002332void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002333 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002335 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002336 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002337}
Brett Warrene0edc842021-08-17 09:53:13 +01002338#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002339#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002340
Brett Warrene0edc842021-08-17 09:53:13 +01002341/*
2342 * Set the allowed groups
2343 */
2344void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2345 const uint16_t *group_list )
2346{
2347#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2348 conf->curve_list = NULL;
2349#endif
2350 conf->group_list = group_list;
2351}
2352
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002353#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002355{
Hanno Becker947194e2017-04-07 13:25:49 +01002356 /* Initialize to suppress unnecessary compiler warning */
2357 size_t hostname_len = 0;
2358
2359 /* Check if new hostname is valid before
2360 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002361 if( hostname != NULL )
2362 {
2363 hostname_len = strlen( hostname );
2364
2365 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2367 }
2368
2369 /* Now it's clear that we will overwrite the old hostname,
2370 * so we can free it safely */
2371
2372 if( ssl->hostname != NULL )
2373 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002374 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002375 mbedtls_free( ssl->hostname );
2376 }
2377
2378 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002381 {
2382 ssl->hostname = NULL;
2383 }
2384 else
2385 {
2386 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002387 if( ssl->hostname == NULL )
2388 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002389
Hanno Becker947194e2017-04-07 13:25:49 +01002390 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002391
Hanno Becker947194e2017-04-07 13:25:49 +01002392 ssl->hostname[hostname_len] = '\0';
2393 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395 return( 0 );
2396}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002397#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002399#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002400void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002402 const unsigned char *, size_t),
2403 void *p_sni )
2404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002405 conf->f_sni = f_sni;
2406 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002411int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002412{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002413 size_t cur_len, tot_len;
2414 const char **p;
2415
2416 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002417 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2418 * MUST NOT be truncated."
2419 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002420 */
2421 tot_len = 0;
2422 for( p = protos; *p != NULL; p++ )
2423 {
2424 cur_len = strlen( *p );
2425 tot_len += cur_len;
2426
Ronald Cron8216dd32020-04-23 16:41:44 +02002427 if( ( cur_len == 0 ) ||
2428 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2429 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002431 }
2432
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002433 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002434
2435 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002436}
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002439{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002440 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002443
Johan Pascalb62bb512015-12-03 21:56:45 +01002444#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002445void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2446 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002447{
2448 conf->dtls_srtp_mki_support = support_mki_value;
2449}
2450
Ron Eldoref72faf2018-07-12 11:54:20 +03002451int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2452 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002453 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002454{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002455 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002456 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002458 }
Ron Eldor591f1622018-01-22 12:30:04 +02002459
2460 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002461 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002462 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002463 }
Ron Eldor591f1622018-01-22 12:30:04 +02002464
2465 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2466 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002467 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002468}
2469
Ron Eldoref72faf2018-07-12 11:54:20 +03002470int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002471 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002472{
Johan Pascal253d0262020-09-22 13:04:45 +02002473 const mbedtls_ssl_srtp_profile *p;
2474 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002475
Johan Pascal253d0262020-09-22 13:04:45 +02002476 /* check the profiles list: all entry must be valid,
2477 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002478 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2479 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2480 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002481 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002482 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002483 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002484 list_size++;
2485 }
2486 else
2487 {
2488 /* unsupported value, stop parsing and set the size to an error value */
2489 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002490 }
2491 }
2492
Johan Pascal5ef72d22020-10-28 17:05:47 +01002493 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002494 {
Johan Pascal253d0262020-09-22 13:04:45 +02002495 conf->dtls_srtp_profile_list = NULL;
2496 conf->dtls_srtp_profile_list_len = 0;
2497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2498 }
2499
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002500 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002501 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002502
2503 return( 0 );
2504}
2505
Johan Pascal5ef72d22020-10-28 17:05:47 +01002506void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2507 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002508{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002509 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2510 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002511 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002512 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002513 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002514 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002515 else
2516 {
2517 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002518 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2519 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002520 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002521}
Johan Pascalb62bb512015-12-03 21:56:45 +01002522#endif /* MBEDTLS_SSL_DTLS_SRTP */
2523
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302524#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002525void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002526{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002527 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002528}
2529
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002530void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002531{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002532 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002533}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302534#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002535
Janos Follath088ce432017-04-10 12:42:31 +01002536#if defined(MBEDTLS_SSL_SRV_C)
2537void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2538 char cert_req_ca_list )
2539{
2540 conf->cert_req_ca_list = cert_req_ca_list;
2541}
2542#endif
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002545void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002546{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002547 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002548}
2549#endif
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002552void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002554 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002555}
2556#endif
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002559int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002562 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002565 }
2566
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002567 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002568
2569 return( 0 );
2570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002572
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002573void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002575 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002576}
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002579void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002581 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002582}
2583
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002584void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002586 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002587}
2588
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002589void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002590 const unsigned char period[8] )
2591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002592 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002597#if defined(MBEDTLS_SSL_CLI_C)
2598void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002599{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002600 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002601}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002602#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002603
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002604#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002605void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2606 mbedtls_ssl_ticket_write_t *f_ticket_write,
2607 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2608 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002609{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002610 conf->f_ticket_write = f_ticket_write;
2611 conf->f_ticket_parse = f_ticket_parse;
2612 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002613}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002616
Hanno Becker7e6c1782021-06-08 09:24:55 +01002617void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2618 mbedtls_ssl_export_keys_t *f_export_keys,
2619 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002620{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002621 ssl->f_export_keys = f_export_keys;
2622 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002623}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002624
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002625#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002626void mbedtls_ssl_conf_async_private_cb(
2627 mbedtls_ssl_config *conf,
2628 mbedtls_ssl_async_sign_t *f_async_sign,
2629 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2630 mbedtls_ssl_async_resume_t *f_async_resume,
2631 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002632 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002633{
2634 conf->f_async_sign_start = f_async_sign;
2635 conf->f_async_decrypt_start = f_async_decrypt;
2636 conf->f_async_resume = f_async_resume;
2637 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002638 conf->p_async_config_data = async_config_data;
2639}
2640
Gilles Peskine8f97af72018-04-26 11:46:10 +02002641void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2642{
2643 return( conf->p_async_config_data );
2644}
2645
Gilles Peskine1febfef2018-04-30 11:54:39 +02002646void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002647{
2648 if( ssl->handshake == NULL )
2649 return( NULL );
2650 else
2651 return( ssl->handshake->user_async_ctx );
2652}
2653
Gilles Peskine1febfef2018-04-30 11:54:39 +02002654void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002655 void *ctx )
2656{
2657 if( ssl->handshake != NULL )
2658 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002659}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002660#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002661
Paul Bakker5121ce52009-01-03 21:22:43 +00002662/*
2663 * SSL get accessors
2664 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002665uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002666{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002667 if( ssl->session != NULL )
2668 return( ssl->session->verify_result );
2669
2670 if( ssl->session_negotiate != NULL )
2671 return( ssl->session_negotiate->verify_result );
2672
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002673 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674}
2675
Glenn Strauss8f526902022-01-13 00:04:49 -05002676int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2677{
2678 if( ssl == NULL || ssl->session == NULL )
2679 return( 0 );
2680
2681 return( ssl->session->ciphersuite );
2682}
2683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002685{
Paul Bakker926c8e42013-03-06 10:23:34 +01002686 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002687 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002690}
2691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002696 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002697 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002698 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002699 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002700 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002701 default:
2702 return( "unknown (DTLS)" );
2703 }
2704 }
2705#endif
2706
Glenn Strauss60bfe602022-03-14 19:04:24 -04002707 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002708 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002709 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002710 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002711 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002712 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002713 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002714 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002715 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002716}
2717
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002718#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002719size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2720{
David Horstmann95d516f2021-05-04 18:36:56 +01002721 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002722 size_t read_mfl;
2723
2724 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2726 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2727 {
2728 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2729 }
2730
2731 /* Check if a smaller max length was negotiated */
2732 if( ssl->session_out != NULL )
2733 {
2734 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2735 if( read_mfl < max_len )
2736 {
2737 max_len = read_mfl;
2738 }
2739 }
2740
2741 // During a handshake, use the value being negotiated
2742 if( ssl->session_negotiate != NULL )
2743 {
2744 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2745 if( read_mfl < max_len )
2746 {
2747 max_len = read_mfl;
2748 }
2749 }
2750
2751 return( max_len );
2752}
2753
2754size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002755{
2756 size_t max_len;
2757
2758 /*
2759 * Assume mfl_code is correct since it was checked when set
2760 */
Angus Grattond8213d02016-05-25 20:56:48 +10002761 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002762
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002763 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002764 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002765 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002766 {
Angus Grattond8213d02016-05-25 20:56:48 +10002767 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002768 }
2769
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002770 /* During a handshake, use the value being negotiated */
2771 if( ssl->session_negotiate != NULL &&
2772 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2773 {
2774 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2775 }
2776
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002777 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002778}
2779#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2780
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002782size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002783{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002784 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2786 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2787 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2788 return ( 0 );
2789
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002790 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2791 return( ssl->mtu );
2792
2793 if( ssl->mtu == 0 )
2794 return( ssl->handshake->mtu );
2795
2796 return( ssl->mtu < ssl->handshake->mtu ?
2797 ssl->mtu : ssl->handshake->mtu );
2798}
2799#endif /* MBEDTLS_SSL_PROTO_DTLS */
2800
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002801int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2802{
2803 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2804
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002805#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2806 !defined(MBEDTLS_SSL_PROTO_DTLS)
2807 (void) ssl;
2808#endif
2809
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002810#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002811 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002812
2813 if( max_len > mfl )
2814 max_len = mfl;
2815#endif
2816
2817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002818 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002819 {
Hanno Becker89490712020-02-05 10:50:12 +00002820 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002821 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2822 const size_t overhead = (size_t) ret;
2823
2824 if( ret < 0 )
2825 return( ret );
2826
2827 if( mtu <= overhead )
2828 {
2829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2830 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2831 }
2832
2833 if( max_len > mtu - overhead )
2834 max_len = mtu - overhead;
2835 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002836#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002837
Hanno Becker0defedb2018-08-10 12:35:02 +01002838#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2839 !defined(MBEDTLS_SSL_PROTO_DTLS)
2840 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002841#endif
2842
2843 return( (int) max_len );
2844}
2845
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002846int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2847{
2848 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2849
2850#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2851 (void) ssl;
2852#endif
2853
2854#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2855 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2856
2857 if( max_len > mfl )
2858 max_len = mfl;
2859#endif
2860
2861 return( (int) max_len );
2862}
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_X509_CRT_PARSE_C)
2865const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002866{
2867 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002868 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002869
Hanno Beckere6824572019-02-07 13:18:46 +00002870#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002871 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002872#else
2873 return( NULL );
2874#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002879int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2880 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002881{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002882 int ret;
2883
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002884 if( ssl == NULL ||
2885 dst == NULL ||
2886 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002887 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002890 }
2891
Hanno Beckere810bbc2021-05-14 16:01:05 +01002892 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2893 * idempotent: Each session can only be exported once.
2894 *
2895 * (This is in preparation for TLS 1.3 support where we will
2896 * need the ability to export multiple sessions (aka tickets),
2897 * which will be achieved by calling mbedtls_ssl_get_session()
2898 * multiple times until it fails.)
2899 *
2900 * Check whether we have already exported the current session,
2901 * and fail if so.
2902 */
2903 if( ssl->session->exported == 1 )
2904 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2905
2906 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2907 if( ret != 0 )
2908 return( ret );
2909
2910 /* Remember that we've exported the session. */
2911 ssl->session->exported = 1;
2912 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916/*
Hanno Beckera835da52019-05-16 12:39:07 +01002917 * Define ticket header determining Mbed TLS version
2918 * and structure of the ticket.
2919 */
2920
Hanno Becker94ef3b32019-05-16 12:50:45 +01002921/*
Hanno Becker50b59662019-05-28 14:30:45 +01002922 * Define bitflag determining compile-time settings influencing
2923 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002924 */
2925
Hanno Becker50b59662019-05-28 14:30:45 +01002926#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002927#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002928#else
Hanno Becker3e088662019-05-29 11:10:18 +01002929#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002930#endif /* MBEDTLS_HAVE_TIME */
2931
2932#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002933#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002934#else
Hanno Becker3e088662019-05-29 11:10:18 +01002935#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002936#endif /* MBEDTLS_X509_CRT_PARSE_C */
2937
2938#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002939#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002940#else
Hanno Becker3e088662019-05-29 11:10:18 +01002941#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002942#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2943
2944#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002945#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002946#else
Hanno Becker3e088662019-05-29 11:10:18 +01002947#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002948#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2949
Hanno Becker94ef3b32019-05-16 12:50:45 +01002950#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002951#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002952#else
Hanno Becker3e088662019-05-29 11:10:18 +01002953#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002954#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2955
Hanno Becker94ef3b32019-05-16 12:50:45 +01002956#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2957#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2958#else
2959#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2960#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2961
Hanno Becker3e088662019-05-29 11:10:18 +01002962#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2963#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2964#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2965#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002966#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2967#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002968
Hanno Becker50b59662019-05-28 14:30:45 +01002969#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002970 ( (uint16_t) ( \
2971 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
2972 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
2973 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
2974 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01002975 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01002976 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01002977
Hanno Beckerf8787072019-05-16 12:41:07 +01002978static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979 MBEDTLS_VERSION_MAJOR,
2980 MBEDTLS_VERSION_MINOR,
2981 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01002982 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
2983 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01002984};
Hanno Beckera835da52019-05-16 12:39:07 +01002985
2986/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002987 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02002988 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002989 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002990 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002991 *
Hanno Beckerdce50972021-08-01 05:39:23 +01002992 * opaque mbedtls_version[3]; // library version: major, minor, patch
2993 * opaque session_format[2]; // library-version specific 16-bit field
2994 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002995 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01002996 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01002997 * Note: When updating the format, remember to keep
2998 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02002999 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003000 * // In this version, `session_format` determines
3001 * // the setting of those compile-time
3002 * // configuration options which influence
3003 * // the structure of mbedtls_ssl_session.
3004 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003005 * uint8_t minor_ver; // Protocol minor version. Possible values:
3006 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003007 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003008 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003009 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003010 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003011 * serialized_session_tls12 data;
3012 *
3013 * };
3014 *
3015 * } serialized_session;
3016 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003017 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003018
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003019MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003020static int ssl_session_save( const mbedtls_ssl_session *session,
3021 unsigned char omit_header,
3022 unsigned char *buf,
3023 size_t buf_len,
3024 size_t *olen )
3025{
3026 unsigned char *p = buf;
3027 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003028 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003029#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3030 size_t out_len;
3031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3032#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003033 if( session == NULL )
3034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3035
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003036 if( !omit_header )
3037 {
3038 /*
3039 * Add Mbed TLS version identifier
3040 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003041 used += sizeof( ssl_serialized_session_header );
3042
3043 if( used <= buf_len )
3044 {
3045 memcpy( p, ssl_serialized_session_header,
3046 sizeof( ssl_serialized_session_header ) );
3047 p += sizeof( ssl_serialized_session_header );
3048 }
3049 }
3050
3051 /*
3052 * TLS version identifier
3053 */
3054 used += 1;
3055 if( used <= buf_len )
3056 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003057 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003058 }
3059
3060 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003061 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003062 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003063 {
3064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003065 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003066 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003067 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003068#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3069
Jerry Yu251a12e2022-07-13 15:15:48 +08003070#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3071 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003072 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3073 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003074 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003075 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003076 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003077#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3078
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003079 default:
3080 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3081 }
3082
3083 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003084 if( used > buf_len )
3085 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003086
3087 return( 0 );
3088}
3089
3090/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003091 * Public wrapper for ssl_session_save()
3092 */
3093int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3094 unsigned char *buf,
3095 size_t buf_len,
3096 size_t *olen )
3097{
3098 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3099}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003100
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003101/*
3102 * Deserialize session, see mbedtls_ssl_session_save() for format.
3103 *
3104 * This internal version is wrapped by a public function that cleans up in
3105 * case of error, and has an extra option omit_header.
3106 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003107MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003108static int ssl_session_load( mbedtls_ssl_session *session,
3109 unsigned char omit_header,
3110 const unsigned char *buf,
3111 size_t len )
3112{
3113 const unsigned char *p = buf;
3114 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003115 size_t remaining_len;
3116
3117
3118 if( session == NULL )
3119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003120
3121 if( !omit_header )
3122 {
3123 /*
3124 * Check Mbed TLS version identifier
3125 */
3126
3127 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3129
3130 if( memcmp( p, ssl_serialized_session_header,
3131 sizeof( ssl_serialized_session_header ) ) != 0 )
3132 {
3133 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3134 }
3135 p += sizeof( ssl_serialized_session_header );
3136 }
3137
3138 /*
3139 * TLS version identifier
3140 */
3141 if( 1 > (size_t)( end - p ) )
3142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003143 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003144
3145 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003146 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003147 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003148 {
3149#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003150 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003151 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003152#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3153
Jerry Yu438ddd82022-07-07 06:55:50 +00003154#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3155 case MBEDTLS_SSL_VERSION_TLS1_3:
3156 return( ssl_tls13_session_load( session, p, remaining_len ) );
3157#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3158
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003159 default:
3160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3161 }
3162}
3163
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003164/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003165 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003166 */
3167int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3168 const unsigned char *buf,
3169 size_t len )
3170{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003171 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003172
3173 if( ret != 0 )
3174 mbedtls_ssl_session_free( session );
3175
3176 return( ret );
3177}
3178
3179/*
Paul Bakker1961b702013-01-25 14:49:24 +01003180 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003182MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003183static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3184{
3185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3186
Ronald Cron66dbf912022-02-02 15:33:46 +01003187 /*
3188 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003189 * that were written into the output buffer by the previous handshake step,
3190 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003191 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3192 * We proceed to the next handshake step only when all data from the
3193 * previous one have been sent to the peer, thus we make sure that this is
3194 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3195 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3196 * we have to wait before to go ahead.
3197 * In the case of TLS 1.3, handshake step handlers do not send data to the
3198 * peer. Data are only sent here and through
3199 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003200 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003201 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003202 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3203 return( ret );
3204
3205#if defined(MBEDTLS_SSL_PROTO_DTLS)
3206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3207 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3208 {
3209 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3210 return( ret );
3211 }
3212#endif /* MBEDTLS_SSL_PROTO_DTLS */
3213
3214 return( ret );
3215}
3216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218{
Hanno Becker41934dd2021-08-07 19:13:43 +01003219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
Hanno Becker41934dd2021-08-07 19:13:43 +01003221 if( ssl == NULL ||
3222 ssl->conf == NULL ||
3223 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003224 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003225 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003227 }
3228
3229 ret = ssl_prepare_handshake_step( ssl );
3230 if( ret != 0 )
3231 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003232
Jerry Yue7047812021-09-13 19:26:39 +08003233 ret = mbedtls_ssl_handle_pending_alert( ssl );
3234 if( ret != 0 )
3235 goto cleanup;
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003238 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003239 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3241 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003242
Ronald Cron9f0fba32022-02-10 16:45:15 +01003243 switch( ssl->state )
3244 {
3245 case MBEDTLS_SSL_HELLO_REQUEST:
3246 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3247 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003248
Ronald Cron9f0fba32022-02-10 16:45:15 +01003249 case MBEDTLS_SSL_CLIENT_HELLO:
3250 ret = mbedtls_ssl_write_client_hello( ssl );
3251 break;
3252
3253 default:
3254#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003255 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003256 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3257 else
3258 ret = mbedtls_ssl_handshake_client_step( ssl );
3259#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3260 ret = mbedtls_ssl_handshake_client_step( ssl );
3261#else
3262 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3263#endif
3264 }
Jerry Yub9930e72021-08-06 17:11:51 +08003265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003268 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003269 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003271 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003272 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003273#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003274
3275#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3276 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3277 ret = mbedtls_ssl_handshake_server_step( ssl );
3278#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003280#endif
3281
Jerry Yue7047812021-09-13 19:26:39 +08003282 if( ret != 0 )
3283 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003284 /* handshake_step return error. And it is same
3285 * with alert_reason.
3286 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003287 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003288 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003289 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003290 goto cleanup;
3291 }
3292 }
3293
3294cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003295 return( ret );
3296}
3297
3298/*
3299 * Perform the SSL handshake
3300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003302{
3303 int ret = 0;
3304
Hanno Beckera817ea42020-10-20 15:20:23 +01003305 /* Sanity checks */
3306
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003307 if( ssl == NULL || ssl->conf == NULL )
3308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3309
Hanno Beckera817ea42020-10-20 15:20:23 +01003310#if defined(MBEDTLS_SSL_PROTO_DTLS)
3311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3312 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3313 {
3314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3315 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3317 }
3318#endif /* MBEDTLS_SSL_PROTO_DTLS */
3319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003321
Hanno Beckera817ea42020-10-20 15:20:23 +01003322 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003323 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003326
3327 if( ret != 0 )
3328 break;
3329 }
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003332
3333 return( ret );
3334}
3335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#if defined(MBEDTLS_SSL_RENEGOTIATION)
3337#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003338/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003339 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003340 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003341MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003343{
Janos Follath865b3eb2019-12-16 11:46:15 +00003344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003347
3348 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3350 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003351
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003352 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003353 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003355 return( ret );
3356 }
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003359
3360 return( 0 );
3361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003363
3364/*
3365 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 * - any side: calling mbedtls_ssl_renegotiate(),
3367 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3368 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003369 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003370 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003372 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003373int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003374{
Janos Follath865b3eb2019-12-16 11:46:15 +00003375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003378
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003379 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3380 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003381
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003382 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3383 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003387 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003388 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003389 ssl->handshake->out_msg_seq = 1;
3390 else
3391 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003392 }
3393#endif
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3396 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003401 return( ret );
3402 }
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003405
3406 return( 0 );
3407}
3408
3409/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003410 * Renegotiate current connection on client,
3411 * or request renegotiation on server
3412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003416
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003417 if( ssl == NULL || ssl->conf == NULL )
3418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003421 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003423 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003424 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003428
3429 /* Did we already try/start sending HelloRequest? */
3430 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003432
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003433 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003438 /*
3439 * On client, either start the renegotiation process or,
3440 * if already in progress, continue the handshake
3441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003443 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003444 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003446
Hanno Becker40cdaa12020-02-05 10:48:27 +00003447 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003448 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003450 return( ret );
3451 }
3452 }
3453 else
3454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003458 return( ret );
3459 }
3460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003462
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003463 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003466
Gilles Peskine9b562d52018-04-25 20:32:43 +02003467void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003468{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003469 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3470
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003471 if( handshake == NULL )
3472 return;
3473
Brett Warrene0edc842021-08-17 09:53:13 +01003474#if defined(MBEDTLS_ECP_C)
3475#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3476 if ( ssl->handshake->group_list_heap_allocated )
3477 mbedtls_free( (void*) handshake->group_list );
3478 handshake->group_list = NULL;
3479#endif /* MBEDTLS_DEPRECATED_REMOVED */
3480#endif /* MBEDTLS_ECP_C */
3481
Jerry Yuf017ee42022-01-12 15:49:48 +08003482#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3483#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3484 if ( ssl->handshake->sig_algs_heap_allocated )
3485 mbedtls_free( (void*) handshake->sig_algs );
3486 handshake->sig_algs = NULL;
3487#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003488#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3489 if( ssl->handshake->certificate_request_context )
3490 {
3491 mbedtls_free( (void*) handshake->certificate_request_context );
3492 }
3493#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003494#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3495
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003496#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3497 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3498 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003499 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003500 handshake->async_in_progress = 0;
3501 }
3502#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3503
Andrzej Kurek25f27152022-08-17 16:09:31 -04003504#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003505#if defined(MBEDTLS_USE_PSA_CRYPTO)
3506 psa_hash_abort( &handshake->fin_sha256_psa );
3507#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003508 mbedtls_sha256_free( &handshake->fin_sha256 );
3509#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003510#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003511#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003512#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003513 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003514#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003515 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003516#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003517#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#if defined(MBEDTLS_DHM_C)
3520 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003521#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003522#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003524#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003525#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003526 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003527#if defined(MBEDTLS_SSL_CLI_C)
3528 mbedtls_free( handshake->ecjpake_cache );
3529 handshake->ecjpake_cache = NULL;
3530 handshake->ecjpake_cache_len = 0;
3531#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003532#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003533
Janos Follath4ae5c292016-02-10 11:27:43 +00003534#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3535 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003536 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003538#endif
3539
Gilles Peskineeccd8882020-03-10 12:19:08 +01003540#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003541#if defined(MBEDTLS_USE_PSA_CRYPTO)
3542 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3543 {
3544 /* The maintenance of the external PSK key slot is the
3545 * user's responsibility. */
3546 if( ssl->handshake->psk_opaque_is_internal )
3547 {
3548 psa_destroy_key( ssl->handshake->psk_opaque );
3549 ssl->handshake->psk_opaque_is_internal = 0;
3550 }
3551 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3552 }
Neil Armstronge952a302022-05-03 10:22:14 +02003553#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003554 if( handshake->psk != NULL )
3555 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003556 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003557 mbedtls_free( handshake->psk );
3558 }
Neil Armstronge952a302022-05-03 10:22:14 +02003559#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003560#endif
3561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3563 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003564 /*
3565 * Free only the linked list wrapper, not the keys themselves
3566 * since the belong to the SNI callback
3567 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003568 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003570
Gilles Peskineeccd8882020-03-10 12:19:08 +01003571#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003572 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003573 if( handshake->ecrs_peer_cert != NULL )
3574 {
3575 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3576 mbedtls_free( handshake->ecrs_peer_cert );
3577 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003578#endif
3579
Hanno Becker75173122019-02-06 16:18:31 +00003580#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3581 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3582 mbedtls_pk_free( &handshake->peer_pubkey );
3583#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3584
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003585#if defined(MBEDTLS_SSL_CLI_C) && \
3586 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3587 mbedtls_free( handshake->cookie );
3588#endif /* MBEDTLS_SSL_CLI_C &&
3589 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003590
3591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003592 mbedtls_ssl_flight_free( handshake->flight );
3593 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003595
Ronald Cronf12b81d2022-03-15 10:42:41 +01003596#if defined(MBEDTLS_ECDH_C) && \
3597 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003598 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003599 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003600#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3601
Ronald Cron6f135e12021-12-08 16:57:54 +01003602#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003603 mbedtls_ssl_transform_free( handshake->transform_handshake );
3604 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003605 mbedtls_free( handshake->transform_earlydata );
3606 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003607#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003608
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003609
3610#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3611 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3612 * processes datagrams and the fact that a datagram is allowed to have
3613 * several records in it, it is possible that the I/O buffers are not
3614 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003615 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3616 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003617#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003618
Jerry Yuba9c7272021-10-30 11:54:10 +08003619 /* mbedtls_platform_zeroize MUST be last one in this function */
3620 mbedtls_platform_zeroize( handshake,
3621 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003622}
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003626 if( session == NULL )
3627 return;
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003630 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003631#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003632
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003633#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003635#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003636
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003637 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003638}
3639
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003640#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003641
3642#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3643#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3644#else
3645#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3646#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3647
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003648#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003649
3650#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3651#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3652#else
3653#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3654#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3655
3656#if defined(MBEDTLS_SSL_ALPN)
3657#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3658#else
3659#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3660#endif /* MBEDTLS_SSL_ALPN */
3661
3662#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3663#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3664#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3665#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3666
3667#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3668 ( (uint32_t) ( \
3669 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3670 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3671 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3672 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3673 0u ) )
3674
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003675static unsigned char ssl_serialized_context_header[] = {
3676 MBEDTLS_VERSION_MAJOR,
3677 MBEDTLS_VERSION_MINOR,
3678 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003679 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3680 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3681 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3682 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3683 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003684};
3685
Paul Bakker5121ce52009-01-03 21:22:43 +00003686/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003687 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003688 *
3689 * The format of the serialized data is:
3690 * (in the presentation language of TLS, RFC 8446 section 3)
3691 *
3692 * // header
3693 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003694 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003695 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003696 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003697 * Note: When updating the format, remember to keep these
3698 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003699 *
3700 * // session sub-structure
3701 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3702 * // transform sub-structure
3703 * uint8 random[64]; // ServerHello.random+ClientHello.random
3704 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3705 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3706 * // fields from ssl_context
3707 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3708 * uint64 in_window_top; // DTLS: last validated record seq_num
3709 * uint64 in_window; // DTLS: bitmask for replay protection
3710 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3711 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3712 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3713 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3714 *
3715 * Note that many fields of the ssl_context or sub-structures are not
3716 * serialized, as they fall in one of the following categories:
3717 *
3718 * 1. forced value (eg in_left must be 0)
3719 * 2. pointer to dynamically-allocated memory (eg session, transform)
3720 * 3. value can be re-derived from other data (eg session keys from MS)
3721 * 4. value was temporary (eg content of input buffer)
3722 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003723 */
3724int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3725 unsigned char *buf,
3726 size_t buf_len,
3727 size_t *olen )
3728{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003729 unsigned char *p = buf;
3730 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003731 size_t session_len;
3732 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003733
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003734 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003735 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3736 * this function's documentation.
3737 *
3738 * These are due to assumptions/limitations in the implementation. Some of
3739 * them are likely to stay (no handshake in progress) some might go away
3740 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003741 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003742 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003743 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003744 {
3745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003747 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003748 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003749 {
3750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003752 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003753 /* Double-check that sub-structures are indeed ready */
3754 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003755 {
3756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003758 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003759 /* There must be no pending incoming or outgoing data */
3760 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003761 {
3762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003764 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003765 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003766 {
3767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003769 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003770 /* Protocol must be DLTS, not TLS */
3771 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003772 {
3773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003775 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003776 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003777 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003778 {
3779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003781 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003782 /* We must be using an AEAD ciphersuite */
3783 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003784 {
3785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003787 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003788 /* Renegotiation must not be enabled */
3789#if defined(MBEDTLS_SSL_RENEGOTIATION)
3790 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003791 {
3792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
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#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003796
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003797 /*
3798 * Version and format identifier
3799 */
3800 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003801
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003802 if( used <= buf_len )
3803 {
3804 memcpy( p, ssl_serialized_context_header,
3805 sizeof( ssl_serialized_context_header ) );
3806 p += sizeof( ssl_serialized_context_header );
3807 }
3808
3809 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003810 * Session (length + data)
3811 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003812 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003813 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3814 return( ret );
3815
3816 used += 4 + session_len;
3817 if( used <= buf_len )
3818 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003819 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3820 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003821
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003822 ret = ssl_session_save( ssl->session, 1,
3823 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003824 if( ret != 0 )
3825 return( ret );
3826
3827 p += session_len;
3828 }
3829
3830 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003831 * Transform
3832 */
3833 used += sizeof( ssl->transform->randbytes );
3834 if( used <= buf_len )
3835 {
3836 memcpy( p, ssl->transform->randbytes,
3837 sizeof( ssl->transform->randbytes ) );
3838 p += sizeof( ssl->transform->randbytes );
3839 }
3840
3841#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3842 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3843 if( used <= buf_len )
3844 {
3845 *p++ = ssl->transform->in_cid_len;
3846 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3847 p += ssl->transform->in_cid_len;
3848
3849 *p++ = ssl->transform->out_cid_len;
3850 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3851 p += ssl->transform->out_cid_len;
3852 }
3853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3854
3855 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003856 * Saved fields from top-level ssl_context structure
3857 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003858 used += 4;
3859 if( used <= buf_len )
3860 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003861 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3862 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003863 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003864
3865#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3866 used += 16;
3867 if( used <= buf_len )
3868 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003869 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3870 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003871
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003872 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3873 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003874 }
3875#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3876
3877#if defined(MBEDTLS_SSL_PROTO_DTLS)
3878 used += 1;
3879 if( used <= buf_len )
3880 {
3881 *p++ = ssl->disable_datagram_packing;
3882 }
3883#endif /* MBEDTLS_SSL_PROTO_DTLS */
3884
Jerry Yuae0b2e22021-10-08 15:21:19 +08003885 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003886 if( used <= buf_len )
3887 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003888 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3889 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003890 }
3891
3892#if defined(MBEDTLS_SSL_PROTO_DTLS)
3893 used += 2;
3894 if( used <= buf_len )
3895 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003896 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3897 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003898 }
3899#endif /* MBEDTLS_SSL_PROTO_DTLS */
3900
3901#if defined(MBEDTLS_SSL_ALPN)
3902 {
3903 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003904 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003905 : 0;
3906
3907 used += 1 + alpn_len;
3908 if( used <= buf_len )
3909 {
3910 *p++ = alpn_len;
3911
3912 if( ssl->alpn_chosen != NULL )
3913 {
3914 memcpy( p, ssl->alpn_chosen, alpn_len );
3915 p += alpn_len;
3916 }
3917 }
3918 }
3919#endif /* MBEDTLS_SSL_ALPN */
3920
3921 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003922 * Done
3923 */
3924 *olen = used;
3925
3926 if( used > buf_len )
3927 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003928
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003929 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3930
Hanno Becker43aefe22020-02-05 10:44:56 +00003931 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003932}
3933
3934/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003935 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003936 *
3937 * This internal version is wrapped by a public function that cleans up in
3938 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003939 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003940MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003941static int ssl_context_load( mbedtls_ssl_context *ssl,
3942 const unsigned char *buf,
3943 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003944{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003945 const unsigned char *p = buf;
3946 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003947 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003949
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003950 /*
3951 * The context should have been freshly setup or reset.
3952 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003953 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003954 * renegotiating, or if the user mistakenly loaded a session first.)
3955 */
3956 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3957 ssl->session != NULL )
3958 {
3959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3960 }
3961
3962 /*
3963 * We can't check that the config matches the initial one, but we can at
3964 * least check it matches the requirements for serializing.
3965 */
3966 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003967 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3968 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003969#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003970 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003971#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003972 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003973 {
3974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3975 }
3976
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003977 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
3978
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003979 /*
3980 * Check version identifier
3981 */
3982 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
3983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3984
3985 if( memcmp( p, ssl_serialized_context_header,
3986 sizeof( ssl_serialized_context_header ) ) != 0 )
3987 {
3988 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3989 }
3990 p += sizeof( ssl_serialized_context_header );
3991
3992 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003993 * Session
3994 */
3995 if( (size_t)( end - p ) < 4 )
3996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3997
3998 session_len = ( (size_t) p[0] << 24 ) |
3999 ( (size_t) p[1] << 16 ) |
4000 ( (size_t) p[2] << 8 ) |
4001 ( (size_t) p[3] );
4002 p += 4;
4003
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004004 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004005 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004006 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004007 ssl->session_in = ssl->session;
4008 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004009 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004010
4011 if( (size_t)( end - p ) < session_len )
4012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4013
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004014 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004015 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004016 {
4017 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004018 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004019 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004020
4021 p += session_len;
4022
4023 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004024 * Transform
4025 */
4026
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004027 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004028 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004029 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004030 ssl->transform_in = ssl->transform;
4031 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004032 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004033
4034 /* Read random bytes and populate structure */
4035 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004037#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004038 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004039 ssl->session->ciphersuite,
4040 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004041#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004042 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004043#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004044 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4045 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004046 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004047 ssl->conf->endpoint,
4048 ssl );
4049 if( ret != 0 )
4050 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004051#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004052 p += sizeof( ssl->transform->randbytes );
4053
4054#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4055 /* Read connection IDs and store them */
4056 if( (size_t)( end - p ) < 1 )
4057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4058
4059 ssl->transform->in_cid_len = *p++;
4060
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004061 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4063
4064 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4065 p += ssl->transform->in_cid_len;
4066
4067 ssl->transform->out_cid_len = *p++;
4068
4069 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4071
4072 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4073 p += ssl->transform->out_cid_len;
4074#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4075
4076 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004077 * Saved fields from top-level ssl_context structure
4078 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004079 if( (size_t)( end - p ) < 4 )
4080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4081
4082 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4083 ( (uint32_t) p[1] << 16 ) |
4084 ( (uint32_t) p[2] << 8 ) |
4085 ( (uint32_t) p[3] );
4086 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004087
4088#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4089 if( (size_t)( end - p ) < 16 )
4090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4091
4092 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4093 ( (uint64_t) p[1] << 48 ) |
4094 ( (uint64_t) p[2] << 40 ) |
4095 ( (uint64_t) p[3] << 32 ) |
4096 ( (uint64_t) p[4] << 24 ) |
4097 ( (uint64_t) p[5] << 16 ) |
4098 ( (uint64_t) p[6] << 8 ) |
4099 ( (uint64_t) p[7] );
4100 p += 8;
4101
4102 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4103 ( (uint64_t) p[1] << 48 ) |
4104 ( (uint64_t) p[2] << 40 ) |
4105 ( (uint64_t) p[3] << 32 ) |
4106 ( (uint64_t) p[4] << 24 ) |
4107 ( (uint64_t) p[5] << 16 ) |
4108 ( (uint64_t) p[6] << 8 ) |
4109 ( (uint64_t) p[7] );
4110 p += 8;
4111#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4112
4113#if defined(MBEDTLS_SSL_PROTO_DTLS)
4114 if( (size_t)( end - p ) < 1 )
4115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4116
4117 ssl->disable_datagram_packing = *p++;
4118#endif /* MBEDTLS_SSL_PROTO_DTLS */
4119
Jerry Yud9a94fe2021-09-28 18:58:59 +08004120 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004122 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4123 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004124
4125#if defined(MBEDTLS_SSL_PROTO_DTLS)
4126 if( (size_t)( end - p ) < 2 )
4127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4128
4129 ssl->mtu = ( p[0] << 8 ) | p[1];
4130 p += 2;
4131#endif /* MBEDTLS_SSL_PROTO_DTLS */
4132
4133#if defined(MBEDTLS_SSL_ALPN)
4134 {
4135 uint8_t alpn_len;
4136 const char **cur;
4137
4138 if( (size_t)( end - p ) < 1 )
4139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4140
4141 alpn_len = *p++;
4142
4143 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4144 {
4145 /* alpn_chosen should point to an item in the configured list */
4146 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4147 {
4148 if( strlen( *cur ) == alpn_len &&
4149 memcmp( p, cur, alpn_len ) == 0 )
4150 {
4151 ssl->alpn_chosen = *cur;
4152 break;
4153 }
4154 }
4155 }
4156
4157 /* can only happen on conf mismatch */
4158 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4160
4161 p += alpn_len;
4162 }
4163#endif /* MBEDTLS_SSL_ALPN */
4164
4165 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004166 * Forced fields from top-level ssl_context structure
4167 *
4168 * Most of them already set to the correct value by mbedtls_ssl_init() and
4169 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4170 */
4171 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004172 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004173
Hanno Becker361b10d2019-08-30 10:42:49 +01004174 /* Adjust pointers for header fields of outgoing records to
4175 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004176 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004177
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004178#if defined(MBEDTLS_SSL_PROTO_DTLS)
4179 ssl->in_epoch = 1;
4180#endif
4181
4182 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4183 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004184 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4185 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004186 if( ssl->handshake != NULL )
4187 {
4188 mbedtls_ssl_handshake_free( ssl );
4189 mbedtls_free( ssl->handshake );
4190 ssl->handshake = NULL;
4191 }
4192
4193 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004194 * Done - should have consumed entire buffer
4195 */
4196 if( p != end )
4197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004198
4199 return( 0 );
4200}
4201
4202/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004203 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004204 */
4205int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4206 const unsigned char *buf,
4207 size_t len )
4208{
4209 int ret = ssl_context_load( context, buf, len );
4210
4211 if( ret != 0 )
4212 mbedtls_ssl_free( context );
4213
4214 return( ret );
4215}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004217
4218/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004219 * Free an SSL context
4220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004222{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004223 if( ssl == NULL )
4224 return;
4225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004227
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004228 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 {
sander-visserb8aa2072020-05-06 22:05:13 +02004230#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4231 size_t out_buf_len = ssl->out_buf_len;
4232#else
4233 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4234#endif
4235
Darryl Greenb33cc762019-11-28 14:29:44 +00004236 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004238 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004239 }
4240
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004241 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 {
sander-visserb8aa2072020-05-06 22:05:13 +02004243#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4244 size_t in_buf_len = ssl->in_buf_len;
4245#else
4246 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4247#endif
4248
Darryl Greenb33cc762019-11-28 14:29:44 +00004249 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004251 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 }
4253
Paul Bakker48916f92012-09-16 19:57:18 +00004254 if( ssl->transform )
4255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 mbedtls_ssl_transform_free( ssl->transform );
4257 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004258 }
4259
4260 if( ssl->handshake )
4261 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004262 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4264 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 mbedtls_free( ssl->handshake );
4267 mbedtls_free( ssl->transform_negotiate );
4268 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004269 }
4270
Ronald Cron6f135e12021-12-08 16:57:54 +01004271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004272 mbedtls_ssl_transform_free( ssl->transform_application );
4273 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004274#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004275
Paul Bakkerc0463502013-02-14 11:19:38 +01004276 if( ssl->session )
4277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 mbedtls_ssl_session_free( ssl->session );
4279 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004280 }
4281
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004282#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004283 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004285 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004287 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004290#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004292#endif
4293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004295
Paul Bakker86f04f42013-02-14 11:20:09 +01004296 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004297 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004298}
4299
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004300/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004301 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004302 */
4303void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4304{
4305 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4306}
4307
Gilles Peskineae270bf2021-06-02 00:05:29 +02004308/* The selection should be the same as mbedtls_x509_crt_profile_default in
4309 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004310 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004311 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004312 * about this list.
4313 */
Brett Warrene0edc842021-08-17 09:53:13 +01004314static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004315#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004316 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004317#endif
4318#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004319 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004320#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004321#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004322 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004323#endif
4324#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004325 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004326#endif
4327#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004328 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004329#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004330#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004331 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004332#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004333#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004334 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004335#endif
4336#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004337 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004338#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004339 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004340};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004341
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004342static int ssl_preset_suiteb_ciphersuites[] = {
4343 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4344 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4345 0
4346};
4347
Gilles Peskineeccd8882020-03-10 12:19:08 +01004348#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004349
Jerry Yu909df7b2022-01-22 11:56:27 +08004350/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004351 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004352 * rules SHOULD be upheld.
4353 * - No duplicate entries.
4354 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004355 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004356 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004357 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004358static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004359
Andrzej Kurek25f27152022-08-17 16:09:31 -04004360#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 +08004361 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4362 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004363#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004364 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004365
Andrzej Kurek25f27152022-08-17 16:09:31 -04004366#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 +08004367 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4368 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004369#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA &&
Jerry Yu909df7b2022-01-22 11:56:27 +08004370 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4371
Andrzej Kurek25f27152022-08-17 16:09:31 -04004372#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 +08004373 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4374 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004375#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004376 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004377
Andrzej Kurek25f27152022-08-17 16:09:31 -04004378#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 +08004379 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004380#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 +08004381
Andrzej Kurek25f27152022-08-17 16:09:31 -04004382#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 +08004383 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004384#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 +08004385
Andrzej Kurek25f27152022-08-17 16:09:31 -04004386#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 +08004387 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004388#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 +08004389
Andrzej Kurek25f27152022-08-17 16:09:31 -04004390#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 +08004391 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4392#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4393
Andrzej Kurek25f27152022-08-17 16:09:31 -04004394#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 +08004395 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4396#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4397
Andrzej Kurek25f27152022-08-17 16:09:31 -04004398#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 +08004399 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4400#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4401
Gabor Mezei15b95a62022-05-09 16:37:58 +02004402 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004403};
4404
4405/* NOTICE: see above */
4406#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4407static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004408#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004409#if defined(MBEDTLS_ECDSA_C)
4410 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004411#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004412#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4413 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4414#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004415#if defined(MBEDTLS_RSA_C)
4416 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4417#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004418#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA */
4419#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004420#if defined(MBEDTLS_ECDSA_C)
4421 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004422#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004423#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4424 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4425#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004426#if defined(MBEDTLS_RSA_C)
4427 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4428#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004429#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA */
4430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004431#if defined(MBEDTLS_ECDSA_C)
4432 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004433#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004434#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4435 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4436#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004437#if defined(MBEDTLS_RSA_C)
4438 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4439#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004440#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004441 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004442};
4443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4444/* NOTICE: see above */
4445static uint16_t ssl_preset_suiteb_sig_algs[] = {
4446
Andrzej Kurek25f27152022-08-17 16:09:31 -04004447#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 +08004448 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4449 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004450#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA &&
Jerry Yu909df7b2022-01-22 11:56:27 +08004451 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4452
Andrzej Kurek25f27152022-08-17 16:09:31 -04004453#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 +08004454 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4455 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004456#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA &&
Jerry Yu53037892022-01-25 11:02:06 +08004457 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004458
Andrzej Kurek25f27152022-08-17 16:09:31 -04004459#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 +08004460 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004461#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004462
Andrzej Kurek25f27152022-08-17 16:09:31 -04004463#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 +08004464 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004465#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Jerry Yu53037892022-01-25 11:02:06 +08004466
Gabor Mezei15b95a62022-05-09 16:37:58 +02004467 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004468};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004469
Jerry Yu909df7b2022-01-22 11:56:27 +08004470/* NOTICE: see above */
4471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4472static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004473#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004474#if defined(MBEDTLS_ECDSA_C)
4475 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004476#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004477#if defined(MBEDTLS_RSA_C)
4478 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4479#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004480#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
4481#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004482#if defined(MBEDTLS_ECDSA_C)
4483 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004484#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004485#if defined(MBEDTLS_RSA_C)
4486 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4487#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Gabor Mezei15b95a62022-05-09 16:37:58 +02004489 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004490};
Jerry Yu909df7b2022-01-22 11:56:27 +08004491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4492
Jerry Yu1a8b4812022-01-20 17:56:50 +08004493#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004494
Brett Warrene0edc842021-08-17 09:53:13 +01004495static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004496#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004497 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004498#endif
4499#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004500 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004501#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004502 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004503};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004504
Jerry Yu1a8b4812022-01-20 17:56:50 +08004505#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004506/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004507 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004508MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004509static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004510{
4511 size_t i, j;
4512 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004513
Gabor Mezei15b95a62022-05-09 16:37:58 +02004514 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004515 {
Jerry Yuf377d642022-01-25 10:43:59 +08004516 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004517 {
Jerry Yuf377d642022-01-25 10:43:59 +08004518 if( sig_algs[i] != sig_algs[j] )
4519 continue;
4520 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4521 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4522 sig_algs[i], j, i );
4523 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004524 }
4525 }
4526 return( ret );
4527}
4528
4529#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4530
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004531/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004532 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004533 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004534int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004535 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004536{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004537#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004539#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004540
Jerry Yu1a8b4812022-01-20 17:56:50 +08004541#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004542 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004543 {
4544 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4545 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4546 }
4547
Jerry Yuf377d642022-01-25 10:43:59 +08004548 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004549 {
4550 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4551 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4552 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004553
4554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004555 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004556 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004557 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004558 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4559 }
4560
Jerry Yuf377d642022-01-25 10:43:59 +08004561 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004562 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004563 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004564 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4565 }
4566#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004567#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4568
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004569 /* Use the functions here so that they are covered in tests,
4570 * but otherwise access member directly for efficiency */
4571 mbedtls_ssl_conf_endpoint( conf, endpoint );
4572 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004573
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004574 /*
4575 * Things that are common to all presets
4576 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004577#if defined(MBEDTLS_SSL_CLI_C)
4578 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4579 {
4580 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4581#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4582 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4583#endif
4584 }
4585#endif
4586
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004587#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4588 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4589#endif
4590
4591#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4592 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4593#endif
4594
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004595#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004596 conf->f_cookie_write = ssl_cookie_write_dummy;
4597 conf->f_cookie_check = ssl_cookie_check_dummy;
4598#endif
4599
4600#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4601 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4602#endif
4603
Janos Follath088ce432017-04-10 12:42:31 +01004604#if defined(MBEDTLS_SSL_SRV_C)
4605 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004606 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004607#endif
4608
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
4610 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4611 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4612#endif
4613
4614#if defined(MBEDTLS_SSL_RENEGOTIATION)
4615 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004616 memset( conf->renego_period, 0x00, 2 );
4617 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004618#endif
4619
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004620#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004621 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4622 {
4623 const unsigned char dhm_p[] =
4624 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4625 const unsigned char dhm_g[] =
4626 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004627
Hanno Beckere2defad2021-07-24 05:59:17 +01004628 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4629 dhm_p, sizeof( dhm_p ),
4630 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4631 {
4632 return( ret );
4633 }
4634 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004635#endif
4636
Ronald Cron6f135e12021-12-08 16:57:54 +01004637#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker71f1ed62021-07-24 06:01:47 +01004638 /*
4639 * Allow all TLS 1.3 key exchange modes by default.
4640 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004641 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004642#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004643
XiaokangQian4d3a6042022-04-21 13:46:17 +00004644 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4645 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004646#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004647 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004648 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004649#else
XiaokangQian060d8672022-04-21 09:24:56 +00004650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004651#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004652 }
4653 else
4654 {
4655#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4656 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4657 {
4658 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4659 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4660 }
4661 else
4662 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4663 {
4664 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4665 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4666 }
4667#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4668 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4669 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4670#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4671 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4672 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4673#else
4674 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4675#endif
4676 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004677
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004678 /*
4679 * Preset-specific defaults
4680 */
4681 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004682 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004683 /*
4684 * NSA Suite B
4685 */
4686 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004687
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004688 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004689
4690#if defined(MBEDTLS_X509_CRT_PARSE_C)
4691 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004692#endif
4693
Gilles Peskineeccd8882020-03-10 12:19:08 +01004694#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004695#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4696 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4697 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4698 else
4699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4700 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004701#endif
4702
Brett Warrene0edc842021-08-17 09:53:13 +01004703#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4704 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004705#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004706 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004707 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004708
4709 /*
4710 * Default
4711 */
4712 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004713
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004714 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004715
4716#if defined(MBEDTLS_X509_CRT_PARSE_C)
4717 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4718#endif
4719
Gilles Peskineeccd8882020-03-10 12:19:08 +01004720#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4722 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4723 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4724 else
4725#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4726 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004727#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728
Brett Warrene0edc842021-08-17 09:53:13 +01004729#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4730 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004731#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004732 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004733
4734#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4735 conf->dhm_min_bitlen = 1024;
4736#endif
4737 }
4738
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004739 return( 0 );
4740}
4741
4742/*
4743 * Free mbedtls_ssl_config
4744 */
4745void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4746{
4747#if defined(MBEDTLS_DHM_C)
4748 mbedtls_mpi_free( &conf->dhm_P );
4749 mbedtls_mpi_free( &conf->dhm_G );
4750#endif
4751
Gilles Peskineeccd8882020-03-10 12:19:08 +01004752#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004753#if defined(MBEDTLS_USE_PSA_CRYPTO)
4754 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4755 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004756 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4757 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004758#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004759 if( conf->psk != NULL )
4760 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004761 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004762 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004763 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004764 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004765 }
4766
4767 if( conf->psk_identity != NULL )
4768 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004769 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004770 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004771 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004772 conf->psk_identity_len = 0;
4773 }
4774#endif
4775
4776#if defined(MBEDTLS_X509_CRT_PARSE_C)
4777 ssl_key_cert_free( conf->key_cert );
4778#endif
4779
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004780 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004781}
4782
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004783#if defined(MBEDTLS_PK_C) && \
4784 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004785/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790#if defined(MBEDTLS_RSA_C)
4791 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4792 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#if defined(MBEDTLS_ECDSA_C)
4795 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4796 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004799}
4800
Hanno Becker7e5437a2017-04-28 17:15:26 +01004801unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4802{
4803 switch( type ) {
4804 case MBEDTLS_PK_RSA:
4805 return( MBEDTLS_SSL_SIG_RSA );
4806 case MBEDTLS_PK_ECDSA:
4807 case MBEDTLS_PK_ECKEY:
4808 return( MBEDTLS_SSL_SIG_ECDSA );
4809 default:
4810 return( MBEDTLS_SSL_SIG_ANON );
4811 }
4812}
4813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004815{
4816 switch( sig )
4817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if defined(MBEDTLS_RSA_C)
4819 case MBEDTLS_SSL_SIG_RSA:
4820 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#if defined(MBEDTLS_ECDSA_C)
4823 case MBEDTLS_SSL_SIG_ECDSA:
4824 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004825#endif
4826 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004828 }
4829}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004830#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004831
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004832/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004833 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004836{
4837 switch( hash )
4838 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004839#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 case MBEDTLS_SSL_HASH_MD5:
4841 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004842#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004843#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 case MBEDTLS_SSL_HASH_SHA1:
4845 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004846#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004847#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 case MBEDTLS_SSL_HASH_SHA224:
4849 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004850#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004851#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 case MBEDTLS_SSL_HASH_SHA256:
4853 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004854#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004855#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 case MBEDTLS_SSL_HASH_SHA384:
4857 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004858#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004859#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 case MBEDTLS_SSL_HASH_SHA512:
4861 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004862#endif
4863 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004865 }
4866}
4867
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004868/*
4869 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4870 */
4871unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4872{
4873 switch( md )
4874 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004876 case MBEDTLS_MD_MD5:
4877 return( MBEDTLS_SSL_HASH_MD5 );
4878#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004880 case MBEDTLS_MD_SHA1:
4881 return( MBEDTLS_SSL_HASH_SHA1 );
4882#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004883#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004884 case MBEDTLS_MD_SHA224:
4885 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004886#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004887#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004888 case MBEDTLS_MD_SHA256:
4889 return( MBEDTLS_SSL_HASH_SHA256 );
4890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004892 case MBEDTLS_MD_SHA384:
4893 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004894#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004895#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004896 case MBEDTLS_MD_SHA512:
4897 return( MBEDTLS_SSL_HASH_SHA512 );
4898#endif
4899 default:
4900 return( MBEDTLS_SSL_HASH_NONE );
4901 }
4902}
4903
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004904/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004905 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004906 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004907 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004908int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004909{
Brett Warrene0edc842021-08-17 09:53:13 +01004910 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004911
Brett Warrene0edc842021-08-17 09:53:13 +01004912 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004913 return( -1 );
4914
Brett Warrene0edc842021-08-17 09:53:13 +01004915 for( ; *group_list != 0; group_list++ )
4916 {
4917 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004918 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004919 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004920
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004921 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004922}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004923
4924#if defined(MBEDTLS_ECP_C)
4925/*
4926 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4927 */
4928int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4929{
Manuel Pégourié-Gonnard422370d2022-02-07 11:55:21 +01004930 uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id;
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004931 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4932}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004933#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#if defined(MBEDTLS_X509_CRT_PARSE_C)
4936int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4937 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004938 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004939 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004940{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004941 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004942 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004943 const char *ext_oid;
4944 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004947 {
4948 /* Server part of the key exchange */
4949 switch( ciphersuite->key_exchange )
4950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 case MBEDTLS_KEY_EXCHANGE_RSA:
4952 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004953 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004954 break;
4955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4957 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4958 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4959 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004960 break;
4961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
4963 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004964 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004965 break;
4966
4967 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 case MBEDTLS_KEY_EXCHANGE_NONE:
4969 case MBEDTLS_KEY_EXCHANGE_PSK:
4970 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
4971 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02004972 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004973 usage = 0;
4974 }
4975 }
4976 else
4977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
4979 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004980 }
4981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004983 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004984 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004985 ret = -1;
4986 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 ext_oid = MBEDTLS_OID_SERVER_AUTH;
4991 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004992 }
4993 else
4994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
4996 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004997 }
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005000 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005001 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005002 ret = -1;
5003 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005004
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005005 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005008
Jerry Yu148165c2021-09-24 23:20:59 +08005009#if defined(MBEDTLS_USE_PSA_CRYPTO)
5010int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5011 const mbedtls_md_type_t md,
5012 unsigned char *dst,
5013 size_t dst_len,
5014 size_t *olen )
5015{
Ronald Cronf6893e12022-01-07 22:09:01 +01005016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5017 psa_hash_operation_t *hash_operation_to_clone;
5018 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5019
Jerry Yu148165c2021-09-24 23:20:59 +08005020 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005021
5022 switch( md )
5023 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005024#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005025 case MBEDTLS_MD_SHA384:
5026 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5027 break;
5028#endif
5029
Andrzej Kurek25f27152022-08-17 16:09:31 -04005030#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005031 case MBEDTLS_MD_SHA256:
5032 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5033 break;
5034#endif
5035
5036 default:
5037 goto exit;
5038 }
5039
5040 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5041 if( status != PSA_SUCCESS )
5042 goto exit;
5043
5044 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5045 if( status != PSA_SUCCESS )
5046 goto exit;
5047
5048exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005049 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005050}
5051#else /* MBEDTLS_USE_PSA_CRYPTO */
5052
Andrzej Kurek25f27152022-08-17 16:09:31 -04005053#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005054MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005055static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5056 unsigned char *dst,
5057 size_t dst_len,
5058 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005059{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005060 int ret;
5061 mbedtls_sha512_context sha512;
5062
5063 if( dst_len < 48 )
5064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5065
5066 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005067 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005068
5069 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5070 {
5071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5072 goto exit;
5073 }
5074
5075 *olen = 48;
5076
5077exit:
5078
5079 mbedtls_sha512_free( &sha512 );
5080 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005081}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005082#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005083
Andrzej Kurek25f27152022-08-17 16:09:31 -04005084#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005085MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005086static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5087 unsigned char *dst,
5088 size_t dst_len,
5089 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005090{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005091 int ret;
5092 mbedtls_sha256_context sha256;
5093
5094 if( dst_len < 32 )
5095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5096
5097 mbedtls_sha256_init( &sha256 );
5098 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005099
Jerry Yu24c0ec32021-09-09 14:21:07 +08005100 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5101 {
5102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5103 goto exit;
5104 }
5105
5106 *olen = 32;
5107
5108exit:
5109
5110 mbedtls_sha256_free( &sha256 );
5111 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005112}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005113#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005114
Jerry Yu000f9762021-09-14 11:12:51 +08005115int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5116 const mbedtls_md_type_t md,
5117 unsigned char *dst,
5118 size_t dst_len,
5119 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005121 switch( md )
5122 {
5123
Andrzej Kurek25f27152022-08-17 16:09:31 -04005124#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005125 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005126 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurek25f27152022-08-17 16:09:31 -04005127#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005128
Andrzej Kurek25f27152022-08-17 16:09:31 -04005129#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005130 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005131 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurek25f27152022-08-17 16:09:31 -04005132#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005133
5134 default:
5135 break;
5136 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005138}
XiaokangQian647719a2021-12-07 09:16:29 +00005139
Jerry Yu148165c2021-09-24 23:20:59 +08005140#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005141
Gabor Mezei078e8032022-04-27 21:17:56 +02005142#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5143/* mbedtls_ssl_parse_sig_alg_ext()
5144 *
5145 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5146 * value (TLS 1.3 RFC8446):
5147 * enum {
5148 * ....
5149 * ecdsa_secp256r1_sha256( 0x0403 ),
5150 * ecdsa_secp384r1_sha384( 0x0503 ),
5151 * ecdsa_secp521r1_sha512( 0x0603 ),
5152 * ....
5153 * } SignatureScheme;
5154 *
5155 * struct {
5156 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5157 * } SignatureSchemeList;
5158 *
5159 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5160 * value (TLS 1.2 RFC5246):
5161 * enum {
5162 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5163 * sha512(6), (255)
5164 * } HashAlgorithm;
5165 *
5166 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5167 * SignatureAlgorithm;
5168 *
5169 * struct {
5170 * HashAlgorithm hash;
5171 * SignatureAlgorithm signature;
5172 * } SignatureAndHashAlgorithm;
5173 *
5174 * SignatureAndHashAlgorithm
5175 * supported_signature_algorithms<2..2^16-2>;
5176 *
5177 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5178 * generalization of the TLS 1.2 signature algorithm extension.
5179 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5180 * `SignatureScheme` field of TLS 1.3
5181 *
5182 */
5183int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5184 const unsigned char *buf,
5185 const unsigned char *end )
5186{
5187 const unsigned char *p = buf;
5188 size_t supported_sig_algs_len = 0;
5189 const unsigned char *supported_sig_algs_end;
5190 uint16_t sig_alg;
5191 uint32_t common_idx = 0;
5192
5193 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5194 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5195 p += 2;
5196
5197 memset( ssl->handshake->received_sig_algs, 0,
5198 sizeof(ssl->handshake->received_sig_algs) );
5199
5200 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5201 supported_sig_algs_end = p + supported_sig_algs_len;
5202 while( p < supported_sig_algs_end )
5203 {
5204 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5205 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5206 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005207 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5208 sig_alg,
5209 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005210#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005211 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005212 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5213 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5214 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005215 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005216 }
5217#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005218
Jerry Yuf3b46b52022-06-19 16:52:27 +08005219 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5220 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005221
5222 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5223 {
5224 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5225 common_idx += 1;
5226 }
5227 }
5228 /* Check that we consumed all the message. */
5229 if( p != end )
5230 {
5231 MBEDTLS_SSL_DEBUG_MSG( 1,
5232 ( "Signature algorithms extension length misaligned" ) );
5233 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5234 MBEDTLS_ERR_SSL_DECODE_ERROR );
5235 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5236 }
5237
5238 if( common_idx == 0 )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5241 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5242 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5243 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5244 }
5245
Gabor Mezei15b95a62022-05-09 16:37:58 +02005246 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005247 return( 0 );
5248}
5249
5250#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5251
Jerry Yudc7bd172022-02-17 13:44:15 +08005252#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5253
5254#if defined(MBEDTLS_USE_PSA_CRYPTO)
5255
5256static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5257 mbedtls_svc_key_id_t key,
5258 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005259 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005260 const unsigned char* seed, size_t seed_length,
5261 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005262 const unsigned char* other_secret,
5263 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005264 size_t capacity )
5265{
5266 psa_status_t status;
5267
5268 status = psa_key_derivation_setup( derivation, alg );
5269 if( status != PSA_SUCCESS )
5270 return( status );
5271
5272 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5273 {
5274 status = psa_key_derivation_input_bytes( derivation,
5275 PSA_KEY_DERIVATION_INPUT_SEED,
5276 seed, seed_length );
5277 if( status != PSA_SUCCESS )
5278 return( status );
5279
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005280 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005281 {
5282 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005283 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5284 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005285 if( status != PSA_SUCCESS )
5286 return( status );
5287 }
5288
Jerry Yudc7bd172022-02-17 13:44:15 +08005289 if( mbedtls_svc_key_id_is_null( key ) )
5290 {
5291 status = psa_key_derivation_input_bytes(
5292 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005293 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005294 }
5295 else
5296 {
5297 status = psa_key_derivation_input_key(
5298 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5299 }
5300 if( status != PSA_SUCCESS )
5301 return( status );
5302
5303 status = psa_key_derivation_input_bytes( derivation,
5304 PSA_KEY_DERIVATION_INPUT_LABEL,
5305 label, label_length );
5306 if( status != PSA_SUCCESS )
5307 return( status );
5308 }
5309 else
5310 {
5311 return( PSA_ERROR_NOT_SUPPORTED );
5312 }
5313
5314 status = psa_key_derivation_set_capacity( derivation, capacity );
5315 if( status != PSA_SUCCESS )
5316 return( status );
5317
5318 return( PSA_SUCCESS );
5319}
5320
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005321MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005322static int tls_prf_generic( mbedtls_md_type_t md_type,
5323 const unsigned char *secret, size_t slen,
5324 const char *label,
5325 const unsigned char *random, size_t rlen,
5326 unsigned char *dstbuf, size_t dlen )
5327{
5328 psa_status_t status;
5329 psa_algorithm_t alg;
5330 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5331 psa_key_derivation_operation_t derivation =
5332 PSA_KEY_DERIVATION_OPERATION_INIT;
5333
5334 if( md_type == MBEDTLS_MD_SHA384 )
5335 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5336 else
5337 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5338
5339 /* Normally a "secret" should be long enough to be impossible to
5340 * find by brute force, and in particular should not be empty. But
5341 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5342 * and for this use case it makes sense to have a 0-length "secret".
5343 * Since the key API doesn't allow importing a key of length 0,
5344 * keep master_key=0, which setup_psa_key_derivation() understands
5345 * to mean a 0-length "secret" input. */
5346 if( slen != 0 )
5347 {
5348 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5349 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5350 psa_set_key_algorithm( &key_attributes, alg );
5351 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5352
5353 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5354 if( status != PSA_SUCCESS )
5355 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5356 }
5357
5358 status = setup_psa_key_derivation( &derivation,
5359 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005360 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005361 random, rlen,
5362 (unsigned char const *) label,
5363 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005364 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005365 dlen );
5366 if( status != PSA_SUCCESS )
5367 {
5368 psa_key_derivation_abort( &derivation );
5369 psa_destroy_key( master_key );
5370 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5371 }
5372
5373 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5374 if( status != PSA_SUCCESS )
5375 {
5376 psa_key_derivation_abort( &derivation );
5377 psa_destroy_key( master_key );
5378 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5379 }
5380
5381 status = psa_key_derivation_abort( &derivation );
5382 if( status != PSA_SUCCESS )
5383 {
5384 psa_destroy_key( master_key );
5385 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5386 }
5387
5388 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5389 status = psa_destroy_key( master_key );
5390 if( status != PSA_SUCCESS )
5391 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5392
5393 return( 0 );
5394}
5395
5396#else /* MBEDTLS_USE_PSA_CRYPTO */
5397
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005398MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005399static int tls_prf_generic( mbedtls_md_type_t md_type,
5400 const unsigned char *secret, size_t slen,
5401 const char *label,
5402 const unsigned char *random, size_t rlen,
5403 unsigned char *dstbuf, size_t dlen )
5404{
5405 size_t nb;
5406 size_t i, j, k, md_len;
5407 unsigned char *tmp;
5408 size_t tmp_len = 0;
5409 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5410 const mbedtls_md_info_t *md_info;
5411 mbedtls_md_context_t md_ctx;
5412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5413
5414 mbedtls_md_init( &md_ctx );
5415
5416 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5418
5419 md_len = mbedtls_md_get_size( md_info );
5420
5421 tmp_len = md_len + strlen( label ) + rlen;
5422 tmp = mbedtls_calloc( 1, tmp_len );
5423 if( tmp == NULL )
5424 {
5425 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5426 goto exit;
5427 }
5428
5429 nb = strlen( label );
5430 memcpy( tmp + md_len, label, nb );
5431 memcpy( tmp + md_len + nb, random, rlen );
5432 nb += rlen;
5433
5434 /*
5435 * Compute P_<hash>(secret, label + random)[0..dlen]
5436 */
5437 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5438 goto exit;
5439
5440 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5441 if( ret != 0 )
5442 goto exit;
5443 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5444 if( ret != 0 )
5445 goto exit;
5446 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5447 if( ret != 0 )
5448 goto exit;
5449
5450 for( i = 0; i < dlen; i += md_len )
5451 {
5452 ret = mbedtls_md_hmac_reset ( &md_ctx );
5453 if( ret != 0 )
5454 goto exit;
5455 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5456 if( ret != 0 )
5457 goto exit;
5458 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5459 if( ret != 0 )
5460 goto exit;
5461
5462 ret = mbedtls_md_hmac_reset ( &md_ctx );
5463 if( ret != 0 )
5464 goto exit;
5465 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5466 if( ret != 0 )
5467 goto exit;
5468 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5469 if( ret != 0 )
5470 goto exit;
5471
5472 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5473
5474 for( j = 0; j < k; j++ )
5475 dstbuf[i + j] = h_i[j];
5476 }
5477
5478exit:
5479 mbedtls_md_free( &md_ctx );
5480
5481 mbedtls_platform_zeroize( tmp, tmp_len );
5482 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5483
5484 mbedtls_free( tmp );
5485
5486 return( ret );
5487}
5488#endif /* MBEDTLS_USE_PSA_CRYPTO */
5489
Andrzej Kurek25f27152022-08-17 16:09:31 -04005490#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005491MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005492static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5493 const char *label,
5494 const unsigned char *random, size_t rlen,
5495 unsigned char *dstbuf, size_t dlen )
5496{
5497 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5498 label, random, rlen, dstbuf, dlen ) );
5499}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005500#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Jerry Yudc7bd172022-02-17 13:44:15 +08005501
Andrzej Kurek25f27152022-08-17 16:09:31 -04005502#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005503MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005504static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5505 const char *label,
5506 const unsigned char *random, size_t rlen,
5507 unsigned char *dstbuf, size_t dlen )
5508{
5509 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5510 label, random, rlen, dstbuf, dlen ) );
5511}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005512#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA */
Jerry Yudc7bd172022-02-17 13:44:15 +08005513
Jerry Yuf009d862022-02-17 14:01:37 +08005514/*
5515 * Set appropriate PRF function and other SSL / TLS1.2 functions
5516 *
5517 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005518 * - hash associated with the ciphersuite (only used by TLS 1.2)
5519 *
5520 * Outputs:
5521 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5522 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005523MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005524static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005525 mbedtls_md_type_t hash )
5526{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005527#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005528 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005529 {
5530 handshake->tls_prf = tls_prf_sha384;
5531 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5532 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5533 }
5534 else
5535#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005536#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005537 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005538 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005539 handshake->tls_prf = tls_prf_sha256;
5540 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5541 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5542 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005543#else
Jerry Yuf009d862022-02-17 14:01:37 +08005544 {
Jerry Yuf009d862022-02-17 14:01:37 +08005545 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005546 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5548 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005549#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005550
5551 return( 0 );
5552}
Jerry Yud6ab2352022-02-17 14:03:43 +08005553
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005554/*
5555 * Compute master secret if needed
5556 *
5557 * Parameters:
5558 * [in/out] handshake
5559 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5560 * (PSA-PSK) ciphersuite_info, psk_opaque
5561 * [out] premaster (cleared)
5562 * [out] master
5563 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5564 * debug: conf->f_dbg, conf->p_dbg
5565 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005566 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005567 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005568MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005569static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5570 unsigned char *master,
5571 const mbedtls_ssl_context *ssl )
5572{
5573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5574
5575 /* cf. RFC 5246, Section 8.1:
5576 * "The master secret is always exactly 48 bytes in length." */
5577 size_t const master_secret_len = 48;
5578
5579#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5580 unsigned char session_hash[48];
5581#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5582
5583 /* The label for the KDF used for key expansion.
5584 * This is either "master secret" or "extended master secret"
5585 * depending on whether the Extended Master Secret extension
5586 * is used. */
5587 char const *lbl = "master secret";
5588
Przemek Stekielae4ed302022-04-05 17:15:55 +02005589 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005590 * - If the Extended Master Secret extension is not used,
5591 * this is ClientHello.Random + ServerHello.Random
5592 * (see Sect. 8.1 in RFC 5246).
5593 * - If the Extended Master Secret extension is used,
5594 * this is the transcript of the handshake so far.
5595 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005596 unsigned char const *seed = handshake->randbytes;
5597 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005598
5599#if !defined(MBEDTLS_DEBUG_C) && \
5600 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5601 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5602 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5603 ssl = NULL; /* make sure we don't use it except for those cases */
5604 (void) ssl;
5605#endif
5606
5607 if( handshake->resume != 0 )
5608 {
5609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5610 return( 0 );
5611 }
5612
5613#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5614 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5615 {
5616 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005617 seed = session_hash;
5618 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005619
5620 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005621 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005622 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005623#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005624
Przemek Stekiel99114f32022-04-22 11:20:09 +02005625#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005626 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005627 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005628 {
5629 /* Perform PSK-to-MS expansion in a single step. */
5630 psa_status_t status;
5631 psa_algorithm_t alg;
5632 mbedtls_svc_key_id_t psk;
5633 psa_key_derivation_operation_t derivation =
5634 PSA_KEY_DERIVATION_OPERATION_INIT;
5635 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5636
5637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5638
5639 psk = mbedtls_ssl_get_opaque_psk( ssl );
5640
5641 if( hash_alg == MBEDTLS_MD_SHA384 )
5642 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5643 else
5644 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5645
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005646 size_t other_secret_len = 0;
5647 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005648
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005649 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005650 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005651 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005652 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005653 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005654 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005655 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005656 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005657 other_secret_len = 48;
5658 other_secret = handshake->premaster + 2;
5659 break;
5660 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005661 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005662 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5663 other_secret = handshake->premaster + 2;
5664 break;
5665 default:
5666 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005667 }
5668
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005669 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005670 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005671 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005672 (unsigned char const *) lbl,
5673 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005674 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005675 master_secret_len );
5676 if( status != PSA_SUCCESS )
5677 {
5678 psa_key_derivation_abort( &derivation );
5679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5680 }
5681
5682 status = psa_key_derivation_output_bytes( &derivation,
5683 master,
5684 master_secret_len );
5685 if( status != PSA_SUCCESS )
5686 {
5687 psa_key_derivation_abort( &derivation );
5688 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5689 }
5690
5691 status = psa_key_derivation_abort( &derivation );
5692 if( status != PSA_SUCCESS )
5693 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5694 }
5695 else
5696#endif
5697 {
5698 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005699 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005700 master,
5701 master_secret_len );
5702 if( ret != 0 )
5703 {
5704 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5705 return( ret );
5706 }
5707
5708 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5709 handshake->premaster,
5710 handshake->pmslen );
5711
5712 mbedtls_platform_zeroize( handshake->premaster,
5713 sizeof(handshake->premaster) );
5714 }
5715
5716 return( 0 );
5717}
5718
Jerry Yud62f87e2022-02-17 14:09:02 +08005719int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5720{
5721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5722 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5723 ssl->handshake->ciphersuite_info;
5724
5725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5726
5727 /* Set PRF, calc_verify and calc_finished function pointers */
5728 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005729 ciphersuite_info->mac );
5730 if( ret != 0 )
5731 {
5732 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5733 return( ret );
5734 }
5735
5736 /* Compute master secret if needed */
5737 ret = ssl_compute_master( ssl->handshake,
5738 ssl->session_negotiate->master,
5739 ssl );
5740 if( ret != 0 )
5741 {
5742 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5743 return( ret );
5744 }
5745
5746 /* Swap the client and server random values:
5747 * - MS derivation wanted client+server (RFC 5246 8.1)
5748 * - key derivation wants server+client (RFC 5246 6.3) */
5749 {
5750 unsigned char tmp[64];
5751 memcpy( tmp, ssl->handshake->randbytes, 64 );
5752 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5753 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5754 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5755 }
5756
5757 /* Populate transform structure */
5758 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5759 ssl->session_negotiate->ciphersuite,
5760 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005761#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005762 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005763#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005764 ssl->handshake->tls_prf,
5765 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005766 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005767 ssl->conf->endpoint,
5768 ssl );
5769 if( ret != 0 )
5770 {
5771 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5772 return( ret );
5773 }
5774
5775 /* We no longer need Server/ClientHello.random values */
5776 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5777 sizeof( ssl->handshake->randbytes ) );
5778
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5780
5781 return( 0 );
5782}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005783
Ronald Cron4dcbca92022-03-07 10:21:40 +01005784int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5785{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005786 switch( md )
5787 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005788#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005789 case MBEDTLS_SSL_HASH_SHA384:
5790 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5791 break;
5792#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005793#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005794 case MBEDTLS_SSL_HASH_SHA256:
5795 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5796 break;
5797#endif
5798 default:
5799 return( -1 );
5800 }
5801
Ronald Cronc2f13a02022-03-07 10:25:24 +01005802 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005803}
5804
Andrzej Kurek25f27152022-08-17 16:09:31 -04005805#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005806void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5807 unsigned char *hash,
5808 size_t *hlen )
5809{
5810#if defined(MBEDTLS_USE_PSA_CRYPTO)
5811 size_t hash_size;
5812 psa_status_t status;
5813 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5816 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5817 if( status != PSA_SUCCESS )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5820 return;
5821 }
5822
5823 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5824 if( status != PSA_SUCCESS )
5825 {
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5827 return;
5828 }
5829
5830 *hlen = 32;
5831 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5833#else
5834 mbedtls_sha256_context sha256;
5835
5836 mbedtls_sha256_init( &sha256 );
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5839
5840 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5841 mbedtls_sha256_finish( &sha256, hash );
5842
5843 *hlen = 32;
5844
5845 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5847
5848 mbedtls_sha256_free( &sha256 );
5849#endif /* MBEDTLS_USE_PSA_CRYPTO */
5850 return;
5851}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005852#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005853
Andrzej Kurek25f27152022-08-17 16:09:31 -04005854#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005855void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5856 unsigned char *hash,
5857 size_t *hlen )
5858{
5859#if defined(MBEDTLS_USE_PSA_CRYPTO)
5860 size_t hash_size;
5861 psa_status_t status;
5862 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5863
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5865 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5866 if( status != PSA_SUCCESS )
5867 {
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5869 return;
5870 }
5871
5872 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5873 if( status != PSA_SUCCESS )
5874 {
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5876 return;
5877 }
5878
5879 *hlen = 48;
5880 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5882#else
5883 mbedtls_sha512_context sha512;
5884
5885 mbedtls_sha512_init( &sha512 );
5886
5887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5888
Andrzej Kureka242e832022-08-11 10:03:14 -04005889 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005890 mbedtls_sha512_finish( &sha512, hash );
5891
5892 *hlen = 48;
5893
5894 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5896
5897 mbedtls_sha512_free( &sha512 );
5898#endif /* MBEDTLS_USE_PSA_CRYPTO */
5899 return;
5900}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005901#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005902
Neil Armstrong80f6f322022-05-03 17:56:38 +02005903#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5904 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005905int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5906{
5907 unsigned char *p = ssl->handshake->premaster;
5908 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5909 const unsigned char *psk = NULL;
5910 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005911 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005912
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005913 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005914 {
5915 /*
5916 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005917 * checked before calling this function.
5918 *
5919 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005920 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005921 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005922 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5923 {
5924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5926 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005927 }
5928
5929 /*
5930 * PMS = struct {
5931 * opaque other_secret<0..2^16-1>;
5932 * opaque psk<0..2^16-1>;
5933 * };
5934 * with "other_secret" depending on the particular key exchange
5935 */
5936#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5937 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5938 {
5939 if( end - p < 2 )
5940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5941
5942 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5943 p += 2;
5944
5945 if( end < p || (size_t)( end - p ) < psk_len )
5946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5947
5948 memset( p, 0, psk_len );
5949 p += psk_len;
5950 }
5951 else
5952#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5953#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5954 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5955 {
5956 /*
5957 * other_secret already set by the ClientKeyExchange message,
5958 * and is 48 bytes long
5959 */
5960 if( end - p < 2 )
5961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5962
5963 *p++ = 0;
5964 *p++ = 48;
5965 p += 48;
5966 }
5967 else
5968#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
5969#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
5970 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5971 {
5972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5973 size_t len;
5974
5975 /* Write length only when we know the actual value */
5976 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
5977 p + 2, end - ( p + 2 ), &len,
5978 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5979 {
5980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
5981 return( ret );
5982 }
5983 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
5984 p += 2 + len;
5985
5986 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
5987 }
5988 else
5989#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02005990#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005991 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
5992 {
5993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5994 size_t zlen;
5995
5996 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
5997 p + 2, end - ( p + 2 ),
5998 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
5999 {
6000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6001 return( ret );
6002 }
6003
6004 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6005 p += 2 + zlen;
6006
6007 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6008 MBEDTLS_DEBUG_ECDH_Z );
6009 }
6010 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006011#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006012 {
6013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6015 }
6016
6017 /* opaque psk<0..2^16-1>; */
6018 if( end - p < 2 )
6019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6020
6021 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6022 p += 2;
6023
6024 if( end < p || (size_t)( end - p ) < psk_len )
6025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6026
6027 memcpy( p, psk, psk_len );
6028 p += psk_len;
6029
6030 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6031
6032 return( 0 );
6033}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006034#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006035
6036#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006037MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006038static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6039
6040#if defined(MBEDTLS_SSL_PROTO_DTLS)
6041int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6042{
6043 /* If renegotiation is not enforced, retransmit until we would reach max
6044 * timeout if we were using the usual handshake doubling scheme */
6045 if( ssl->conf->renego_max_records < 0 )
6046 {
6047 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6048 unsigned char doublings = 1;
6049
6050 while( ratio != 0 )
6051 {
6052 ++doublings;
6053 ratio >>= 1;
6054 }
6055
6056 if( ++ssl->renego_records_seen > doublings )
6057 {
6058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6059 return( 0 );
6060 }
6061 }
6062
6063 return( ssl_write_hello_request( ssl ) );
6064}
6065#endif
6066#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006067
Jerry Yud9526692022-02-17 14:23:47 +08006068/*
6069 * Handshake functions
6070 */
6071#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6072/* No certificate support -> dummy functions */
6073int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6074{
6075 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6076 ssl->handshake->ciphersuite_info;
6077
6078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6079
6080 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6081 {
6082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6083 ssl->state++;
6084 return( 0 );
6085 }
6086
6087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6088 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6089}
6090
6091int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6092{
6093 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6094 ssl->handshake->ciphersuite_info;
6095
6096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6097
6098 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6099 {
6100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6101 ssl->state++;
6102 return( 0 );
6103 }
6104
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6107}
6108
6109#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6110/* Some certificate support -> implement write and parse */
6111
6112int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6113{
6114 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6115 size_t i, n;
6116 const mbedtls_x509_crt *crt;
6117 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6118 ssl->handshake->ciphersuite_info;
6119
6120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6121
6122 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6123 {
6124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6125 ssl->state++;
6126 return( 0 );
6127 }
6128
6129#if defined(MBEDTLS_SSL_CLI_C)
6130 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6131 {
6132 if( ssl->handshake->client_auth == 0 )
6133 {
6134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6135 ssl->state++;
6136 return( 0 );
6137 }
6138 }
6139#endif /* MBEDTLS_SSL_CLI_C */
6140#if defined(MBEDTLS_SSL_SRV_C)
6141 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6142 {
6143 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6144 {
6145 /* Should never happen because we shouldn't have picked the
6146 * ciphersuite if we don't have a certificate. */
6147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6148 }
6149 }
6150#endif
6151
6152 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6153
6154 /*
6155 * 0 . 0 handshake type
6156 * 1 . 3 handshake length
6157 * 4 . 6 length of all certs
6158 * 7 . 9 length of cert. 1
6159 * 10 . n-1 peer certificate
6160 * n . n+2 length of cert. 2
6161 * n+3 . ... upper level cert, etc.
6162 */
6163 i = 7;
6164 crt = mbedtls_ssl_own_cert( ssl );
6165
6166 while( crt != NULL )
6167 {
6168 n = crt->raw.len;
6169 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6170 {
6171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6172 " > %" MBEDTLS_PRINTF_SIZET,
6173 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6174 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6175 }
6176
6177 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6178 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6179 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6180
6181 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6182 i += n; crt = crt->next;
6183 }
6184
6185 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6186 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6187 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6188
6189 ssl->out_msglen = i;
6190 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6191 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6192
6193 ssl->state++;
6194
6195 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6196 {
6197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6198 return( ret );
6199 }
6200
6201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6202
6203 return( ret );
6204}
6205
6206#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6207
6208#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006209MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006210static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6211 unsigned char *crt_buf,
6212 size_t crt_buf_len )
6213{
6214 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6215
6216 if( peer_crt == NULL )
6217 return( -1 );
6218
6219 if( peer_crt->raw.len != crt_buf_len )
6220 return( -1 );
6221
6222 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6223}
6224#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006225MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006226static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6227 unsigned char *crt_buf,
6228 size_t crt_buf_len )
6229{
6230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6231 unsigned char const * const peer_cert_digest =
6232 ssl->session->peer_cert_digest;
6233 mbedtls_md_type_t const peer_cert_digest_type =
6234 ssl->session->peer_cert_digest_type;
6235 mbedtls_md_info_t const * const digest_info =
6236 mbedtls_md_info_from_type( peer_cert_digest_type );
6237 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6238 size_t digest_len;
6239
6240 if( peer_cert_digest == NULL || digest_info == NULL )
6241 return( -1 );
6242
6243 digest_len = mbedtls_md_get_size( digest_info );
6244 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6245 return( -1 );
6246
6247 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6248 if( ret != 0 )
6249 return( -1 );
6250
6251 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6252}
6253#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6254#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6255
6256/*
6257 * Once the certificate message is read, parse it into a cert chain and
6258 * perform basic checks, but leave actual verification to the caller
6259 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006260MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006261static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6262 mbedtls_x509_crt *chain )
6263{
6264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6265#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6266 int crt_cnt=0;
6267#endif
6268 size_t i, n;
6269 uint8_t alert;
6270
6271 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6272 {
6273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6274 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6275 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6276 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6277 }
6278
6279 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6280 {
6281 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6282 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6283 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6284 }
6285
6286 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6287 {
6288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6290 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6291 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6292 }
6293
6294 i = mbedtls_ssl_hs_hdr_len( ssl );
6295
6296 /*
6297 * Same message structure as in mbedtls_ssl_write_certificate()
6298 */
6299 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6300
6301 if( ssl->in_msg[i] != 0 ||
6302 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6303 {
6304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6305 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6306 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6307 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6308 }
6309
6310 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6311 i += 3;
6312
6313 /* Iterate through and parse the CRTs in the provided chain. */
6314 while( i < ssl->in_hslen )
6315 {
6316 /* Check that there's room for the next CRT's length fields. */
6317 if ( i + 3 > ssl->in_hslen ) {
6318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6319 mbedtls_ssl_send_alert_message( ssl,
6320 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6321 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6322 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6323 }
6324 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6325 * anything beyond 2**16 ~ 64K. */
6326 if( ssl->in_msg[i] != 0 )
6327 {
6328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6329 mbedtls_ssl_send_alert_message( ssl,
6330 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6331 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6332 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6333 }
6334
6335 /* Read length of the next CRT in the chain. */
6336 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6337 | (unsigned int) ssl->in_msg[i + 2];
6338 i += 3;
6339
6340 if( n < 128 || i + n > ssl->in_hslen )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6343 mbedtls_ssl_send_alert_message( ssl,
6344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6345 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6346 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6347 }
6348
6349 /* Check if we're handling the first CRT in the chain. */
6350#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6351 if( crt_cnt++ == 0 &&
6352 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6353 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6354 {
6355 /* During client-side renegotiation, check that the server's
6356 * end-CRTs hasn't changed compared to the initial handshake,
6357 * mitigating the triple handshake attack. On success, reuse
6358 * the original end-CRT instead of parsing it again. */
6359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6360 if( ssl_check_peer_crt_unchanged( ssl,
6361 &ssl->in_msg[i],
6362 n ) != 0 )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6365 mbedtls_ssl_send_alert_message( ssl,
6366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6367 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6368 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6369 }
6370
6371 /* Now we can safely free the original chain. */
6372 ssl_clear_peer_cert( ssl->session );
6373 }
6374#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6375
6376 /* Parse the next certificate in the chain. */
6377#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6378 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6379#else
6380 /* If we don't need to store the CRT chain permanently, parse
6381 * it in-place from the input buffer instead of making a copy. */
6382 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6383#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6384 switch( ret )
6385 {
6386 case 0: /*ok*/
6387 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6388 /* Ignore certificate with an unknown algorithm: maybe a
6389 prior certificate was already trusted. */
6390 break;
6391
6392 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6393 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6394 goto crt_parse_der_failed;
6395
6396 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6397 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6398 goto crt_parse_der_failed;
6399
6400 default:
6401 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6402 crt_parse_der_failed:
6403 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6404 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6405 return( ret );
6406 }
6407
6408 i += n;
6409 }
6410
6411 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6412 return( 0 );
6413}
6414
6415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006416MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006417static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6418{
6419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6420 return( -1 );
6421
6422 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6423 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6424 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6425 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6426 {
Ronald Cron19385882022-06-15 16:26:13 +02006427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006428 return( 0 );
6429 }
6430 return( -1 );
6431}
6432#endif /* MBEDTLS_SSL_SRV_C */
6433
6434/* Check if a certificate message is expected.
6435 * Return either
6436 * - SSL_CERTIFICATE_EXPECTED, or
6437 * - SSL_CERTIFICATE_SKIP
6438 * indicating whether a Certificate message is expected or not.
6439 */
6440#define SSL_CERTIFICATE_EXPECTED 0
6441#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006443static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6444 int authmode )
6445{
6446 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6447 ssl->handshake->ciphersuite_info;
6448
6449 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6450 return( SSL_CERTIFICATE_SKIP );
6451
6452#if defined(MBEDTLS_SSL_SRV_C)
6453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6454 {
6455 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6456 return( SSL_CERTIFICATE_SKIP );
6457
6458 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6459 {
6460 ssl->session_negotiate->verify_result =
6461 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6462 return( SSL_CERTIFICATE_SKIP );
6463 }
6464 }
6465#else
6466 ((void) authmode);
6467#endif /* MBEDTLS_SSL_SRV_C */
6468
6469 return( SSL_CERTIFICATE_EXPECTED );
6470}
6471
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006472MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006473static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6474 int authmode,
6475 mbedtls_x509_crt *chain,
6476 void *rs_ctx )
6477{
6478 int ret = 0;
6479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6480 ssl->handshake->ciphersuite_info;
6481 int have_ca_chain = 0;
6482
6483 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6484 void *p_vrfy;
6485
6486 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6487 return( 0 );
6488
6489 if( ssl->f_vrfy != NULL )
6490 {
6491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6492 f_vrfy = ssl->f_vrfy;
6493 p_vrfy = ssl->p_vrfy;
6494 }
6495 else
6496 {
6497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6498 f_vrfy = ssl->conf->f_vrfy;
6499 p_vrfy = ssl->conf->p_vrfy;
6500 }
6501
6502 /*
6503 * Main check: verify certificate
6504 */
6505#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6506 if( ssl->conf->f_ca_cb != NULL )
6507 {
6508 ((void) rs_ctx);
6509 have_ca_chain = 1;
6510
6511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6512 ret = mbedtls_x509_crt_verify_with_ca_cb(
6513 chain,
6514 ssl->conf->f_ca_cb,
6515 ssl->conf->p_ca_cb,
6516 ssl->conf->cert_profile,
6517 ssl->hostname,
6518 &ssl->session_negotiate->verify_result,
6519 f_vrfy, p_vrfy );
6520 }
6521 else
6522#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6523 {
6524 mbedtls_x509_crt *ca_chain;
6525 mbedtls_x509_crl *ca_crl;
6526
6527#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6528 if( ssl->handshake->sni_ca_chain != NULL )
6529 {
6530 ca_chain = ssl->handshake->sni_ca_chain;
6531 ca_crl = ssl->handshake->sni_ca_crl;
6532 }
6533 else
6534#endif
6535 {
6536 ca_chain = ssl->conf->ca_chain;
6537 ca_crl = ssl->conf->ca_crl;
6538 }
6539
6540 if( ca_chain != NULL )
6541 have_ca_chain = 1;
6542
6543 ret = mbedtls_x509_crt_verify_restartable(
6544 chain,
6545 ca_chain, ca_crl,
6546 ssl->conf->cert_profile,
6547 ssl->hostname,
6548 &ssl->session_negotiate->verify_result,
6549 f_vrfy, p_vrfy, rs_ctx );
6550 }
6551
6552 if( ret != 0 )
6553 {
6554 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6555 }
6556
6557#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6558 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6559 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6560#endif
6561
6562 /*
6563 * Secondary checks: always done, but change 'ret' only if it was 0
6564 */
6565
6566#if defined(MBEDTLS_ECP_C)
6567 {
6568 const mbedtls_pk_context *pk = &chain->pk;
6569
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006570 /* If certificate uses an EC key, make sure the curve is OK.
6571 * This is a public key, so it can't be opaque, so can_do() is a good
6572 * enough check to ensure pk_ec() is safe to use here. */
Jerry Yud9526692022-02-17 14:23:47 +08006573 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6574 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6575 {
6576 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6577
6578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6579 if( ret == 0 )
6580 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6581 }
6582 }
6583#endif /* MBEDTLS_ECP_C */
6584
6585 if( mbedtls_ssl_check_cert_usage( chain,
6586 ciphersuite_info,
6587 ! ssl->conf->endpoint,
6588 &ssl->session_negotiate->verify_result ) != 0 )
6589 {
6590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6591 if( ret == 0 )
6592 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6593 }
6594
6595 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6596 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6597 * with details encoded in the verification flags. All other kinds
6598 * of error codes, including those from the user provided f_vrfy
6599 * functions, are treated as fatal and lead to a failure of
6600 * ssl_parse_certificate even if verification was optional. */
6601 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6602 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6603 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6604 {
6605 ret = 0;
6606 }
6607
6608 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6609 {
6610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6611 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6612 }
6613
6614 if( ret != 0 )
6615 {
6616 uint8_t alert;
6617
6618 /* The certificate may have been rejected for several reasons.
6619 Pick one and send the corresponding alert. Which alert to send
6620 may be a subject of debate in some cases. */
6621 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6622 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6623 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6624 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6625 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6626 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6627 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6628 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6629 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6630 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6631 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6632 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6633 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6634 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6635 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6636 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6637 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6638 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6639 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6640 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6641 else
6642 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6643 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6644 alert );
6645 }
6646
6647#if defined(MBEDTLS_DEBUG_C)
6648 if( ssl->session_negotiate->verify_result != 0 )
6649 {
6650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6651 (unsigned int) ssl->session_negotiate->verify_result ) );
6652 }
6653 else
6654 {
6655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6656 }
6657#endif /* MBEDTLS_DEBUG_C */
6658
6659 return( ret );
6660}
6661
6662#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006663MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006664static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6665 unsigned char *start, size_t len )
6666{
6667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6668 /* Remember digest of the peer's end-CRT. */
6669 ssl->session_negotiate->peer_cert_digest =
6670 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6671 if( ssl->session_negotiate->peer_cert_digest == NULL )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6674 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6675 mbedtls_ssl_send_alert_message( ssl,
6676 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6677 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6678
6679 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6680 }
6681
6682 ret = mbedtls_md( mbedtls_md_info_from_type(
6683 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6684 start, len,
6685 ssl->session_negotiate->peer_cert_digest );
6686
6687 ssl->session_negotiate->peer_cert_digest_type =
6688 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6689 ssl->session_negotiate->peer_cert_digest_len =
6690 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6691
6692 return( ret );
6693}
6694
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006695MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006696static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6697 unsigned char *start, size_t len )
6698{
6699 unsigned char *end = start + len;
6700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6701
6702 /* Make a copy of the peer's raw public key. */
6703 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6704 ret = mbedtls_pk_parse_subpubkey( &start, end,
6705 &ssl->handshake->peer_pubkey );
6706 if( ret != 0 )
6707 {
6708 /* We should have parsed the public key before. */
6709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6710 }
6711
6712 return( 0 );
6713}
6714#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6715
6716int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6717{
6718 int ret = 0;
6719 int crt_expected;
6720#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6721 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6722 ? ssl->handshake->sni_authmode
6723 : ssl->conf->authmode;
6724#else
6725 const int authmode = ssl->conf->authmode;
6726#endif
6727 void *rs_ctx = NULL;
6728 mbedtls_x509_crt *chain = NULL;
6729
6730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6731
6732 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6733 if( crt_expected == SSL_CERTIFICATE_SKIP )
6734 {
6735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6736 goto exit;
6737 }
6738
6739#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6740 if( ssl->handshake->ecrs_enabled &&
6741 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6742 {
6743 chain = ssl->handshake->ecrs_peer_cert;
6744 ssl->handshake->ecrs_peer_cert = NULL;
6745 goto crt_verify;
6746 }
6747#endif
6748
6749 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6750 {
6751 /* mbedtls_ssl_read_record may have sent an alert already. We
6752 let it decide whether to alert. */
6753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6754 goto exit;
6755 }
6756
6757#if defined(MBEDTLS_SSL_SRV_C)
6758 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6759 {
6760 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6761
6762 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6763 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6764
6765 goto exit;
6766 }
6767#endif /* MBEDTLS_SSL_SRV_C */
6768
6769 /* Clear existing peer CRT structure in case we tried to
6770 * reuse a session but it failed, and allocate a new one. */
6771 ssl_clear_peer_cert( ssl->session_negotiate );
6772
6773 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6774 if( chain == NULL )
6775 {
6776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6777 sizeof( mbedtls_x509_crt ) ) );
6778 mbedtls_ssl_send_alert_message( ssl,
6779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6780 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6781
6782 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6783 goto exit;
6784 }
6785 mbedtls_x509_crt_init( chain );
6786
6787 ret = ssl_parse_certificate_chain( ssl, chain );
6788 if( ret != 0 )
6789 goto exit;
6790
6791#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6792 if( ssl->handshake->ecrs_enabled)
6793 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6794
6795crt_verify:
6796 if( ssl->handshake->ecrs_enabled)
6797 rs_ctx = &ssl->handshake->ecrs_ctx;
6798#endif
6799
6800 ret = ssl_parse_certificate_verify( ssl, authmode,
6801 chain, rs_ctx );
6802 if( ret != 0 )
6803 goto exit;
6804
6805#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6806 {
6807 unsigned char *crt_start, *pk_start;
6808 size_t crt_len, pk_len;
6809
6810 /* We parse the CRT chain without copying, so
6811 * these pointers point into the input buffer,
6812 * and are hence still valid after freeing the
6813 * CRT chain. */
6814
6815 crt_start = chain->raw.p;
6816 crt_len = chain->raw.len;
6817
6818 pk_start = chain->pk_raw.p;
6819 pk_len = chain->pk_raw.len;
6820
6821 /* Free the CRT structures before computing
6822 * digest and copying the peer's public key. */
6823 mbedtls_x509_crt_free( chain );
6824 mbedtls_free( chain );
6825 chain = NULL;
6826
6827 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6828 if( ret != 0 )
6829 goto exit;
6830
6831 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6832 if( ret != 0 )
6833 goto exit;
6834 }
6835#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6836 /* Pass ownership to session structure. */
6837 ssl->session_negotiate->peer_cert = chain;
6838 chain = NULL;
6839#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6840
6841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6842
6843exit:
6844
6845 if( ret == 0 )
6846 ssl->state++;
6847
6848#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6849 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6850 {
6851 ssl->handshake->ecrs_peer_cert = chain;
6852 chain = NULL;
6853 }
6854#endif
6855
6856 if( chain != NULL )
6857 {
6858 mbedtls_x509_crt_free( chain );
6859 mbedtls_free( chain );
6860 }
6861
6862 return( ret );
6863}
6864#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6865
Andrzej Kurek25f27152022-08-17 16:09:31 -04006866#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006867static void ssl_calc_finished_tls_sha256(
6868 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6869{
6870 int len = 12;
6871 const char *sender;
6872 unsigned char padbuf[32];
6873#if defined(MBEDTLS_USE_PSA_CRYPTO)
6874 size_t hash_size;
6875 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6876 psa_status_t status;
6877#else
6878 mbedtls_sha256_context sha256;
6879#endif
6880
6881 mbedtls_ssl_session *session = ssl->session_negotiate;
6882 if( !session )
6883 session = ssl->session;
6884
6885 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6886 ? "client finished"
6887 : "server finished";
6888
6889#if defined(MBEDTLS_USE_PSA_CRYPTO)
6890 sha256_psa = psa_hash_operation_init();
6891
6892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6893
6894 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6895 if( status != PSA_SUCCESS )
6896 {
6897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6898 return;
6899 }
6900
6901 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6902 if( status != PSA_SUCCESS )
6903 {
6904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6905 return;
6906 }
6907 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6908#else
6909
6910 mbedtls_sha256_init( &sha256 );
6911
6912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6913
6914 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6915
6916 /*
6917 * TLSv1.2:
6918 * hash = PRF( master, finished_label,
6919 * Hash( handshake ) )[0.11]
6920 */
6921
6922#if !defined(MBEDTLS_SHA256_ALT)
6923 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6924 sha256.state, sizeof( sha256.state ) );
6925#endif
6926
6927 mbedtls_sha256_finish( &sha256, padbuf );
6928 mbedtls_sha256_free( &sha256 );
6929#endif /* MBEDTLS_USE_PSA_CRYPTO */
6930
6931 ssl->handshake->tls_prf( session->master, 48, sender,
6932 padbuf, 32, buf, len );
6933
6934 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6935
6936 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6937
6938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6939}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006940#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA */
Jerry Yu615bd6f2022-02-17 14:25:15 +08006941
Jerry Yub7ba49e2022-02-17 14:25:53 +08006942
Andrzej Kurek25f27152022-08-17 16:09:31 -04006943#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006944static void ssl_calc_finished_tls_sha384(
6945 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6946{
6947 int len = 12;
6948 const char *sender;
6949 unsigned char padbuf[48];
6950#if defined(MBEDTLS_USE_PSA_CRYPTO)
6951 size_t hash_size;
6952 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
6953 psa_status_t status;
6954#else
6955 mbedtls_sha512_context sha512;
6956#endif
6957
6958 mbedtls_ssl_session *session = ssl->session_negotiate;
6959 if( !session )
6960 session = ssl->session;
6961
6962 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6963 ? "client finished"
6964 : "server finished";
6965
6966#if defined(MBEDTLS_USE_PSA_CRYPTO)
6967 sha384_psa = psa_hash_operation_init();
6968
6969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6970
6971 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6972 if( status != PSA_SUCCESS )
6973 {
6974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6975 return;
6976 }
6977
6978 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
6979 if( status != PSA_SUCCESS )
6980 {
6981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6982 return;
6983 }
6984 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6985#else
6986 mbedtls_sha512_init( &sha512 );
6987
6988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
6989
Andrzej Kureka242e832022-08-11 10:03:14 -04006990 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08006991
6992 /*
6993 * TLSv1.2:
6994 * hash = PRF( master, finished_label,
6995 * Hash( handshake ) )[0.11]
6996 */
6997
6998#if !defined(MBEDTLS_SHA512_ALT)
6999 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7000 sha512.state, sizeof( sha512.state ) );
7001#endif
7002 mbedtls_sha512_finish( &sha512, padbuf );
7003
7004 mbedtls_sha512_free( &sha512 );
7005#endif
7006
7007 ssl->handshake->tls_prf( session->master, 48, sender,
7008 padbuf, 48, buf, len );
7009
7010 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7011
7012 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7013
7014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7015}
Andrzej Kurek25f27152022-08-17 16:09:31 -04007016#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007017
Jerry Yuaef00152022-02-17 14:27:31 +08007018void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7019{
7020 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7021
7022 /*
7023 * Free our handshake params
7024 */
7025 mbedtls_ssl_handshake_free( ssl );
7026 mbedtls_free( ssl->handshake );
7027 ssl->handshake = NULL;
7028
7029 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007030 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007031 */
7032 if( ssl->transform )
7033 {
7034 mbedtls_ssl_transform_free( ssl->transform );
7035 mbedtls_free( ssl->transform );
7036 }
7037 ssl->transform = ssl->transform_negotiate;
7038 ssl->transform_negotiate = NULL;
7039
7040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7041}
7042
Jerry Yu2a9fff52022-02-17 14:28:51 +08007043void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7044{
7045 int resume = ssl->handshake->resume;
7046
7047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7048
7049#if defined(MBEDTLS_SSL_RENEGOTIATION)
7050 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7051 {
7052 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7053 ssl->renego_records_seen = 0;
7054 }
7055#endif
7056
7057 /*
7058 * Free the previous session and switch in the current one
7059 */
7060 if( ssl->session )
7061 {
7062#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7063 /* RFC 7366 3.1: keep the EtM state */
7064 ssl->session_negotiate->encrypt_then_mac =
7065 ssl->session->encrypt_then_mac;
7066#endif
7067
7068 mbedtls_ssl_session_free( ssl->session );
7069 mbedtls_free( ssl->session );
7070 }
7071 ssl->session = ssl->session_negotiate;
7072 ssl->session_negotiate = NULL;
7073
7074 /*
7075 * Add cache entry
7076 */
7077 if( ssl->conf->f_set_cache != NULL &&
7078 ssl->session->id_len != 0 &&
7079 resume == 0 )
7080 {
7081 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7082 ssl->session->id,
7083 ssl->session->id_len,
7084 ssl->session ) != 0 )
7085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7086 }
7087
7088#if defined(MBEDTLS_SSL_PROTO_DTLS)
7089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7090 ssl->handshake->flight != NULL )
7091 {
7092 /* Cancel handshake timer */
7093 mbedtls_ssl_set_timer( ssl, 0 );
7094
7095 /* Keep last flight around in case we need to resend it:
7096 * we need the handshake and transform structures for that */
7097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7098 }
7099 else
7100#endif
7101 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7102
7103 ssl->state++;
7104
7105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7106}
7107
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007108int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7109{
7110 int ret, hash_len;
7111
7112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7113
7114 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7115
7116 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7117
7118 /*
7119 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7120 * may define some other value. Currently (early 2016), no defined
7121 * ciphersuite does this (and this is unlikely to change as activity has
7122 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7123 */
7124 hash_len = 12;
7125
7126#if defined(MBEDTLS_SSL_RENEGOTIATION)
7127 ssl->verify_data_len = hash_len;
7128 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7129#endif
7130
7131 ssl->out_msglen = 4 + hash_len;
7132 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7133 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7134
7135 /*
7136 * In case of session resuming, invert the client and server
7137 * ChangeCipherSpec messages order.
7138 */
7139 if( ssl->handshake->resume != 0 )
7140 {
7141#if defined(MBEDTLS_SSL_CLI_C)
7142 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7143 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7144#endif
7145#if defined(MBEDTLS_SSL_SRV_C)
7146 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7147 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7148#endif
7149 }
7150 else
7151 ssl->state++;
7152
7153 /*
7154 * Switch to our negotiated transform and session parameters for outbound
7155 * data.
7156 */
7157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7158
7159#if defined(MBEDTLS_SSL_PROTO_DTLS)
7160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7161 {
7162 unsigned char i;
7163
7164 /* Remember current epoch settings for resending */
7165 ssl->handshake->alt_transform_out = ssl->transform_out;
7166 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7167 sizeof( ssl->handshake->alt_out_ctr ) );
7168
7169 /* Set sequence_number to zero */
7170 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7171
7172
7173 /* Increment epoch */
7174 for( i = 2; i > 0; i-- )
7175 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7176 break;
7177
7178 /* The loop goes to its end iff the counter is wrapping */
7179 if( i == 0 )
7180 {
7181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7182 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7183 }
7184 }
7185 else
7186#endif /* MBEDTLS_SSL_PROTO_DTLS */
7187 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7188
7189 ssl->transform_out = ssl->transform_negotiate;
7190 ssl->session_out = ssl->session_negotiate;
7191
7192#if defined(MBEDTLS_SSL_PROTO_DTLS)
7193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7194 mbedtls_ssl_send_flight_completed( ssl );
7195#endif
7196
7197 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7198 {
7199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7200 return( ret );
7201 }
7202
7203#if defined(MBEDTLS_SSL_PROTO_DTLS)
7204 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7205 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7206 {
7207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7208 return( ret );
7209 }
7210#endif
7211
7212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7213
7214 return( 0 );
7215}
7216
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007217#define SSL_MAX_HASH_LEN 12
7218
7219int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7220{
7221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7222 unsigned int hash_len = 12;
7223 unsigned char buf[SSL_MAX_HASH_LEN];
7224
7225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7226
7227 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7228
7229 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7230 {
7231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7232 goto exit;
7233 }
7234
7235 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7236 {
7237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7238 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7239 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7240 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7241 goto exit;
7242 }
7243
7244 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7245 {
7246 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7247 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7248 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7249 goto exit;
7250 }
7251
7252 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7253 {
7254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7255 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7256 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7257 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7258 goto exit;
7259 }
7260
7261 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7262 buf, hash_len ) != 0 )
7263 {
7264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7266 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7267 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7268 goto exit;
7269 }
7270
7271#if defined(MBEDTLS_SSL_RENEGOTIATION)
7272 ssl->verify_data_len = hash_len;
7273 memcpy( ssl->peer_verify_data, buf, hash_len );
7274#endif
7275
7276 if( ssl->handshake->resume != 0 )
7277 {
7278#if defined(MBEDTLS_SSL_CLI_C)
7279 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7280 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7281#endif
7282#if defined(MBEDTLS_SSL_SRV_C)
7283 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7284 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7285#endif
7286 }
7287 else
7288 ssl->state++;
7289
7290#if defined(MBEDTLS_SSL_PROTO_DTLS)
7291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7292 mbedtls_ssl_recv_flight_completed( ssl );
7293#endif
7294
7295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7296
7297exit:
7298 mbedtls_platform_zeroize( buf, hash_len );
7299 return( ret );
7300}
7301
Jerry Yu392112c2022-02-17 14:34:10 +08007302#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7303/*
7304 * Helper to get TLS 1.2 PRF from ciphersuite
7305 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7306 */
7307static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7308{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007309#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007310 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7311 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7312
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007313 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007314 return( tls_prf_sha384 );
7315#else
7316 (void) ciphersuite_id;
7317#endif
7318 return( tls_prf_sha256 );
7319}
7320#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007321
7322static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7323{
7324 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007325#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007326 if( tls_prf == tls_prf_sha384 )
7327 {
7328 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7329 }
7330 else
7331#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007332#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007333 if( tls_prf == tls_prf_sha256 )
7334 {
7335 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7336 }
7337 else
7338#endif
7339 return( MBEDTLS_SSL_TLS_PRF_NONE );
7340}
7341
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007342/*
7343 * Populate a transform structure with session keys and all the other
7344 * necessary information.
7345 *
7346 * Parameters:
7347 * - [in/out]: transform: structure to populate
7348 * [in] must be just initialised with mbedtls_ssl_transform_init()
7349 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7350 * - [in] ciphersuite
7351 * - [in] master
7352 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007353 * - [in] tls_prf: pointer to PRF to use for key derivation
7354 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007355 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007356 * - [in] endpoint: client or server
7357 * - [in] ssl: used for:
7358 * - ssl->conf->{f,p}_export_keys
7359 * [in] optionally used for:
7360 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7361 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007362MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007363static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7364 int ciphersuite,
7365 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007366#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007367 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007368#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007369 ssl_tls_prf_t tls_prf,
7370 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007371 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007372 unsigned endpoint,
7373 const mbedtls_ssl_context *ssl )
7374{
7375 int ret = 0;
7376 unsigned char keyblk[256];
7377 unsigned char *key1;
7378 unsigned char *key2;
7379 unsigned char *mac_enc;
7380 unsigned char *mac_dec;
7381 size_t mac_key_len = 0;
7382 size_t iv_copy_len;
7383 size_t keylen;
7384 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007385 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007386#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007387 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007388 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007389#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007390
7391#if defined(MBEDTLS_USE_PSA_CRYPTO)
7392 psa_key_type_t key_type;
7393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7394 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007395 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007396 size_t key_bits;
7397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7398#endif
7399
7400#if !defined(MBEDTLS_DEBUG_C) && \
7401 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7402 if( ssl->f_export_keys == NULL )
7403 {
7404 ssl = NULL; /* make sure we don't use it except for these cases */
7405 (void) ssl;
7406 }
7407#endif
7408
7409 /*
7410 * Some data just needs copying into the structure
7411 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007413 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007414#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007415 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007416
7417#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7418 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7419#endif
7420
7421#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007422 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007423 {
7424 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7425 * generation separate. This should never happen. */
7426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7427 }
7428#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7429
7430 /*
7431 * Get various info structures
7432 */
7433 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7434 if( ciphersuite_info == NULL )
7435 {
7436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7437 ciphersuite ) );
7438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7439 }
7440
Neil Armstrongab555e02022-04-04 11:07:59 +02007441 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007442#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007443 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007444#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007445 ciphersuite_info );
7446
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007447 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7448 transform->taglen =
7449 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7450
7451#if defined(MBEDTLS_USE_PSA_CRYPTO)
7452 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7453 transform->taglen,
7454 &alg,
7455 &key_type,
7456 &key_bits ) ) != PSA_SUCCESS )
7457 {
7458 ret = psa_ssl_status_to_mbedtls( status );
7459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7460 goto end;
7461 }
7462#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007463 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7464 if( cipher_info == NULL )
7465 {
7466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7467 ciphersuite_info->cipher ) );
7468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7469 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007470#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007471
Neil Armstronge4512952022-03-08 09:08:22 +01007472#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007473 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007474 if( mac_alg == 0 )
7475 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007477 (unsigned) ciphersuite_info->mac ) );
7478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7479 }
7480#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007481 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7482 if( md_info == NULL )
7483 {
7484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7485 (unsigned) ciphersuite_info->mac ) );
7486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7487 }
Neil Armstronge4512952022-03-08 09:08:22 +01007488#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007489
7490#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7491 /* Copy own and peer's CID if the use of the CID
7492 * extension has been negotiated. */
7493 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7494 {
7495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7496
7497 transform->in_cid_len = ssl->own_cid_len;
7498 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7499 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7500 transform->in_cid_len );
7501
7502 transform->out_cid_len = ssl->handshake->peer_cid_len;
7503 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7504 ssl->handshake->peer_cid_len );
7505 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7506 transform->out_cid_len );
7507 }
7508#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7509
7510 /*
7511 * Compute key block using the PRF
7512 */
7513 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7514 if( ret != 0 )
7515 {
7516 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7517 return( ret );
7518 }
7519
7520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7521 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7522 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7523 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7524 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7525
7526 /*
7527 * Determine the appropriate key, IV and MAC length.
7528 */
7529
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007530#if defined(MBEDTLS_USE_PSA_CRYPTO)
7531 keylen = PSA_BITS_TO_BYTES(key_bits);
7532#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007533 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007534#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007535
7536#if defined(MBEDTLS_GCM_C) || \
7537 defined(MBEDTLS_CCM_C) || \
7538 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007539 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007540 {
7541 size_t explicit_ivlen;
7542
7543 transform->maclen = 0;
7544 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007545
7546 /* All modes haves 96-bit IVs, but the length of the static parts vary
7547 * with mode and version:
7548 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7549 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7550 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7551 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7552 * sequence number).
7553 */
7554 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007555#if defined(MBEDTLS_USE_PSA_CRYPTO)
7556 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7557#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007558 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007559#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007560 transform->fixed_ivlen = 12;
7561 else
7562 transform->fixed_ivlen = 4;
7563
7564 /* Minimum length of encrypted record */
7565 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7566 transform->minlen = explicit_ivlen + transform->taglen;
7567 }
7568 else
7569#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7570#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007571 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7572 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7573 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007574 {
Neil Armstronge4512952022-03-08 09:08:22 +01007575#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007576 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007577#else
7578 size_t block_size = cipher_info->block_size;
7579#endif /* MBEDTLS_USE_PSA_CRYPTO */
7580
7581#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007582 /* Get MAC length */
7583 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7584#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007585 /* Initialize HMAC contexts */
7586 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7587 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7588 {
7589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7590 goto end;
7591 }
7592
7593 /* Get MAC length */
7594 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007595#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007596 transform->maclen = mac_key_len;
7597
7598 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007599#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007600 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007601#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007602 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007603#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007604
7605 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007606 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007607 transform->minlen = transform->maclen;
7608 else
7609 {
7610 /*
7611 * GenericBlockCipher:
7612 * 1. if EtM is in use: one block plus MAC
7613 * otherwise: * first multiple of blocklen greater than maclen
7614 * 2. IV
7615 */
7616#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007617 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007618 {
7619 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007620 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007621 }
7622 else
7623#endif
7624 {
7625 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007626 + block_size
7627 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007628 }
7629
Glenn Strauss07c64162022-03-14 12:34:51 -04007630 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007631 {
7632 transform->minlen += transform->ivlen;
7633 }
7634 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007635 {
7636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7637 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7638 goto end;
7639 }
7640 }
7641 }
7642 else
7643#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7644 {
7645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7647 }
7648
7649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7650 (unsigned) keylen,
7651 (unsigned) transform->minlen,
7652 (unsigned) transform->ivlen,
7653 (unsigned) transform->maclen ) );
7654
7655 /*
7656 * Finally setup the cipher contexts, IVs and MAC secrets.
7657 */
7658#if defined(MBEDTLS_SSL_CLI_C)
7659 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7660 {
7661 key1 = keyblk + mac_key_len * 2;
7662 key2 = keyblk + mac_key_len * 2 + keylen;
7663
7664 mac_enc = keyblk;
7665 mac_dec = keyblk + mac_key_len;
7666
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667 iv_copy_len = ( transform->fixed_ivlen ) ?
7668 transform->fixed_ivlen : transform->ivlen;
7669 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7670 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7671 iv_copy_len );
7672 }
7673 else
7674#endif /* MBEDTLS_SSL_CLI_C */
7675#if defined(MBEDTLS_SSL_SRV_C)
7676 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7677 {
7678 key1 = keyblk + mac_key_len * 2 + keylen;
7679 key2 = keyblk + mac_key_len * 2;
7680
7681 mac_enc = keyblk + mac_key_len;
7682 mac_dec = keyblk;
7683
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007684 iv_copy_len = ( transform->fixed_ivlen ) ?
7685 transform->fixed_ivlen : transform->ivlen;
7686 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7687 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7688 iv_copy_len );
7689 }
7690 else
7691#endif /* MBEDTLS_SSL_SRV_C */
7692 {
7693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7694 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7695 goto end;
7696 }
7697
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007698 if( ssl != NULL && ssl->f_export_keys != NULL )
7699 {
7700 ssl->f_export_keys( ssl->p_export_keys,
7701 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7702 master, 48,
7703 randbytes + 32,
7704 randbytes,
7705 tls_prf_get_type( tls_prf ) );
7706 }
7707
7708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007709 transform->psa_alg = alg;
7710
7711 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7712 {
7713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7714 psa_set_key_algorithm( &attributes, alg );
7715 psa_set_key_type( &attributes, key_type );
7716
7717 if( ( status = psa_import_key( &attributes,
7718 key1,
7719 PSA_BITS_TO_BYTES( key_bits ),
7720 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7721 {
7722 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7723 ret = psa_ssl_status_to_mbedtls( status );
7724 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7725 goto end;
7726 }
7727
7728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7729
7730 if( ( status = psa_import_key( &attributes,
7731 key2,
7732 PSA_BITS_TO_BYTES( key_bits ),
7733 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7734 {
7735 ret = psa_ssl_status_to_mbedtls( status );
7736 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7737 goto end;
7738 }
7739 }
7740#else
7741 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7742 cipher_info ) ) != 0 )
7743 {
7744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7745 goto end;
7746 }
7747
7748 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7749 cipher_info ) ) != 0 )
7750 {
7751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7752 goto end;
7753 }
7754
7755 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7756 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7757 MBEDTLS_ENCRYPT ) ) != 0 )
7758 {
7759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7760 goto end;
7761 }
7762
7763 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7764 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7765 MBEDTLS_DECRYPT ) ) != 0 )
7766 {
7767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7768 goto end;
7769 }
7770
7771#if defined(MBEDTLS_CIPHER_MODE_CBC)
7772 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7773 {
7774 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7775 MBEDTLS_PADDING_NONE ) ) != 0 )
7776 {
7777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7778 goto end;
7779 }
7780
7781 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7782 MBEDTLS_PADDING_NONE ) ) != 0 )
7783 {
7784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7785 goto end;
7786 }
7787 }
7788#endif /* MBEDTLS_CIPHER_MODE_CBC */
7789#endif /* MBEDTLS_USE_PSA_CRYPTO */
7790
Neil Armstrong29c0c042022-03-17 17:47:28 +01007791#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7792 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7793 For AEAD-based ciphersuites, there is nothing to do here. */
7794 if( mac_key_len != 0 )
7795 {
7796#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007797 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007798
7799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007800 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007801 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7802
7803 if( ( status = psa_import_key( &attributes,
7804 mac_enc, mac_key_len,
7805 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7806 {
7807 ret = psa_ssl_status_to_mbedtls( status );
7808 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7809 goto end;
7810 }
7811
Ronald Cronfb39f152022-03-25 14:36:28 +01007812 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007813 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7814#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7815 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7816#endif
7817 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007818 /* mbedtls_ct_hmac() requires the key to be exportable */
7819 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7820 PSA_KEY_USAGE_VERIFY_HASH );
7821 else
7822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7823
7824 if( ( status = psa_import_key( &attributes,
7825 mac_dec, mac_key_len,
7826 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7827 {
7828 ret = psa_ssl_status_to_mbedtls( status );
7829 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7830 goto end;
7831 }
7832#else
7833 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7834 if( ret != 0 )
7835 goto end;
7836 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7837 if( ret != 0 )
7838 goto end;
7839#endif /* MBEDTLS_USE_PSA_CRYPTO */
7840 }
7841#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7842
7843 ((void) mac_dec);
7844 ((void) mac_enc);
7845
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007846end:
7847 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7848 return( ret );
7849}
7850
Jerry Yuee40f9d2022-02-17 14:55:16 +08007851#if defined(MBEDTLS_USE_PSA_CRYPTO)
7852int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7853 unsigned char *hash, size_t *hashlen,
7854 unsigned char *data, size_t data_len,
7855 mbedtls_md_type_t md_alg )
7856{
7857 psa_status_t status;
7858 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007859 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007860
7861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7862
7863 if( ( status = psa_hash_setup( &hash_operation,
7864 hash_alg ) ) != PSA_SUCCESS )
7865 {
7866 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7867 goto exit;
7868 }
7869
7870 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7871 64 ) ) != PSA_SUCCESS )
7872 {
7873 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7874 goto exit;
7875 }
7876
7877 if( ( status = psa_hash_update( &hash_operation,
7878 data, data_len ) ) != PSA_SUCCESS )
7879 {
7880 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7881 goto exit;
7882 }
7883
7884 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7885 hashlen ) ) != PSA_SUCCESS )
7886 {
7887 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7888 goto exit;
7889 }
7890
7891exit:
7892 if( status != PSA_SUCCESS )
7893 {
7894 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7895 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7896 switch( status )
7897 {
7898 case PSA_ERROR_NOT_SUPPORTED:
7899 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7900 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7901 case PSA_ERROR_BUFFER_TOO_SMALL:
7902 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7903 case PSA_ERROR_INSUFFICIENT_MEMORY:
7904 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7905 default:
7906 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7907 }
7908 }
7909 return( 0 );
7910}
7911
7912#else
7913
7914int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7915 unsigned char *hash, size_t *hashlen,
7916 unsigned char *data, size_t data_len,
7917 mbedtls_md_type_t md_alg )
7918{
7919 int ret = 0;
7920 mbedtls_md_context_t ctx;
7921 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7922 *hashlen = mbedtls_md_get_size( md_info );
7923
7924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7925
7926 mbedtls_md_init( &ctx );
7927
7928 /*
7929 * digitally-signed struct {
7930 * opaque client_random[32];
7931 * opaque server_random[32];
7932 * ServerDHParams params;
7933 * };
7934 */
7935 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7936 {
7937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7938 goto exit;
7939 }
7940 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7941 {
7942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7943 goto exit;
7944 }
7945 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7946 {
7947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7948 goto exit;
7949 }
7950 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
7951 {
7952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
7953 goto exit;
7954 }
7955 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
7956 {
7957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
7958 goto exit;
7959 }
7960
7961exit:
7962 mbedtls_md_free( &ctx );
7963
7964 if( ret != 0 )
7965 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7966 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7967
7968 return( ret );
7969}
7970#endif /* MBEDTLS_USE_PSA_CRYPTO */
7971
Jerry Yud9d91da2022-02-17 14:57:06 +08007972#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7973
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007974/* Find the preferred hash for a given signature algorithm. */
7975unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
7976 mbedtls_ssl_context *ssl,
7977 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08007978{
Gabor Mezei078e8032022-04-27 21:17:56 +02007979 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007980 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02007981
7982 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007983 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02007984
Gabor Mezeia3d016c2022-05-10 12:44:09 +02007985 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08007986 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02007987 unsigned int hash_alg_received =
7988 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
7989 received_sig_algs[i] );
7990 unsigned int sig_alg_received =
7991 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
7992 received_sig_algs[i] );
7993
7994 if( sig_alg == sig_alg_received )
7995 {
7996#if defined(MBEDTLS_USE_PSA_CRYPTO)
7997 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
7998 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02007999 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008000 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008001
Neil Armstrong96eceb82022-06-30 18:05:05 +02008002 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8003 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8004 PSA_ALG_ECDSA( psa_hash_alg ),
8005 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008006 continue;
8007
Neil Armstrong96eceb82022-06-30 18:05:05 +02008008 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008009 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8010 PSA_ALG_RSA_PKCS1V15_SIGN(
8011 psa_hash_alg ),
8012 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008013 continue;
8014 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008015#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008016
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008017 return( hash_alg_received );
8018 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008019 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008020
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008021 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008022}
8023
8024#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8025
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008026/* Serialization of TLS 1.2 sessions:
8027 *
8028 * struct {
8029 * uint64 start_time;
8030 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008031 * uint8 session_id_len; // at most 32
8032 * opaque session_id[32];
8033 * opaque master[48]; // fixed length in the standard
8034 * uint32 verify_result;
8035 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8036 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8037 * uint32 ticket_lifetime;
8038 * uint8 mfl_code; // up to 255 according to standard
8039 * uint8 encrypt_then_mac; // 0 or 1
8040 * } serialized_session_tls12;
8041 *
8042 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008043static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008044 unsigned char *buf,
8045 size_t buf_len )
8046{
8047 unsigned char *p = buf;
8048 size_t used = 0;
8049
8050#if defined(MBEDTLS_HAVE_TIME)
8051 uint64_t start;
8052#endif
8053#if defined(MBEDTLS_X509_CRT_PARSE_C)
8054#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8055 size_t cert_len;
8056#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8057#endif /* MBEDTLS_X509_CRT_PARSE_C */
8058
8059 /*
8060 * Time
8061 */
8062#if defined(MBEDTLS_HAVE_TIME)
8063 used += 8;
8064
8065 if( used <= buf_len )
8066 {
8067 start = (uint64_t) session->start;
8068
8069 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8070 p += 8;
8071 }
8072#endif /* MBEDTLS_HAVE_TIME */
8073
8074 /*
8075 * Basic mandatory fields
8076 */
8077 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008078 + 1 /* id_len */
8079 + sizeof( session->id )
8080 + sizeof( session->master )
8081 + 4; /* verify_result */
8082
8083 if( used <= buf_len )
8084 {
8085 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8086 p += 2;
8087
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008088 *p++ = MBEDTLS_BYTE_0( session->id_len );
8089 memcpy( p, session->id, 32 );
8090 p += 32;
8091
8092 memcpy( p, session->master, 48 );
8093 p += 48;
8094
8095 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8096 p += 4;
8097 }
8098
8099 /*
8100 * Peer's end-entity certificate
8101 */
8102#if defined(MBEDTLS_X509_CRT_PARSE_C)
8103#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8104 if( session->peer_cert == NULL )
8105 cert_len = 0;
8106 else
8107 cert_len = session->peer_cert->raw.len;
8108
8109 used += 3 + cert_len;
8110
8111 if( used <= buf_len )
8112 {
8113 *p++ = MBEDTLS_BYTE_2( cert_len );
8114 *p++ = MBEDTLS_BYTE_1( cert_len );
8115 *p++ = MBEDTLS_BYTE_0( cert_len );
8116
8117 if( session->peer_cert != NULL )
8118 {
8119 memcpy( p, session->peer_cert->raw.p, cert_len );
8120 p += cert_len;
8121 }
8122 }
8123#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8124 if( session->peer_cert_digest != NULL )
8125 {
8126 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8127 if( used <= buf_len )
8128 {
8129 *p++ = (unsigned char) session->peer_cert_digest_type;
8130 *p++ = (unsigned char) session->peer_cert_digest_len;
8131 memcpy( p, session->peer_cert_digest,
8132 session->peer_cert_digest_len );
8133 p += session->peer_cert_digest_len;
8134 }
8135 }
8136 else
8137 {
8138 used += 2;
8139 if( used <= buf_len )
8140 {
8141 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8142 *p++ = 0;
8143 }
8144 }
8145#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8146#endif /* MBEDTLS_X509_CRT_PARSE_C */
8147
8148 /*
8149 * Session ticket if any, plus associated data
8150 */
8151#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8152 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8153
8154 if( used <= buf_len )
8155 {
8156 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8157 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8158 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8159
8160 if( session->ticket != NULL )
8161 {
8162 memcpy( p, session->ticket, session->ticket_len );
8163 p += session->ticket_len;
8164 }
8165
8166 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8167 p += 4;
8168 }
8169#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8170
8171 /*
8172 * Misc extension-related info
8173 */
8174#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8175 used += 1;
8176
8177 if( used <= buf_len )
8178 *p++ = session->mfl_code;
8179#endif
8180
8181#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8182 used += 1;
8183
8184 if( used <= buf_len )
8185 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8186#endif
8187
8188 return( used );
8189}
8190
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008191MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008192static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008193 const unsigned char *buf,
8194 size_t len )
8195{
8196#if defined(MBEDTLS_HAVE_TIME)
8197 uint64_t start;
8198#endif
8199#if defined(MBEDTLS_X509_CRT_PARSE_C)
8200#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8201 size_t cert_len;
8202#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8203#endif /* MBEDTLS_X509_CRT_PARSE_C */
8204
8205 const unsigned char *p = buf;
8206 const unsigned char * const end = buf + len;
8207
8208 /*
8209 * Time
8210 */
8211#if defined(MBEDTLS_HAVE_TIME)
8212 if( 8 > (size_t)( end - p ) )
8213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8214
8215 start = ( (uint64_t) p[0] << 56 ) |
8216 ( (uint64_t) p[1] << 48 ) |
8217 ( (uint64_t) p[2] << 40 ) |
8218 ( (uint64_t) p[3] << 32 ) |
8219 ( (uint64_t) p[4] << 24 ) |
8220 ( (uint64_t) p[5] << 16 ) |
8221 ( (uint64_t) p[6] << 8 ) |
8222 ( (uint64_t) p[7] );
8223 p += 8;
8224
8225 session->start = (time_t) start;
8226#endif /* MBEDTLS_HAVE_TIME */
8227
8228 /*
8229 * Basic mandatory fields
8230 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008231 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8233
8234 session->ciphersuite = ( p[0] << 8 ) | p[1];
8235 p += 2;
8236
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008237 session->id_len = *p++;
8238 memcpy( session->id, p, 32 );
8239 p += 32;
8240
8241 memcpy( session->master, p, 48 );
8242 p += 48;
8243
8244 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8245 ( (uint32_t) p[1] << 16 ) |
8246 ( (uint32_t) p[2] << 8 ) |
8247 ( (uint32_t) p[3] );
8248 p += 4;
8249
8250 /* Immediately clear invalid pointer values that have been read, in case
8251 * we exit early before we replaced them with valid ones. */
8252#if defined(MBEDTLS_X509_CRT_PARSE_C)
8253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8254 session->peer_cert = NULL;
8255#else
8256 session->peer_cert_digest = NULL;
8257#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8258#endif /* MBEDTLS_X509_CRT_PARSE_C */
8259#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8260 session->ticket = NULL;
8261#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8262
8263 /*
8264 * Peer certificate
8265 */
8266#if defined(MBEDTLS_X509_CRT_PARSE_C)
8267#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8268 /* Deserialize CRT from the end of the ticket. */
8269 if( 3 > (size_t)( end - p ) )
8270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8271
8272 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8273 p += 3;
8274
8275 if( cert_len != 0 )
8276 {
8277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8278
8279 if( cert_len > (size_t)( end - p ) )
8280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8281
8282 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8283
8284 if( session->peer_cert == NULL )
8285 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8286
8287 mbedtls_x509_crt_init( session->peer_cert );
8288
8289 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8290 p, cert_len ) ) != 0 )
8291 {
8292 mbedtls_x509_crt_free( session->peer_cert );
8293 mbedtls_free( session->peer_cert );
8294 session->peer_cert = NULL;
8295 return( ret );
8296 }
8297
8298 p += cert_len;
8299 }
8300#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8301 /* Deserialize CRT digest from the end of the ticket. */
8302 if( 2 > (size_t)( end - p ) )
8303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8304
8305 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8306 session->peer_cert_digest_len = (size_t) *p++;
8307
8308 if( session->peer_cert_digest_len != 0 )
8309 {
8310 const mbedtls_md_info_t *md_info =
8311 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8312 if( md_info == NULL )
8313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8314 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8316
8317 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8319
8320 session->peer_cert_digest =
8321 mbedtls_calloc( 1, session->peer_cert_digest_len );
8322 if( session->peer_cert_digest == NULL )
8323 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8324
8325 memcpy( session->peer_cert_digest, p,
8326 session->peer_cert_digest_len );
8327 p += session->peer_cert_digest_len;
8328 }
8329#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8330#endif /* MBEDTLS_X509_CRT_PARSE_C */
8331
8332 /*
8333 * Session ticket and associated data
8334 */
8335#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8336 if( 3 > (size_t)( end - p ) )
8337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8338
8339 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8340 p += 3;
8341
8342 if( session->ticket_len != 0 )
8343 {
8344 if( session->ticket_len > (size_t)( end - p ) )
8345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8346
8347 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8348 if( session->ticket == NULL )
8349 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8350
8351 memcpy( session->ticket, p, session->ticket_len );
8352 p += session->ticket_len;
8353 }
8354
8355 if( 4 > (size_t)( end - p ) )
8356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8357
8358 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8359 ( (uint32_t) p[1] << 16 ) |
8360 ( (uint32_t) p[2] << 8 ) |
8361 ( (uint32_t) p[3] );
8362 p += 4;
8363#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8364
8365 /*
8366 * Misc extension-related info
8367 */
8368#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8369 if( 1 > (size_t)( end - p ) )
8370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8371
8372 session->mfl_code = *p++;
8373#endif
8374
8375#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8376 if( 1 > (size_t)( end - p ) )
8377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8378
8379 session->encrypt_then_mac = *p++;
8380#endif
8381
8382 /* Done, should have consumed entire buffer */
8383 if( p != end )
8384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8385
8386 return( 0 );
8387}
Jerry Yudc7bd172022-02-17 13:44:15 +08008388#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8389
XiaokangQian75d40ef2022-04-20 11:05:24 +00008390int mbedtls_ssl_validate_ciphersuite(
8391 const mbedtls_ssl_context *ssl,
8392 const mbedtls_ssl_ciphersuite_t *suite_info,
8393 mbedtls_ssl_protocol_version min_tls_version,
8394 mbedtls_ssl_protocol_version max_tls_version )
8395{
8396 (void) ssl;
8397
8398 if( suite_info == NULL )
8399 return( -1 );
8400
8401 if( ( suite_info->min_tls_version > max_tls_version ) ||
8402 ( suite_info->max_tls_version < min_tls_version ) )
8403 {
8404 return( -1 );
8405 }
8406
XiaokangQian060d8672022-04-21 09:24:56 +00008407#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008408#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8409 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8410 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8411 {
8412 return( -1 );
8413 }
8414#endif
8415
8416 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8417#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8418 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8419 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8420 {
8421 return( -1 );
8422 }
8423#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8424#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8425
8426 return( 0 );
8427}
8428
XiaokangQianeaf36512022-04-24 09:07:44 +00008429#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8430/*
8431 * Function for writing a signature algorithm extension.
8432 *
8433 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8434 * value (TLS 1.3 RFC8446):
8435 * enum {
8436 * ....
8437 * ecdsa_secp256r1_sha256( 0x0403 ),
8438 * ecdsa_secp384r1_sha384( 0x0503 ),
8439 * ecdsa_secp521r1_sha512( 0x0603 ),
8440 * ....
8441 * } SignatureScheme;
8442 *
8443 * struct {
8444 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8445 * } SignatureSchemeList;
8446 *
8447 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8448 * value (TLS 1.2 RFC5246):
8449 * enum {
8450 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8451 * sha512(6), (255)
8452 * } HashAlgorithm;
8453 *
8454 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8455 * SignatureAlgorithm;
8456 *
8457 * struct {
8458 * HashAlgorithm hash;
8459 * SignatureAlgorithm signature;
8460 * } SignatureAndHashAlgorithm;
8461 *
8462 * SignatureAndHashAlgorithm
8463 * supported_signature_algorithms<2..2^16-2>;
8464 *
8465 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8466 * generalization of the TLS 1.2 signature algorithm extension.
8467 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8468 * `SignatureScheme` field of TLS 1.3
8469 *
8470 */
8471int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8472 const unsigned char *end, size_t *out_len )
8473{
8474 unsigned char *p = buf;
8475 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8476 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8477
8478 *out_len = 0;
8479
8480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8481
8482 /* Check if we have space for header and length field:
8483 * - extension_type (2 bytes)
8484 * - extension_data_length (2 bytes)
8485 * - supported_signature_algorithms_length (2 bytes)
8486 */
8487 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8488 p += 6;
8489
8490 /*
8491 * Write supported_signature_algorithms
8492 */
8493 supported_sig_alg = p;
8494 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8495 if( sig_alg == NULL )
8496 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8497
8498 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8499 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8501 *sig_alg,
8502 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008503 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8504 continue;
8505 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8506 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8507 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008509 *sig_alg,
8510 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008511 }
8512
8513 /* Length of supported_signature_algorithms */
8514 supported_sig_alg_len = p - supported_sig_alg;
8515 if( supported_sig_alg_len == 0 )
8516 {
8517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8519 }
8520
XiaokangQianeaf36512022-04-24 09:07:44 +00008521 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008522 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008523 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8524
XiaokangQianeaf36512022-04-24 09:07:44 +00008525 *out_len = p - buf;
8526
8527#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8528 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8529#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8530 return( 0 );
8531}
8532#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8533
XiaokangQian40a35232022-05-07 09:02:40 +00008534#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008535/*
8536 * mbedtls_ssl_parse_server_name_ext
8537 *
8538 * Structure of server_name extension:
8539 *
8540 * enum {
8541 * host_name(0), (255)
8542 * } NameType;
8543 * opaque HostName<1..2^16-1>;
8544 *
8545 * struct {
8546 * NameType name_type;
8547 * select (name_type) {
8548 * case host_name: HostName;
8549 * } name;
8550 * } ServerName;
8551 * struct {
8552 * ServerName server_name_list<1..2^16-1>
8553 * } ServerNameList;
8554 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008555MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008556int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8557 const unsigned char *buf,
8558 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008559{
8560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8561 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008562 size_t server_name_list_len, hostname_len;
8563 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008564
XiaokangQianf2a94202022-05-20 06:44:24 +00008565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008566
8567 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008568 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008569 p += 2;
8570
XiaokangQian9b2b7712022-05-17 02:57:00 +00008571 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8572 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008573 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008574 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008575 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008576 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008577 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8578 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008579
8580 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8581 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008582 /* sni_name is intended to be used only during the parsing of the
8583 * ClientHello message (it is reset to NULL before the end of
8584 * the message parsing). Thus it is ok to just point to the
8585 * reception buffer and not make a copy of it.
8586 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008587 ssl->handshake->sni_name = p + 3;
8588 ssl->handshake->sni_name_len = hostname_len;
8589 if( ssl->conf->f_sni == NULL )
8590 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008591 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008592 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008593 if( ret != 0 )
8594 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008596 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8597 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008598 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8599 }
8600 return( 0 );
8601 }
8602
8603 p += hostname_len + 3;
8604 }
8605
8606 return( 0 );
8607}
8608#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8609
XiaokangQianacb39922022-06-17 10:18:48 +00008610#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008611MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008612int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8613 const unsigned char *buf,
8614 const unsigned char *end )
8615{
8616 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008617 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008618 const unsigned char *protocol_name_list;
8619 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008620 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008621
8622 /* If ALPN not configured, just ignore the extension */
8623 if( ssl->conf->alpn_list == NULL )
8624 return( 0 );
8625
8626 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008627 * RFC7301, section 3.1
8628 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008629 *
XiaokangQianc7403452022-06-23 03:24:12 +00008630 * struct {
8631 * ProtocolName protocol_name_list<2..2^16-1>
8632 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008633 */
8634
XiaokangQianc7403452022-06-23 03:24:12 +00008635 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008636 * protocol_name_list_len 2 bytes
8637 * protocol_name_len 1 bytes
8638 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008639 */
XiaokangQianacb39922022-06-17 10:18:48 +00008640 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8641
XiaokangQianc7403452022-06-23 03:24:12 +00008642 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008643 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008644 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008645 protocol_name_list = p;
8646 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008647
8648 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008649 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008650 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008651 protocol_name_len = *p++;
8652 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8653 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008654 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008655 {
8656 MBEDTLS_SSL_PEND_FATAL_ALERT(
8657 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8658 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008659 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008660 }
8661
8662 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008663 }
8664
8665 /* Use our order of preference */
8666 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8667 {
8668 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008669 p = protocol_name_list;
8670 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008671 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008672 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008673 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008674 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008675 {
8676 ssl->alpn_chosen = *alpn;
8677 return( 0 );
8678 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008679
8680 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008681 }
8682 }
8683
XiaokangQian95d5f542022-06-24 02:29:26 +00008684 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008685 MBEDTLS_SSL_PEND_FATAL_ALERT(
8686 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8687 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8688 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8689}
8690
8691int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8692 unsigned char *buf,
8693 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008694 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008695{
8696 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008697 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008698 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008699
8700 if( ssl->alpn_chosen == NULL )
8701 {
8702 return( 0 );
8703 }
8704
XiaokangQian95d5f542022-06-24 02:29:26 +00008705 protocol_name_len = strlen( ssl->alpn_chosen );
8706 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008707
8708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8709 /*
8710 * 0 . 1 ext identifier
8711 * 2 . 3 ext length
8712 * 4 . 5 protocol list length
8713 * 6 . 6 protocol name length
8714 * 7 . 7+n protocol name
8715 */
8716 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8717
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008719
XiaokangQian95d5f542022-06-24 02:29:26 +00008720 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8721 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008722 /* Note: the length of the chosen protocol has been checked to be less
8723 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8724 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008725 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008726
XiaokangQian95d5f542022-06-24 02:29:26 +00008727 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008728 return ( 0 );
8729}
8730#endif /* MBEDTLS_SSL_ALPN */
8731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#endif /* MBEDTLS_SSL_TLS_C */