blob: 616df07de8c1aa0fc72c6c53708a370716640615 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
Jerry Yu6ade7432022-01-25 10:39:33 +080034#include <stdio.h>
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_calloc calloc
36#define mbedtls_free free
Jerry Yu6ade7432022-01-25 10:39:33 +080037#define mbedtls_printf printf
38#endif /* !MBEDTLS_PLATFORM_C */
SimonBd5800b72016-04-26 07:43:27 +010039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010041#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010042#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000043#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040044
Janos Follath73c616b2019-12-18 15:07:04 +000045#include "mbedtls/debug.h"
46#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050047#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010048#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020049#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020057#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050058
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Ronald Cronad8c17b2022-06-10 17:18:09 +020063#if defined(MBEDTLS_TEST_HOOKS)
64static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
65
66void mbedtls_ssl_set_chk_buf_ptr_fail_args(
67 const uint8_t *cur, const uint8_t *end, size_t need )
68{
69 chk_buf_ptr_fail_args.cur = cur;
70 chk_buf_ptr_fail_args.end = end;
71 chk_buf_ptr_fail_args.need = need;
72}
73
74void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
75{
76 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
77}
78
79int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
80{
81 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
82 ( chk_buf_ptr_fail_args.end != args->end ) ||
83 ( chk_buf_ptr_fail_args.need != args->need ) );
84}
85#endif /* MBEDTLS_TEST_HOOKS */
86
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010088
Hanno Beckera0e20d02019-05-15 14:03:01 +010089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010090/* Top-level Connection ID API */
91
Hanno Becker8367ccc2019-05-14 11:30:10 +010092int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
93 size_t len,
94 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010095{
96 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
97 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
98
Hanno Becker611ac772019-05-14 11:45:26 +010099 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
100 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
101 {
102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
103 }
104
105 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106 conf->cid_len = len;
107 return( 0 );
108}
109
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100110int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
111 int enable,
112 unsigned char const *own_cid,
113 size_t own_cid_len )
114{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100115 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
117
Hanno Beckerca092242019-04-25 16:01:49 +0100118 ssl->negotiate_cid = enable;
119 if( enable == MBEDTLS_SSL_CID_DISABLED )
120 {
121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
122 return( 0 );
123 }
124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100125 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100126
Hanno Beckerad4a1372019-05-03 13:06:44 +0100127 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100128 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
133 }
134
135 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100140 return( 0 );
141}
142
Paul Elliott0113cf12022-03-11 20:26:47 +0000143int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len )
147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
150 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
152
153 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
154 * zero as this is indistinguishable from not requesting to use
155 * the CID extension. */
156 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
157 return( 0 );
158
159 if( own_cid_len != NULL )
160 {
161 *own_cid_len = ssl->own_cid_len;
162 if( own_cid != NULL )
163 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
164 }
165
166 *enabled = MBEDTLS_SSL_CID_ENABLED;
167
168 return( 0 );
169}
170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177
Hanno Becker76a79ab2019-05-03 14:38:32 +0100178 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000179 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100182 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Hanno Beckerc5f24222019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker615ef172019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213/*
214 * Convert max_fragment_length codes to length.
215 * RFC 6066 says:
216 * enum{
217 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
218 * } MaxFragmentLength;
219 * and we add 0 -> extension unused
220 */
Angus Grattond8213d02016-05-25 20:56:48 +1000221static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222{
Angus Grattond8213d02016-05-25 20:56:48 +1000223 switch( mfl )
224 {
225 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
226 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
227 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
228 return 512;
229 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
230 return 1024;
231 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
232 return 2048;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
234 return 4096;
235 default:
236 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
237 }
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200240
Hanno Becker52055ae2019-02-06 14:30:46 +0000241int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
242 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ssl_session_free( dst );
245 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254 if( src->peer_cert != NULL )
255 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200257
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200258 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200265 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200268 dst->peer_cert = NULL;
269 return( ret );
270 }
271 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 if( src->peer_cert_digest != NULL )
274 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 if( dst->peer_cert_digest == NULL )
278 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
279
280 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
281 src->peer_cert_digest_len );
282 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000283 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000284 }
285#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200288
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200289#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290 if( src->ticket != NULL )
291 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200292 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200293 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200295
296 memcpy( dst->ticket, src->ticket, src->ticket_len );
297 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
300 return( 0 );
301}
302
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500303#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200304MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
306{
307 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
308 if( resized_buffer == NULL )
309 return -1;
310
311 /* We want to copy len_new bytes when downsizing the buffer, and
312 * len_old bytes when upsizing, so we choose the smaller of two sizes,
313 * to fit one buffer into another. Size checks, ensuring that no data is
314 * lost, are done outside of this function. */
315 memcpy( resized_buffer, *buffer,
316 ( len_new < *len_old ) ? len_new : *len_old );
317 mbedtls_platform_zeroize( *buffer, *len_old );
318 mbedtls_free( *buffer );
319
320 *buffer = resized_buffer;
321 *len_old = len_new;
322
323 return 0;
324}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200325
326static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500327 size_t in_buf_new_len,
328 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200329{
330 int modified = 0;
331 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
332 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
333 if( ssl->in_buf != NULL )
334 {
335 written_in = ssl->in_msg - ssl->in_buf;
336 iv_offset_in = ssl->in_iv - ssl->in_buf;
337 len_offset_in = ssl->in_len - ssl->in_buf;
338 if( downsizing ?
339 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
340 ssl->in_buf_len < in_buf_new_len )
341 {
342 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
343 {
344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
345 }
346 else
347 {
Paul Elliottb7449902021-03-10 18:14:58 +0000348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
349 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 modified = 1;
351 }
352 }
353 }
354
355 if( ssl->out_buf != NULL )
356 {
357 written_out = ssl->out_msg - ssl->out_buf;
358 iv_offset_out = ssl->out_iv - ssl->out_buf;
359 len_offset_out = ssl->out_len - ssl->out_buf;
360 if( downsizing ?
361 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
362 ssl->out_buf_len < out_buf_new_len )
363 {
364 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
365 {
366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
367 }
368 else
369 {
Paul Elliottb7449902021-03-10 18:14:58 +0000370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
371 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 modified = 1;
373 }
374 }
375 }
376 if( modified )
377 {
378 /* Update pointers here to avoid doing it twice. */
379 mbedtls_ssl_reset_in_out_pointers( ssl );
380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Jerry Yudb8c48a2022-01-27 14:54:54 +0800393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
396typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
397 const char *label,
398 const unsigned char *random, size_t rlen,
399 unsigned char *dstbuf, size_t dlen );
400
401static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
402
403#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
404
405/* Type for the TLS PRF */
406typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
407 const unsigned char *, size_t,
408 unsigned char *, size_t);
409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800411static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
412 int ciphersuite,
413 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800415 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200416#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800417 ssl_tls_prf_t tls_prf,
418 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400419 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800420 unsigned endpoint,
421 const mbedtls_ssl_context *ssl );
422
Andrzej Kurek25f27152022-08-17 16:09:31 -0400423#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800425static int tls_prf_sha256( const unsigned char *secret, size_t slen,
426 const char *label,
427 const unsigned char *random, size_t rlen,
428 unsigned char *dstbuf, size_t dlen );
429static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
430static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
431
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400432#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800433
Andrzej Kurek25f27152022-08-17 16:09:31 -0400434#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200435MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800436static int tls_prf_sha384( const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen );
440
441static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
442static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800444
Jerry Yu438ddd82022-07-07 06:55:50 +0000445static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800446 unsigned char *buf,
447 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800448
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000450static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800451 const unsigned char *buf,
452 size_t len );
453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
454
Jerry Yu53d23e22022-02-09 16:25:09 +0800455static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
456
Andrzej Kurek25f27152022-08-17 16:09:31 -0400457#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800458static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400459#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800460
Andrzej Kurek25f27152022-08-17 16:09:31 -0400461#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800462static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400463#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000464
Ron Eldor51d3ab52019-05-12 14:54:30 +0300465int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
466 const unsigned char *secret, size_t slen,
467 const char *label,
468 const unsigned char *random, size_t rlen,
469 unsigned char *dstbuf, size_t dlen )
470{
471 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
472
473 switch( prf )
474 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400476#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA384:
478 tls_prf = tls_prf_sha384;
479 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA256:
483 tls_prf = tls_prf_sha256;
484 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 default:
488 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
489 }
490
491 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
495static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
498 if( session->peer_cert != NULL )
499 {
500 mbedtls_x509_crt_free( session->peer_cert );
501 mbedtls_free( session->peer_cert );
502 session->peer_cert = NULL;
503 }
504#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
505 if( session->peer_cert_digest != NULL )
506 {
507 /* Zeroization is not necessary. */
508 mbedtls_free( session->peer_cert_digest );
509 session->peer_cert_digest = NULL;
510 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
511 session->peer_cert_digest_len = 0;
512 }
513#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
514}
515#endif /* MBEDTLS_X509_CRT_PARSE_C */
516
517void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
519{
520 ((void) ciphersuite_info);
521
Andrzej Kurek25f27152022-08-17 16:09:31 -0400522#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800523 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
524 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
525 else
526#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400527#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800528 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
529 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
530 else
531#endif
532 {
533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
534 return;
535 }
536}
537
XiaokangQianadab9a62022-07-18 07:41:26 +0000538void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
539 unsigned hs_type,
540 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100541{
542 unsigned char hs_hdr[4];
543
544 /* Build HS header for checksum update. */
545 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
546 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
547 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
548 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
549
550 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
551}
552
553void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
554 unsigned hs_type,
555 unsigned char const *msg,
556 size_t msg_len )
557{
558 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
559 ssl->handshake->update_checksum( ssl, msg, msg_len );
560}
561
Jerry Yuc73c6182022-02-08 20:29:25 +0800562void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
563{
564 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400565#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800566#if defined(MBEDTLS_USE_PSA_CRYPTO)
567 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
568 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
569#else
570 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
571#endif
572#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800574#if defined(MBEDTLS_USE_PSA_CRYPTO)
575 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
576 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
577#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400578 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800579#endif
580#endif
581}
582
583static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
584 const unsigned char *buf, size_t len )
585{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400586#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800587#if defined(MBEDTLS_USE_PSA_CRYPTO)
588 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
589#else
590 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
591#endif
592#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
596#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400597 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#endif
599#endif
600}
601
Andrzej Kurek25f27152022-08-17 16:09:31 -0400602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800603static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
604 const unsigned char *buf, size_t len )
605{
606#if defined(MBEDTLS_USE_PSA_CRYPTO)
607 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
608#else
609 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
610#endif
611}
612#endif
613
Andrzej Kurek25f27152022-08-17 16:09:31 -0400614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800615static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
616 const unsigned char *buf, size_t len )
617{
618#if defined(MBEDTLS_USE_PSA_CRYPTO)
619 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
620#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400621 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800622#endif
623}
624#endif
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200629
Andrzej Kurek25f27152022-08-17 16:09:31 -0400630#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 handshake->fin_sha256_psa = psa_hash_operation_init();
633 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
634#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200636 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200637#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500638#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500641 handshake->fin_sha384_psa = psa_hash_operation_init();
642 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500643#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400644 mbedtls_sha512_init( &handshake->fin_sha384 );
645 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200646#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500647#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648
649 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_DHM_C)
652 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200653#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200654#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200656#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200657#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200658 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200659#if defined(MBEDTLS_SSL_CLI_C)
660 handshake->ecjpake_cache = NULL;
661 handshake->ecjpake_cache_len = 0;
662#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200663#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200664
Gilles Peskineeccd8882020-03-10 12:19:08 +0100665#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200666 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200667#endif
668
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
670 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
671#endif
Hanno Becker75173122019-02-06 16:18:31 +0000672
673#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
674 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
675 mbedtls_pk_init( &handshake->peer_pubkey );
676#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200677}
678
Hanno Beckera18d1322018-01-03 14:27:32 +0000679void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200680{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200682
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100683#if defined(MBEDTLS_USE_PSA_CRYPTO)
684 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
685 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100686#else
687 mbedtls_cipher_init( &transform->cipher_ctx_enc );
688 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100689#endif
690
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000691#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100692#if defined(MBEDTLS_USE_PSA_CRYPTO)
693 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
694 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100695#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_init( &transform->md_ctx_enc );
697 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000698#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100699#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200700}
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200705}
706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200707MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000709{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200710 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000711 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200713 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200715 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200716 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717
718 /*
719 * Either the pointers are now NULL or cleared properly and can be freed.
720 * Now allocate missing structures.
721 */
722 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200724 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200725 }
Paul Bakker48916f92012-09-16 19:57:18 +0000726
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200728 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200729 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 }
Paul Bakker48916f92012-09-16 19:57:18 +0000731
Paul Bakker82788fb2014-10-20 13:59:19 +0200732 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200733 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200735 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500736#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
737 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400738
Andrzej Kurek4a063792020-10-21 15:08:44 +0200739 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
740 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500741#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200743 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000744 if( ssl->handshake == NULL ||
745 ssl->transform_negotiate == NULL ||
746 ssl->session_negotiate == NULL )
747 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_free( ssl->handshake );
751 mbedtls_free( ssl->transform_negotiate );
752 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200753
754 ssl->handshake = NULL;
755 ssl->transform_negotiate = NULL;
756 ssl->session_negotiate = NULL;
757
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 }
760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000763 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200764 ssl_handshake_params_init( ssl->handshake );
765
Jerry Yud0766ec2022-09-22 10:46:57 +0800766#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
767 defined(MBEDTLS_SSL_SRV_C) && \
768 defined(MBEDTLS_SSL_SESSION_TICKETS)
769 ssl->handshake->new_session_tickets_count =
770 ssl->conf->new_session_tickets_count ;
771#endif
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
775 {
776 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200777
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200778 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
779 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
780 else
781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200782
Hanno Becker0f57a652020-02-05 10:37:26 +0000783 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200784 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200785#endif
786
Brett Warrene0edc842021-08-17 09:53:13 +0100787/*
788 * curve_list is translated to IANA TLS group identifiers here because
789 * mbedtls_ssl_conf_curves returns void and so can't return
790 * any error codes.
791 */
792#if defined(MBEDTLS_ECP_C)
793#if !defined(MBEDTLS_DEPRECATED_REMOVED)
794 /* Heap allocate and translate curve_list from internal to IANA group ids */
795 if ( ssl->conf->curve_list != NULL )
796 {
797 size_t length;
798 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
799
800 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
801 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
802
803 /* Leave room for zero termination */
804 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
805 if ( group_list == NULL )
806 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
807
808 for( size_t i = 0; i < length; i++ )
809 {
810 const mbedtls_ecp_curve_info *info =
811 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
812 if ( info == NULL )
813 {
814 mbedtls_free( group_list );
815 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
816 }
817 group_list[i] = info->tls_id;
818 }
819
820 group_list[length] = 0;
821
822 ssl->handshake->group_list = group_list;
823 ssl->handshake->group_list_heap_allocated = 1;
824 }
825 else
826 {
827 ssl->handshake->group_list = ssl->conf->group_list;
828 ssl->handshake->group_list_heap_allocated = 0;
829 }
830#endif /* MBEDTLS_DEPRECATED_REMOVED */
831#endif /* MBEDTLS_ECP_C */
832
Jerry Yuf017ee42022-01-12 15:49:48 +0800833#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
834#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800836 /* Heap allocate and translate sig_hashes from internal hash identifiers to
837 signature algorithms IANA identifiers. */
838 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800839 ssl->conf->sig_hashes != NULL )
840 {
841 const int *md;
842 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800843 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800844 uint16_t *p;
845
Jerry Yub476a442022-01-21 18:14:45 +0800846#if defined(static_assert)
847 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
848 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
849 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800850#endif
851
Jerry Yuf017ee42022-01-12 15:49:48 +0800852 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
853 {
854 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
855 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800856#if defined(MBEDTLS_ECDSA_C)
857 sig_algs_len += sizeof( uint16_t );
858#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800859
Jerry Yub476a442022-01-21 18:14:45 +0800860#if defined(MBEDTLS_RSA_C)
861 sig_algs_len += sizeof( uint16_t );
862#endif
863 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
864 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800865 }
866
Jerry Yub476a442022-01-21 18:14:45 +0800867 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800868 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
869
Jerry Yub476a442022-01-21 18:14:45 +0800870 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
871 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800872 if( ssl->handshake->sig_algs == NULL )
873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
874
875 p = (uint16_t *)ssl->handshake->sig_algs;
876 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
877 {
878 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
879 if( hash == MBEDTLS_SSL_HASH_NONE )
880 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800881#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
883 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800884#endif
885#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
887 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800888#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800889 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200890 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800891 ssl->handshake->sig_algs_heap_allocated = 1;
892 }
893 else
Jerry Yua69269a2022-01-17 21:06:01 +0800894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800895 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 ssl->handshake->sig_algs_heap_allocated = 0;
897 }
Jerry Yucc539102022-06-27 16:27:35 +0800898#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800899#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000900 return( 0 );
901}
902
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200903#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200904/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200905MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200906static int ssl_cookie_write_dummy( void *ctx,
907 unsigned char **p, unsigned char *end,
908 const unsigned char *cli_id, size_t cli_id_len )
909{
910 ((void) ctx);
911 ((void) p);
912 ((void) end);
913 ((void) cli_id);
914 ((void) cli_id_len);
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917}
918
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200919MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200920static int ssl_cookie_check_dummy( void *ctx,
921 const unsigned char *cookie, size_t cookie_len,
922 const unsigned char *cli_id, size_t cli_id_len )
923{
924 ((void) ctx);
925 ((void) cookie);
926 ((void) cookie_len);
927 ((void) cli_id);
928 ((void) cli_id_len);
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200932#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200933
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * Initialize an SSL context
936 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200937void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
938{
939 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
940}
941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200942MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800943static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
944{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100945 const mbedtls_ssl_config *conf = ssl->conf;
946
Ronald Cron6f135e12021-12-08 16:57:54 +0100947#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100948 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
949 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000950 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
953 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
954 }
955
Jerry Yu60835a82021-08-04 10:13:52 +0800956 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
957 return( 0 );
958 }
959#endif
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100962 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800963 {
964 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
965 return( 0 );
966 }
967#endif
968
Ronald Cron6f135e12021-12-08 16:57:54 +0100969#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100970 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800971 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
973 {
974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
975 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
976 }
977
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000978 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
981 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
982 }
983
984
Ronald Crone1d3f062022-02-10 14:50:54 +0100985 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
986 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800987 }
988#endif
989
990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
991 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
992}
993
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800995static int ssl_conf_check(const mbedtls_ssl_context *ssl)
996{
997 int ret;
998 ret = ssl_conf_version_check( ssl );
999 if( ret != 0 )
1000 return( ret );
1001
1002 /* Space for further checks */
1003
1004 return( 0 );
1005}
1006
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001007/*
1008 * Setup an SSL context
1009 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001010
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001011int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001012 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013{
Janos Follath865b3eb2019-12-16 11:46:15 +00001014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001015 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1016 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001018 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001019
Jerry Yu60835a82021-08-04 10:13:52 +08001020 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1021 return( ret );
1022
Paul Bakker62f2dee2012-09-28 07:31:51 +00001023 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001024 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001025 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001026
1027 /* Set to NULL in case of an error condition */
1028 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001029
Darryl Greenb33cc762019-11-28 14:29:44 +00001030#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1031 ssl->in_buf_len = in_buf_len;
1032#endif
1033 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001034 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001037 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001038 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001039 }
1040
Darryl Greenb33cc762019-11-28 14:29:44 +00001041#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1042 ssl->out_buf_len = out_buf_len;
1043#endif
1044 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001045 if( ssl->out_buf == NULL )
1046 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001048 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001049 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
1051
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001052 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001053
Johan Pascalb62bb512015-12-03 21:56:45 +01001054#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001055 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001056#endif
1057
Paul Bakker48916f92012-09-16 19:57:18 +00001058 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001059 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001062
1063error:
1064 mbedtls_free( ssl->in_buf );
1065 mbedtls_free( ssl->out_buf );
1066
1067 ssl->conf = NULL;
1068
Darryl Greenb33cc762019-11-28 14:29:44 +00001069#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1070 ssl->in_buf_len = 0;
1071 ssl->out_buf_len = 0;
1072#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001073 ssl->in_buf = NULL;
1074 ssl->out_buf = NULL;
1075
1076 ssl->in_hdr = NULL;
1077 ssl->in_ctr = NULL;
1078 ssl->in_len = NULL;
1079 ssl->in_iv = NULL;
1080 ssl->in_msg = NULL;
1081
1082 ssl->out_hdr = NULL;
1083 ssl->out_ctr = NULL;
1084 ssl->out_len = NULL;
1085 ssl->out_iv = NULL;
1086 ssl->out_msg = NULL;
1087
k-stachowiak9f7798e2018-07-31 16:52:32 +02001088 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089}
1090
1091/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001092 * Reset an initialized and used SSL context for re-use while retaining
1093 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001094 *
1095 * If partial is non-zero, keep data in the input buffer and client ID.
1096 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001097 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001098void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1099 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001100{
Darryl Greenb33cc762019-11-28 14:29:44 +00001101#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1102 size_t in_buf_len = ssl->in_buf_len;
1103 size_t out_buf_len = ssl->out_buf_len;
1104#else
1105 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1106 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1107#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001108
Hanno Beckerb0302c42021-08-03 09:39:42 +01001109#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1110 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001111#endif
1112
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001113 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001114 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001115
Hanno Beckerb0302c42021-08-03 09:39:42 +01001116 mbedtls_ssl_reset_in_out_pointers( ssl );
1117
1118 /* Reset incoming message parsing */
1119 ssl->in_offt = NULL;
1120 ssl->nb_zero = 0;
1121 ssl->in_msgtype = 0;
1122 ssl->in_msglen = 0;
1123 ssl->in_hslen = 0;
1124 ssl->keep_current_message = 0;
1125 ssl->transform_in = NULL;
1126
1127#if defined(MBEDTLS_SSL_PROTO_DTLS)
1128 ssl->next_record_offset = 0;
1129 ssl->in_epoch = 0;
1130#endif
1131
1132 /* Keep current datagram if partial == 1 */
1133 if( partial == 0 )
1134 {
1135 ssl->in_left = 0;
1136 memset( ssl->in_buf, 0, in_buf_len );
1137 }
1138
Ronald Cronad8c17b2022-06-10 17:18:09 +02001139 ssl->send_alert = 0;
1140
Hanno Beckerb0302c42021-08-03 09:39:42 +01001141 /* Reset outgoing message writing */
1142 ssl->out_msgtype = 0;
1143 ssl->out_msglen = 0;
1144 ssl->out_left = 0;
1145 memset( ssl->out_buf, 0, out_buf_len );
1146 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1147 ssl->transform_out = NULL;
1148
1149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1150 mbedtls_ssl_dtls_replay_reset( ssl );
1151#endif
1152
XiaokangQian2b01dc32022-01-21 02:53:13 +00001153#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001154 if( ssl->transform )
1155 {
1156 mbedtls_ssl_transform_free( ssl->transform );
1157 mbedtls_free( ssl->transform );
1158 ssl->transform = NULL;
1159 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001160#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1161
XiaokangQian2b01dc32022-01-21 02:53:13 +00001162#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1163 mbedtls_ssl_transform_free( ssl->transform_application );
1164 mbedtls_free( ssl->transform_application );
1165 ssl->transform_application = NULL;
1166
1167 if( ssl->handshake != NULL )
1168 {
1169 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1170 mbedtls_free( ssl->handshake->transform_earlydata );
1171 ssl->handshake->transform_earlydata = NULL;
1172
1173 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1174 mbedtls_free( ssl->handshake->transform_handshake );
1175 ssl->handshake->transform_handshake = NULL;
1176 }
1177
XiaokangQian2b01dc32022-01-21 02:53:13 +00001178#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001179}
1180
1181int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1182{
1183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1184
1185 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1186
XiaokangQian78b1fa72022-01-19 06:56:30 +00001187 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001188
1189 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_RENEGOTIATION)
1191 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001192 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001193
1194 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1196 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001199
Hanno Beckerb0302c42021-08-03 09:39:42 +01001200 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001201 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001202 if( ssl->session )
1203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_ssl_session_free( ssl->session );
1205 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001206 ssl->session = NULL;
1207 }
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001210 ssl->alpn_chosen = NULL;
1211#endif
1212
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001213#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001214#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001215 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001216#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001217 {
1218 mbedtls_free( ssl->cli_id );
1219 ssl->cli_id = NULL;
1220 ssl->cli_id_len = 0;
1221 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001222#endif
1223
Paul Bakker48916f92012-09-16 19:57:18 +00001224 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1225 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001226
1227 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001228}
1229
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001230/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001231 * Reset an initialized and used SSL context for re-use while retaining
1232 * all application-set variables, function pointers and data.
1233 */
1234int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1235{
Hanno Becker43aefe22020-02-05 10:44:56 +00001236 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001237}
1238
1239/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 * SSL set accessors
1241 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001242void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001244 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245}
1246
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001247void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001248{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001249 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001250}
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001253void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001256}
1257#endif
1258
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001259void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001261 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001262}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001265
Hanno Becker1841b0a2018-08-24 11:13:57 +01001266void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1267 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001268{
1269 ssl->disable_datagram_packing = !allow_packing;
1270}
1271
1272void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1273 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->hs_timeout_min = min;
1276 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001277}
1278#endif
1279
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001280void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001282 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283}
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001287 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001288 void *p_vrfy )
1289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001290 conf->f_vrfy = f_vrfy;
1291 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001295void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001296 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 void *p_rng )
1298{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001299 conf->f_rng = f_rng;
1300 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001301}
1302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001304 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 void *p_dbg )
1306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001307 conf->f_dbg = f_dbg;
1308 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309}
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001312 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001313 mbedtls_ssl_send_t *f_send,
1314 mbedtls_ssl_recv_t *f_recv,
1315 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001316{
1317 ssl->p_bio = p_bio;
1318 ssl->f_send = f_send;
1319 ssl->f_recv = f_recv;
1320 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001321}
1322
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001323#if defined(MBEDTLS_SSL_PROTO_DTLS)
1324void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1325{
1326 ssl->mtu = mtu;
1327}
1328#endif
1329
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001330void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001331{
1332 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001333}
1334
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001335void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1336 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001337 mbedtls_ssl_set_timer_t *f_set_timer,
1338 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001339{
1340 ssl->p_timer = p_timer;
1341 ssl->f_set_timer = f_set_timer;
1342 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001343
1344 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001345 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001349void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001350 void *p_cache,
1351 mbedtls_ssl_cache_get_t *f_get_cache,
1352 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001353{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001354 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001355 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001356 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_SSL_CLI_C)
1361int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362{
Janos Follath865b3eb2019-12-16 11:46:15 +00001363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001364
1365 if( ssl == NULL ||
1366 session == NULL ||
1367 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001368 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001371 }
1372
Hanno Beckere810bbc2021-05-14 16:01:05 +01001373 if( ssl->handshake->resume == 1 )
1374 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1375
Hanno Becker52055ae2019-02-06 14:30:46 +00001376 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1377 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001378 return( ret );
1379
Paul Bakker0a597072012-09-25 21:55:46 +00001380 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001381
1382 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001386void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1387 const int *ciphersuites )
1388{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001389 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390}
1391
Ronald Cron6f135e12021-12-08 16:57:54 +01001392#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001393void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001394 const int kex_modes )
1395{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001396 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001397}
Ronald Cron6f135e12021-12-08 16:57:54 +01001398#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001401void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001402 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001403{
1404 conf->cert_profile = profile;
1405}
1406
Glenn Strauss36872db2022-01-22 05:06:31 -05001407static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1408{
1409 mbedtls_ssl_key_cert *cur = key_cert, *next;
1410
1411 while( cur != NULL )
1412 {
1413 next = cur->next;
1414 mbedtls_free( cur );
1415 cur = next;
1416 }
1417}
1418
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001419/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001420MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001421static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1422 mbedtls_x509_crt *cert,
1423 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001424{
niisato8ee24222018-06-25 19:05:48 +09001425 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001426
Glenn Strauss36872db2022-01-22 05:06:31 -05001427 if( cert == NULL )
1428 {
1429 /* Free list if cert is null */
1430 ssl_key_cert_free( *head );
1431 *head = NULL;
1432 return( 0 );
1433 }
1434
niisato8ee24222018-06-25 19:05:48 +09001435 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1436 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001438
niisato8ee24222018-06-25 19:05:48 +09001439 new_cert->cert = cert;
1440 new_cert->key = key;
1441 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001442
Glenn Strauss36872db2022-01-22 05:06:31 -05001443 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001444 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001445 {
niisato8ee24222018-06-25 19:05:48 +09001446 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001447 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001448 else
1449 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001450 mbedtls_ssl_key_cert *cur = *head;
1451 while( cur->next != NULL )
1452 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001453 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001454 }
1455
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001456 return( 0 );
1457}
1458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001459int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001460 mbedtls_x509_crt *own_cert,
1461 mbedtls_pk_context *pk_key )
1462{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001463 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001464}
1465
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001466void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001467 mbedtls_x509_crt *ca_chain,
1468 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001470 conf->ca_chain = ca_chain;
1471 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001472
1473#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1474 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1475 * cannot be used together. */
1476 conf->f_ca_cb = NULL;
1477 conf->p_ca_cb = NULL;
1478#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001479}
Hanno Becker5adaad92019-03-27 16:54:37 +00001480
1481#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1482void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001483 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001484 void *p_ca_cb )
1485{
1486 conf->f_ca_cb = f_ca_cb;
1487 conf->p_ca_cb = p_ca_cb;
1488
1489 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1490 * cannot be used together. */
1491 conf->ca_chain = NULL;
1492 conf->ca_crl = NULL;
1493}
1494#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001496
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001497#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001498const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1499 size_t *name_len )
1500{
1501 *name_len = ssl->handshake->sni_name_len;
1502 return( ssl->handshake->sni_name );
1503}
1504
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001505int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1506 mbedtls_x509_crt *own_cert,
1507 mbedtls_pk_context *pk_key )
1508{
1509 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1510 own_cert, pk_key ) );
1511}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001512
1513void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1514 mbedtls_x509_crt *ca_chain,
1515 mbedtls_x509_crl *ca_crl )
1516{
1517 ssl->handshake->sni_ca_chain = ca_chain;
1518 ssl->handshake->sni_ca_crl = ca_crl;
1519}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001520
Glenn Strauss999ef702022-03-11 01:37:23 -05001521#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1522void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1523 const mbedtls_x509_crt *crt)
1524{
1525 ssl->handshake->dn_hints = crt;
1526}
1527#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1528
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001529void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1530 int authmode )
1531{
1532 ssl->handshake->sni_authmode = authmode;
1533}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001534#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1535
Hanno Becker8927c832019-04-03 12:52:50 +01001536#if defined(MBEDTLS_X509_CRT_PARSE_C)
1537void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1538 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1539 void *p_vrfy )
1540{
1541 ssl->f_vrfy = f_vrfy;
1542 ssl->p_vrfy = p_vrfy;
1543}
1544#endif
1545
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001546#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001547/*
1548 * Set EC J-PAKE password for current handshake
1549 */
1550int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1551 const unsigned char *pw,
1552 size_t pw_len )
1553{
1554 mbedtls_ecjpake_role role;
1555
Janos Follath8eb64132016-06-03 15:40:57 +01001556 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1558
1559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1560 role = MBEDTLS_ECJPAKE_SERVER;
1561 else
1562 role = MBEDTLS_ECJPAKE_CLIENT;
1563
1564 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1565 role,
1566 MBEDTLS_MD_SHA256,
1567 MBEDTLS_ECP_DP_SECP256R1,
1568 pw, pw_len ) );
1569}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001571
Gilles Peskineeccd8882020-03-10 12:19:08 +01001572#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001573
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001574MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001575static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1576{
1577#if defined(MBEDTLS_USE_PSA_CRYPTO)
1578 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1579 return( 1 );
1580#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001581 if( conf->psk != NULL )
1582 return( 1 );
1583
1584 return( 0 );
1585}
1586
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001587static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1588{
1589 /* Remove reference to existing PSK, if any. */
1590#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001591 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001592 {
1593 /* The maintenance of the PSK key slot is the
1594 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001596 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001597#endif /* MBEDTLS_USE_PSA_CRYPTO */
1598 if( conf->psk != NULL )
1599 {
1600 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1601
1602 mbedtls_free( conf->psk );
1603 conf->psk = NULL;
1604 conf->psk_len = 0;
1605 }
1606
1607 /* Remove reference to PSK identity, if any. */
1608 if( conf->psk_identity != NULL )
1609 {
1610 mbedtls_free( conf->psk_identity );
1611 conf->psk_identity = NULL;
1612 conf->psk_identity_len = 0;
1613 }
1614}
1615
Hanno Becker7390c712018-11-15 13:33:04 +00001616/* This function assumes that PSK identity in the SSL config is unset.
1617 * It checks that the provided identity is well-formed and attempts
1618 * to make a copy of it in the SSL config.
1619 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001620MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001621static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1622 unsigned char const *psk_identity,
1623 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001624{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001625 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001626 if( psk_identity == NULL ||
1627 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001628 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001629 {
1630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1631 }
1632
Hanno Becker7390c712018-11-15 13:33:04 +00001633 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1634 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001635 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001636
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001637 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001638 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001639
1640 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001641}
1642
Hanno Becker7390c712018-11-15 13:33:04 +00001643int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1644 const unsigned char *psk, size_t psk_len,
1645 const unsigned char *psk_identity, size_t psk_identity_len )
1646{
Janos Follath865b3eb2019-12-16 11:46:15 +00001647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001648
1649 /* We currently only support one PSK, raw or opaque. */
1650 if( ssl_conf_psk_is_configured( conf ) )
1651 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001652
1653 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001654 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001656 if( psk_len == 0 )
1657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1658 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1660
Hanno Becker7390c712018-11-15 13:33:04 +00001661 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1662 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1663 conf->psk_len = psk_len;
1664 memcpy( conf->psk, psk, conf->psk_len );
1665
1666 /* Check and set PSK Identity */
1667 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1668 if( ret != 0 )
1669 ssl_conf_remove_psk( conf );
1670
1671 return( ret );
1672}
1673
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001674static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1675{
1676#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001677 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001678 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001679 /* The maintenance of the external PSK key slot is the
1680 * user's responsibility. */
1681 if( ssl->handshake->psk_opaque_is_internal )
1682 {
1683 psa_destroy_key( ssl->handshake->psk_opaque );
1684 ssl->handshake->psk_opaque_is_internal = 0;
1685 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001687 }
Neil Armstronge952a302022-05-03 10:22:14 +02001688#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001689 if( ssl->handshake->psk != NULL )
1690 {
1691 mbedtls_platform_zeroize( ssl->handshake->psk,
1692 ssl->handshake->psk_len );
1693 mbedtls_free( ssl->handshake->psk );
1694 ssl->handshake->psk_len = 0;
1695 }
Neil Armstronge952a302022-05-03 10:22:14 +02001696#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001697}
1698
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001699int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1700 const unsigned char *psk, size_t psk_len )
1701{
Neil Armstrong501c9322022-05-03 09:35:09 +02001702#if defined(MBEDTLS_USE_PSA_CRYPTO)
1703 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001705 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001707#endif /* MBEDTLS_USE_PSA_CRYPTO */
1708
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001709 if( psk == NULL || ssl->handshake == NULL )
1710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1711
1712 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1714
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001715 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001716
Neil Armstrong501c9322022-05-03 09:35:09 +02001717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1719 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1720 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001721 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001722 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1723 else
1724 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1725 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1726 }
1727#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001728
Jerry Yu568ec252022-07-22 21:27:34 +08001729#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001730 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1731 {
1732 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1733 psa_set_key_usage_flags( &key_attributes,
1734 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1735 }
1736#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1737
Neil Armstrong501c9322022-05-03 09:35:09 +02001738 psa_set_key_algorithm( &key_attributes, alg );
1739 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1740
1741 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1742 if( status != PSA_SUCCESS )
1743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1744
1745 /* Allow calling psa_destroy_key() on psk remove */
1746 ssl->handshake->psk_opaque_is_internal = 1;
1747 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1748#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001749 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001750 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001751
1752 ssl->handshake->psk_len = psk_len;
1753 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1754
1755 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001756#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001757}
1758
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001759#if defined(MBEDTLS_USE_PSA_CRYPTO)
1760int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001761 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001762 const unsigned char *psk_identity,
1763 size_t psk_identity_len )
1764{
Janos Follath865b3eb2019-12-16 11:46:15 +00001765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001766
1767 /* We currently only support one PSK, raw or opaque. */
1768 if( ssl_conf_psk_is_configured( conf ) )
1769 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001770
Hanno Becker7390c712018-11-15 13:33:04 +00001771 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001772 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001775
1776 /* Check and set PSK Identity */
1777 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1778 psk_identity_len );
1779 if( ret != 0 )
1780 ssl_conf_remove_psk( conf );
1781
1782 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001783}
1784
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001785int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1786 mbedtls_svc_key_id_t psk )
1787{
1788 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1789 ( ssl->handshake == NULL ) )
1790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1791
1792 ssl_remove_psk( ssl );
1793 ssl->handshake->psk_opaque = psk;
1794 return( 0 );
1795}
1796#endif /* MBEDTLS_USE_PSA_CRYPTO */
1797
Jerry Yu8897c072022-08-12 13:56:53 +08001798#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001799void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1800 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1801 size_t),
1802 void *p_psk )
1803{
1804 conf->f_psk = f_psk;
1805 conf->p_psk = p_psk;
1806}
Jerry Yu8897c072022-08-12 13:56:53 +08001807#endif /* MBEDTLS_SSL_SRV_C */
1808
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001809#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1810
1811#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001812static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1813 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001814{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001815#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001816 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001817 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001818#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001819 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001820 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001821 return( MBEDTLS_SSL_MODE_STREAM );
1822}
1823
1824#else /* MBEDTLS_USE_PSA_CRYPTO */
1825
1826static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1827 mbedtls_cipher_mode_t mode )
1828{
1829#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1830 if( mode == MBEDTLS_MODE_CBC )
1831 return( MBEDTLS_SSL_MODE_CBC );
1832#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1833
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001834#if defined(MBEDTLS_GCM_C) || \
1835 defined(MBEDTLS_CCM_C) || \
1836 defined(MBEDTLS_CHACHAPOLY_C)
1837 if( mode == MBEDTLS_MODE_GCM ||
1838 mode == MBEDTLS_MODE_CCM ||
1839 mode == MBEDTLS_MODE_CHACHAPOLY )
1840 return( MBEDTLS_SSL_MODE_AEAD );
1841#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001842
1843 return( MBEDTLS_SSL_MODE_STREAM );
1844}
Gilles Peskine301711e2022-04-26 16:57:05 +02001845#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846
Gilles Peskinee108d982022-04-26 16:50:40 +02001847static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1848 mbedtls_ssl_mode_t base_mode,
1849 int encrypt_then_mac )
1850{
1851#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1852 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1853 base_mode == MBEDTLS_SSL_MODE_CBC )
1854 {
1855 return( MBEDTLS_SSL_MODE_CBC_ETM );
1856 }
1857#else
1858 (void) encrypt_then_mac;
1859#endif
1860 return( base_mode );
1861}
1862
Neil Armstrongab555e02022-04-04 11:07:59 +02001863mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001864 const mbedtls_ssl_transform *transform )
1865{
Gilles Peskinee108d982022-04-26 16:50:40 +02001866 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001868 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001869#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001870 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1871#endif
1872 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001873
Gilles Peskinee108d982022-04-26 16:50:40 +02001874 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001875#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001876 encrypt_then_mac = transform->encrypt_then_mac;
1877#endif
1878 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001879}
1880
Neil Armstrongab555e02022-04-04 11:07:59 +02001881mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001882#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001883 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001884#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001885 const mbedtls_ssl_ciphersuite_t *suite )
1886{
Gilles Peskinee108d982022-04-26 16:50:40 +02001887 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1888
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001889#if defined(MBEDTLS_USE_PSA_CRYPTO)
1890 psa_status_t status;
1891 psa_algorithm_t alg;
1892 psa_key_type_t type;
1893 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1895 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897#else
1898 const mbedtls_cipher_info_t *cipher =
1899 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001901 {
1902 base_mode =
1903 mbedtls_ssl_get_base_mode(
1904 mbedtls_cipher_info_get_mode( cipher ) );
1905 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001906#endif /* MBEDTLS_USE_PSA_CRYPTO */
1907
Gilles Peskinee108d982022-04-26 16:50:40 +02001908#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1909 int encrypt_then_mac = 0;
1910#endif
1911 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001912}
1913
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001914#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001915
1916#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001917/* Serialization of TLS 1.3 sessions:
1918 *
1919 * struct {
1920 * uint64 ticket_received;
1921 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001922 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001923 * } ClientOnlyData;
1924 *
1925 * struct {
1926 * uint8 endpoint;
1927 * uint8 ciphersuite[2];
1928 * uint32 ticket_age_add;
1929 * uint8 ticket_flags;
1930 * opaque resumption_key<0..255>;
1931 * select ( endpoint ) {
1932 * case client: ClientOnlyData;
1933 * case server: uint64 start_time;
1934 * };
1935 * } serialized_session_tls13;
1936 *
1937 */
1938#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001939MBEDTLS_CHECK_RETURN_CRITICAL
1940static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1941 unsigned char *buf,
1942 size_t buf_len,
1943 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001944{
1945 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001946 size_t needed = 1 /* endpoint */
1947 + 2 /* ciphersuite */
1948 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001949 + 1 /* ticket_flags */
1950 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001951 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001952
1953 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1955 needed += session->resumption_key_len; /* resumption_key */
1956
Jerry Yu438ddd82022-07-07 06:55:50 +00001957#if defined(MBEDTLS_HAVE_TIME)
1958 needed += 8; /* start_time or ticket_received */
1959#endif
1960
1961#if defined(MBEDTLS_SSL_CLI_C)
1962 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1963 {
1964 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001965 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001966
1967 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001968 if( session->ticket_len > SIZE_MAX - needed )
1969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001970
Jerry Yue28d9742022-08-18 15:44:03 +08001971 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001972 }
1973#endif /* MBEDTLS_SSL_CLI_C */
1974
Jerry Yue36fdd62022-08-17 21:31:36 +08001975 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001976 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001977 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001978
1979 p[0] = session->endpoint;
1980 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1981 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1982 p[7] = session->ticket_flags;
1983
1984 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001985 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001986 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001987 memcpy( p, session->resumption_key, session->resumption_key_len );
1988 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001989
1990#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
1991 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
1992 {
1993 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
1994 p += 8;
1995 }
1996#endif /* MBEDTLS_HAVE_TIME */
1997
1998#if defined(MBEDTLS_SSL_CLI_C)
1999 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2000 {
2001#if defined(MBEDTLS_HAVE_TIME)
2002 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2003 p += 8;
2004#endif
2005 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2006 p += 4;
2007
2008 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2009 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002010
2011 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002012 {
2013 memcpy( p, session->ticket, session->ticket_len );
2014 p += session->ticket_len;
2015 }
2016 }
2017#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002018 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002019}
2020
2021MBEDTLS_CHECK_RETURN_CRITICAL
2022static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2023 const unsigned char *buf,
2024 size_t len )
2025{
2026 const unsigned char *p = buf;
2027 const unsigned char *end = buf + len;
2028
2029 if( end - p < 9 )
2030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2031 session->endpoint = p[0];
2032 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2033 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2034 session->ticket_flags = p[7];
2035
2036 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002037 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002038 p += 9;
2039
Jerry Yubc7c1a42022-07-21 22:57:37 +08002040 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2042
Jerry Yubc7c1a42022-07-21 22:57:37 +08002043 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002044 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002045 memcpy( session->resumption_key, p, session->resumption_key_len );
2046 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002047
2048#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2049 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2050 {
2051 if( end - p < 8 )
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2054 p += 8;
2055 }
2056#endif /* MBEDTLS_HAVE_TIME */
2057
2058#if defined(MBEDTLS_SSL_CLI_C)
2059 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2060 {
2061#if defined(MBEDTLS_HAVE_TIME)
2062 if( end - p < 8 )
2063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2064 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2065 p += 8;
2066#endif
2067 if( end - p < 4 )
2068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2069 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2070 p += 4;
2071
2072 if( end - p < 2 )
2073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2074 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2075 p += 2;
2076
2077 if( end - p < ( long int )session->ticket_len )
2078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2079 if( session->ticket_len > 0 )
2080 {
2081 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2082 if( session->ticket == NULL )
2083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2084 memcpy( session->ticket, p, session->ticket_len );
2085 p += session->ticket_len;
2086 }
2087 }
2088#endif /* MBEDTLS_SSL_CLI_C */
2089
2090 return( 0 );
2091
2092}
2093#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002094MBEDTLS_CHECK_RETURN_CRITICAL
2095static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2096 unsigned char *buf,
2097 size_t buf_len,
2098 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002099{
2100 ((void) session);
2101 ((void) buf);
2102 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002103 *olen = 0;
2104 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002105}
Jerry Yu438ddd82022-07-07 06:55:50 +00002106
2107static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2108 unsigned char *buf,
2109 size_t buf_len )
2110{
2111 ((void) session);
2112 ((void) buf);
2113 ((void) buf_len);
2114 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2115}
2116#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002117#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2118
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002119psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002120 size_t taglen,
2121 psa_algorithm_t *alg,
2122 psa_key_type_t *key_type,
2123 size_t *key_size )
2124{
2125 switch ( mbedtls_cipher_type )
2126 {
2127 case MBEDTLS_CIPHER_AES_128_CBC:
2128 *alg = PSA_ALG_CBC_NO_PADDING;
2129 *key_type = PSA_KEY_TYPE_AES;
2130 *key_size = 128;
2131 break;
2132 case MBEDTLS_CIPHER_AES_128_CCM:
2133 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2134 *key_type = PSA_KEY_TYPE_AES;
2135 *key_size = 128;
2136 break;
2137 case MBEDTLS_CIPHER_AES_128_GCM:
2138 *alg = PSA_ALG_GCM;
2139 *key_type = PSA_KEY_TYPE_AES;
2140 *key_size = 128;
2141 break;
2142 case MBEDTLS_CIPHER_AES_192_CCM:
2143 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2144 *key_type = PSA_KEY_TYPE_AES;
2145 *key_size = 192;
2146 break;
2147 case MBEDTLS_CIPHER_AES_192_GCM:
2148 *alg = PSA_ALG_GCM;
2149 *key_type = PSA_KEY_TYPE_AES;
2150 *key_size = 192;
2151 break;
2152 case MBEDTLS_CIPHER_AES_256_CBC:
2153 *alg = PSA_ALG_CBC_NO_PADDING;
2154 *key_type = PSA_KEY_TYPE_AES;
2155 *key_size = 256;
2156 break;
2157 case MBEDTLS_CIPHER_AES_256_CCM:
2158 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2159 *key_type = PSA_KEY_TYPE_AES;
2160 *key_size = 256;
2161 break;
2162 case MBEDTLS_CIPHER_AES_256_GCM:
2163 *alg = PSA_ALG_GCM;
2164 *key_type = PSA_KEY_TYPE_AES;
2165 *key_size = 256;
2166 break;
2167 case MBEDTLS_CIPHER_ARIA_128_CBC:
2168 *alg = PSA_ALG_CBC_NO_PADDING;
2169 *key_type = PSA_KEY_TYPE_ARIA;
2170 *key_size = 128;
2171 break;
2172 case MBEDTLS_CIPHER_ARIA_128_CCM:
2173 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2174 *key_type = PSA_KEY_TYPE_ARIA;
2175 *key_size = 128;
2176 break;
2177 case MBEDTLS_CIPHER_ARIA_128_GCM:
2178 *alg = PSA_ALG_GCM;
2179 *key_type = PSA_KEY_TYPE_ARIA;
2180 *key_size = 128;
2181 break;
2182 case MBEDTLS_CIPHER_ARIA_192_CCM:
2183 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2184 *key_type = PSA_KEY_TYPE_ARIA;
2185 *key_size = 192;
2186 break;
2187 case MBEDTLS_CIPHER_ARIA_192_GCM:
2188 *alg = PSA_ALG_GCM;
2189 *key_type = PSA_KEY_TYPE_ARIA;
2190 *key_size = 192;
2191 break;
2192 case MBEDTLS_CIPHER_ARIA_256_CBC:
2193 *alg = PSA_ALG_CBC_NO_PADDING;
2194 *key_type = PSA_KEY_TYPE_ARIA;
2195 *key_size = 256;
2196 break;
2197 case MBEDTLS_CIPHER_ARIA_256_CCM:
2198 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2199 *key_type = PSA_KEY_TYPE_ARIA;
2200 *key_size = 256;
2201 break;
2202 case MBEDTLS_CIPHER_ARIA_256_GCM:
2203 *alg = PSA_ALG_GCM;
2204 *key_type = PSA_KEY_TYPE_ARIA;
2205 *key_size = 256;
2206 break;
2207 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2208 *alg = PSA_ALG_CBC_NO_PADDING;
2209 *key_type = PSA_KEY_TYPE_CAMELLIA;
2210 *key_size = 128;
2211 break;
2212 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2213 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2214 *key_type = PSA_KEY_TYPE_CAMELLIA;
2215 *key_size = 128;
2216 break;
2217 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2218 *alg = PSA_ALG_GCM;
2219 *key_type = PSA_KEY_TYPE_CAMELLIA;
2220 *key_size = 128;
2221 break;
2222 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2223 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2224 *key_type = PSA_KEY_TYPE_CAMELLIA;
2225 *key_size = 192;
2226 break;
2227 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2228 *alg = PSA_ALG_GCM;
2229 *key_type = PSA_KEY_TYPE_CAMELLIA;
2230 *key_size = 192;
2231 break;
2232 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2233 *alg = PSA_ALG_CBC_NO_PADDING;
2234 *key_type = PSA_KEY_TYPE_CAMELLIA;
2235 *key_size = 256;
2236 break;
2237 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2238 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2239 *key_type = PSA_KEY_TYPE_CAMELLIA;
2240 *key_size = 256;
2241 break;
2242 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2243 *alg = PSA_ALG_GCM;
2244 *key_type = PSA_KEY_TYPE_CAMELLIA;
2245 *key_size = 256;
2246 break;
2247 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2248 *alg = PSA_ALG_CHACHA20_POLY1305;
2249 *key_type = PSA_KEY_TYPE_CHACHA20;
2250 *key_size = 256;
2251 break;
2252 case MBEDTLS_CIPHER_NULL:
2253 *alg = MBEDTLS_SSL_NULL_CIPHER;
2254 *key_type = 0;
2255 *key_size = 0;
2256 break;
2257 default:
2258 return PSA_ERROR_NOT_SUPPORTED;
2259 }
2260
2261 return PSA_SUCCESS;
2262}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002263#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002264
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002265#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002266int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2267 const unsigned char *dhm_P, size_t P_len,
2268 const unsigned char *dhm_G, size_t G_len )
2269{
Janos Follath865b3eb2019-12-16 11:46:15 +00002270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002271
Glenn Strausscee11292021-12-20 01:43:17 -05002272 mbedtls_mpi_free( &conf->dhm_P );
2273 mbedtls_mpi_free( &conf->dhm_G );
2274
Hanno Beckera90658f2017-10-04 15:29:08 +01002275 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2276 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2277 {
2278 mbedtls_mpi_free( &conf->dhm_P );
2279 mbedtls_mpi_free( &conf->dhm_G );
2280 return( ret );
2281 }
2282
2283 return( 0 );
2284}
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002286int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002287{
Janos Follath865b3eb2019-12-16 11:46:15 +00002288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002289
Glenn Strausscee11292021-12-20 01:43:17 -05002290 mbedtls_mpi_free( &conf->dhm_P );
2291 mbedtls_mpi_free( &conf->dhm_G );
2292
Gilles Peskinee5702482021-06-11 21:59:08 +02002293 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2294 &conf->dhm_P ) ) != 0 ||
2295 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2296 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002297 {
2298 mbedtls_mpi_free( &conf->dhm_P );
2299 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002300 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002301 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002302
2303 return( 0 );
2304}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002305#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002306
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002307#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2308/*
2309 * Set the minimum length for Diffie-Hellman parameters
2310 */
2311void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2312 unsigned int bitlen )
2313{
2314 conf->dhm_min_bitlen = bitlen;
2315}
2316#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2317
Gilles Peskineeccd8882020-03-10 12:19:08 +01002318#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002319#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002320/*
2321 * Set allowed/preferred hashes for handshake signatures
2322 */
2323void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2324 const int *hashes )
2325{
2326 conf->sig_hashes = hashes;
2327}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002328#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002329
Jerry Yuf017ee42022-01-12 15:49:48 +08002330/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002331void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2332 const uint16_t* sig_algs )
2333{
Jerry Yuf017ee42022-01-12 15:49:48 +08002334#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2335 conf->sig_hashes = NULL;
2336#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2337 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002338}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002339#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002340
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002341#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002342#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002343/*
2344 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002345 *
2346 * mbedtls_ssl_setup() takes the provided list
2347 * and translates it to a list of IANA TLS group identifiers,
2348 * stored in ssl->handshake->group_list.
2349 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002350 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002351void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002352 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002353{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002354 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002355 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002356}
Brett Warrene0edc842021-08-17 09:53:13 +01002357#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002358#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002359
Brett Warrene0edc842021-08-17 09:53:13 +01002360/*
2361 * Set the allowed groups
2362 */
2363void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2364 const uint16_t *group_list )
2365{
2366#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2367 conf->curve_list = NULL;
2368#endif
2369 conf->group_list = group_list;
2370}
2371
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374{
Hanno Becker947194e2017-04-07 13:25:49 +01002375 /* Initialize to suppress unnecessary compiler warning */
2376 size_t hostname_len = 0;
2377
2378 /* Check if new hostname is valid before
2379 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002380 if( hostname != NULL )
2381 {
2382 hostname_len = strlen( hostname );
2383
2384 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2386 }
2387
2388 /* Now it's clear that we will overwrite the old hostname,
2389 * so we can free it safely */
2390
2391 if( ssl->hostname != NULL )
2392 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002393 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002394 mbedtls_free( ssl->hostname );
2395 }
2396
2397 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002398
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002400 {
2401 ssl->hostname = NULL;
2402 }
2403 else
2404 {
2405 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002406 if( ssl->hostname == NULL )
2407 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002408
Hanno Becker947194e2017-04-07 13:25:49 +01002409 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002410
Hanno Becker947194e2017-04-07 13:25:49 +01002411 ssl->hostname[hostname_len] = '\0';
2412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 return( 0 );
2415}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002419void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002421 const unsigned char *, size_t),
2422 void *p_sni )
2423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002424 conf->f_sni = f_sni;
2425 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002430int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002431{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002432 size_t cur_len, tot_len;
2433 const char **p;
2434
2435 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002436 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2437 * MUST NOT be truncated."
2438 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002439 */
2440 tot_len = 0;
2441 for( p = protos; *p != NULL; p++ )
2442 {
2443 cur_len = strlen( *p );
2444 tot_len += cur_len;
2445
Ronald Cron8216dd32020-04-23 16:41:44 +02002446 if( ( cur_len == 0 ) ||
2447 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2448 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002450 }
2451
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002452 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002453
2454 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002455}
2456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002458{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002459 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002462
Johan Pascalb62bb512015-12-03 21:56:45 +01002463#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002464void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2465 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002466{
2467 conf->dtls_srtp_mki_support = support_mki_value;
2468}
2469
Ron Eldoref72faf2018-07-12 11:54:20 +03002470int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2471 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002472 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002473{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002474 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002475 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002477 }
Ron Eldor591f1622018-01-22 12:30:04 +02002478
2479 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002480 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002481 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002482 }
Ron Eldor591f1622018-01-22 12:30:04 +02002483
2484 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2485 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002486 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002487}
2488
Ron Eldoref72faf2018-07-12 11:54:20 +03002489int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002490 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002491{
Johan Pascal253d0262020-09-22 13:04:45 +02002492 const mbedtls_ssl_srtp_profile *p;
2493 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002494
Johan Pascal253d0262020-09-22 13:04:45 +02002495 /* check the profiles list: all entry must be valid,
2496 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002497 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2498 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2499 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002500 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002501 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002502 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002503 list_size++;
2504 }
2505 else
2506 {
2507 /* unsupported value, stop parsing and set the size to an error value */
2508 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002509 }
2510 }
2511
Johan Pascal5ef72d22020-10-28 17:05:47 +01002512 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002513 {
Johan Pascal253d0262020-09-22 13:04:45 +02002514 conf->dtls_srtp_profile_list = NULL;
2515 conf->dtls_srtp_profile_list_len = 0;
2516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2517 }
2518
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002519 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002520 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002521
2522 return( 0 );
2523}
2524
Johan Pascal5ef72d22020-10-28 17:05:47 +01002525void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2526 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002527{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002528 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2529 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002530 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002531 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002532 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002533 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002534 else
2535 {
2536 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002537 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2538 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002539 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002540}
Johan Pascalb62bb512015-12-03 21:56:45 +01002541#endif /* MBEDTLS_SSL_DTLS_SRTP */
2542
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302543#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002544void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002545{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002546 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002547}
2548
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002549void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002550{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002551 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002552}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302553#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002554
Janos Follath088ce432017-04-10 12:42:31 +01002555#if defined(MBEDTLS_SSL_SRV_C)
2556void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2557 char cert_req_ca_list )
2558{
2559 conf->cert_req_ca_list = cert_req_ca_list;
2560}
2561#endif
2562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002564void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002566 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002567}
2568#endif
2569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002571void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002573 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002574}
2575#endif
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002578int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002581 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002584 }
2585
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002586 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002587
2588 return( 0 );
2589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002591
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002592void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002594 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002595}
2596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002598void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002600 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002601}
2602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002603void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002605 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002606}
2607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002608void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002609 const unsigned char period[8] )
2610{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002611 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002616#if defined(MBEDTLS_SSL_CLI_C)
2617void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002618{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002619 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002620}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002621#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002622
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002623#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002624
Jerry Yud0766ec2022-09-22 10:46:57 +08002625#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002626void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2627 uint16_t num_tickets )
2628{
Jerry Yud0766ec2022-09-22 10:46:57 +08002629 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002630}
2631#endif
2632
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002633void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2634 mbedtls_ssl_ticket_write_t *f_ticket_write,
2635 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2636 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002637{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002638 conf->f_ticket_write = f_ticket_write;
2639 conf->f_ticket_parse = f_ticket_parse;
2640 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002641}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002642#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002644
Hanno Becker7e6c1782021-06-08 09:24:55 +01002645void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2646 mbedtls_ssl_export_keys_t *f_export_keys,
2647 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002648{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002649 ssl->f_export_keys = f_export_keys;
2650 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002651}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002652
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002653#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002654void mbedtls_ssl_conf_async_private_cb(
2655 mbedtls_ssl_config *conf,
2656 mbedtls_ssl_async_sign_t *f_async_sign,
2657 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2658 mbedtls_ssl_async_resume_t *f_async_resume,
2659 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002660 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002661{
2662 conf->f_async_sign_start = f_async_sign;
2663 conf->f_async_decrypt_start = f_async_decrypt;
2664 conf->f_async_resume = f_async_resume;
2665 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002666 conf->p_async_config_data = async_config_data;
2667}
2668
Gilles Peskine8f97af72018-04-26 11:46:10 +02002669void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2670{
2671 return( conf->p_async_config_data );
2672}
2673
Gilles Peskine1febfef2018-04-30 11:54:39 +02002674void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002675{
2676 if( ssl->handshake == NULL )
2677 return( NULL );
2678 else
2679 return( ssl->handshake->user_async_ctx );
2680}
2681
Gilles Peskine1febfef2018-04-30 11:54:39 +02002682void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002683 void *ctx )
2684{
2685 if( ssl->handshake != NULL )
2686 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002687}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002688#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002689
Paul Bakker5121ce52009-01-03 21:22:43 +00002690/*
2691 * SSL get accessors
2692 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002693uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002694{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002695 if( ssl->session != NULL )
2696 return( ssl->session->verify_result );
2697
2698 if( ssl->session_negotiate != NULL )
2699 return( ssl->session_negotiate->verify_result );
2700
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002701 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702}
2703
Glenn Strauss8f526902022-01-13 00:04:49 -05002704int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2705{
2706 if( ssl == NULL || ssl->session == NULL )
2707 return( 0 );
2708
2709 return( ssl->session->ciphersuite );
2710}
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002713{
Paul Bakker926c8e42013-03-06 10:23:34 +01002714 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002715 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002718}
2719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002724 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002725 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002726 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002727 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002728 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002729 default:
2730 return( "unknown (DTLS)" );
2731 }
2732 }
2733#endif
2734
Glenn Strauss60bfe602022-03-14 19:04:24 -04002735 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002736 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002737 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002738 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002739 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002740 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002741 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002742 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002743 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002744}
2745
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002746#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002747size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2748{
David Horstmann95d516f2021-05-04 18:36:56 +01002749 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002750 size_t read_mfl;
2751
2752 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2753 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2754 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2755 {
2756 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2757 }
2758
2759 /* Check if a smaller max length was negotiated */
2760 if( ssl->session_out != NULL )
2761 {
2762 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2763 if( read_mfl < max_len )
2764 {
2765 max_len = read_mfl;
2766 }
2767 }
2768
2769 // During a handshake, use the value being negotiated
2770 if( ssl->session_negotiate != NULL )
2771 {
2772 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2773 if( read_mfl < max_len )
2774 {
2775 max_len = read_mfl;
2776 }
2777 }
2778
2779 return( max_len );
2780}
2781
2782size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002783{
2784 size_t max_len;
2785
2786 /*
2787 * Assume mfl_code is correct since it was checked when set
2788 */
Angus Grattond8213d02016-05-25 20:56:48 +10002789 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002790
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002791 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002792 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002793 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002794 {
Angus Grattond8213d02016-05-25 20:56:48 +10002795 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002796 }
2797
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002798 /* During a handshake, use the value being negotiated */
2799 if( ssl->session_negotiate != NULL &&
2800 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2801 {
2802 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2803 }
2804
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002805 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002806}
2807#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2808
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002810size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002811{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002812 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2813 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2814 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2815 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2816 return ( 0 );
2817
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002818 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2819 return( ssl->mtu );
2820
2821 if( ssl->mtu == 0 )
2822 return( ssl->handshake->mtu );
2823
2824 return( ssl->mtu < ssl->handshake->mtu ?
2825 ssl->mtu : ssl->handshake->mtu );
2826}
2827#endif /* MBEDTLS_SSL_PROTO_DTLS */
2828
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002829int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2830{
2831 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2832
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002833#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2834 !defined(MBEDTLS_SSL_PROTO_DTLS)
2835 (void) ssl;
2836#endif
2837
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002838#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002839 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002840
2841 if( max_len > mfl )
2842 max_len = mfl;
2843#endif
2844
2845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002846 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002847 {
Hanno Becker89490712020-02-05 10:50:12 +00002848 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002849 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2850 const size_t overhead = (size_t) ret;
2851
2852 if( ret < 0 )
2853 return( ret );
2854
2855 if( mtu <= overhead )
2856 {
2857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2858 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2859 }
2860
2861 if( max_len > mtu - overhead )
2862 max_len = mtu - overhead;
2863 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002864#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002865
Hanno Becker0defedb2018-08-10 12:35:02 +01002866#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2867 !defined(MBEDTLS_SSL_PROTO_DTLS)
2868 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002869#endif
2870
2871 return( (int) max_len );
2872}
2873
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002874int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2875{
2876 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2877
2878#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2879 (void) ssl;
2880#endif
2881
2882#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2883 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2884
2885 if( max_len > mfl )
2886 max_len = mfl;
2887#endif
2888
2889 return( (int) max_len );
2890}
2891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_X509_CRT_PARSE_C)
2893const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002894{
2895 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002896 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002897
Hanno Beckere6824572019-02-07 13:18:46 +00002898#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002899 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002900#else
2901 return( NULL );
2902#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002907int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2908 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002909{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002910 int ret;
2911
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002912 if( ssl == NULL ||
2913 dst == NULL ||
2914 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002915 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002918 }
2919
Hanno Beckere810bbc2021-05-14 16:01:05 +01002920 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2921 * idempotent: Each session can only be exported once.
2922 *
2923 * (This is in preparation for TLS 1.3 support where we will
2924 * need the ability to export multiple sessions (aka tickets),
2925 * which will be achieved by calling mbedtls_ssl_get_session()
2926 * multiple times until it fails.)
2927 *
2928 * Check whether we have already exported the current session,
2929 * and fail if so.
2930 */
2931 if( ssl->session->exported == 1 )
2932 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2933
2934 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2935 if( ret != 0 )
2936 return( ret );
2937
2938 /* Remember that we've exported the session. */
2939 ssl->session->exported = 1;
2940 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002943
Paul Bakker5121ce52009-01-03 21:22:43 +00002944/*
Hanno Beckera835da52019-05-16 12:39:07 +01002945 * Define ticket header determining Mbed TLS version
2946 * and structure of the ticket.
2947 */
2948
Hanno Becker94ef3b32019-05-16 12:50:45 +01002949/*
Hanno Becker50b59662019-05-28 14:30:45 +01002950 * Define bitflag determining compile-time settings influencing
2951 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002952 */
2953
Hanno Becker50b59662019-05-28 14:30:45 +01002954#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002955#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002956#else
Hanno Becker3e088662019-05-29 11:10:18 +01002957#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002958#endif /* MBEDTLS_HAVE_TIME */
2959
2960#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002961#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002962#else
Hanno Becker3e088662019-05-29 11:10:18 +01002963#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002964#endif /* MBEDTLS_X509_CRT_PARSE_C */
2965
2966#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002967#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002968#else
Hanno Becker3e088662019-05-29 11:10:18 +01002969#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002970#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2971
2972#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002973#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002974#else
Hanno Becker3e088662019-05-29 11:10:18 +01002975#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002976#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2977
Hanno Becker94ef3b32019-05-16 12:50:45 +01002978#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002979#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002980#else
Hanno Becker3e088662019-05-29 11:10:18 +01002981#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002982#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2983
Hanno Becker94ef3b32019-05-16 12:50:45 +01002984#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2985#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2986#else
2987#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2988#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2989
Hanno Becker3e088662019-05-29 11:10:18 +01002990#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
2991#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
2992#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
2993#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01002994#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
2995#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01002996
Hanno Becker50b59662019-05-28 14:30:45 +01002997#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01002998 ( (uint16_t) ( \
2999 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3000 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3001 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3002 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003003 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003004 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003005
Hanno Beckerf8787072019-05-16 12:41:07 +01003006static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003007 MBEDTLS_VERSION_MAJOR,
3008 MBEDTLS_VERSION_MINOR,
3009 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003010 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3011 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003012};
Hanno Beckera835da52019-05-16 12:39:07 +01003013
3014/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003015 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003016 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003017 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003018 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003019 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003020 * opaque mbedtls_version[3]; // library version: major, minor, patch
3021 * opaque session_format[2]; // library-version specific 16-bit field
3022 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003023 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003024 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003025 * Note: When updating the format, remember to keep
3026 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003027 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003028 * // In this version, `session_format` determines
3029 * // the setting of those compile-time
3030 * // configuration options which influence
3031 * // the structure of mbedtls_ssl_session.
3032 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003033 * uint8_t minor_ver; // Protocol minor version. Possible values:
3034 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003035 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003036 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003037 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003038 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003039 * serialized_session_tls12 data;
3040 *
3041 * };
3042 *
3043 * } serialized_session;
3044 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003045 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003046
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003047MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003048static int ssl_session_save( const mbedtls_ssl_session *session,
3049 unsigned char omit_header,
3050 unsigned char *buf,
3051 size_t buf_len,
3052 size_t *olen )
3053{
3054 unsigned char *p = buf;
3055 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003056 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003057#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3058 size_t out_len;
3059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3060#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003061 if( session == NULL )
3062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3063
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003064 if( !omit_header )
3065 {
3066 /*
3067 * Add Mbed TLS version identifier
3068 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003069 used += sizeof( ssl_serialized_session_header );
3070
3071 if( used <= buf_len )
3072 {
3073 memcpy( p, ssl_serialized_session_header,
3074 sizeof( ssl_serialized_session_header ) );
3075 p += sizeof( ssl_serialized_session_header );
3076 }
3077 }
3078
3079 /*
3080 * TLS version identifier
3081 */
3082 used += 1;
3083 if( used <= buf_len )
3084 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003085 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003086 }
3087
3088 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003089 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003090 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003091 {
3092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003093 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003094 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003095 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003096#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3097
Jerry Yu251a12e2022-07-13 15:15:48 +08003098#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3099 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003100 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3101 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003102 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003103 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003104 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003105#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3106
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003107 default:
3108 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3109 }
3110
3111 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003112 if( used > buf_len )
3113 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003114
3115 return( 0 );
3116}
3117
3118/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003119 * Public wrapper for ssl_session_save()
3120 */
3121int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3122 unsigned char *buf,
3123 size_t buf_len,
3124 size_t *olen )
3125{
3126 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3127}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003128
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003129/*
3130 * Deserialize session, see mbedtls_ssl_session_save() for format.
3131 *
3132 * This internal version is wrapped by a public function that cleans up in
3133 * case of error, and has an extra option omit_header.
3134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003135MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003136static int ssl_session_load( mbedtls_ssl_session *session,
3137 unsigned char omit_header,
3138 const unsigned char *buf,
3139 size_t len )
3140{
3141 const unsigned char *p = buf;
3142 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003143 size_t remaining_len;
3144
3145
3146 if( session == NULL )
3147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003148
3149 if( !omit_header )
3150 {
3151 /*
3152 * Check Mbed TLS version identifier
3153 */
3154
3155 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3157
3158 if( memcmp( p, ssl_serialized_session_header,
3159 sizeof( ssl_serialized_session_header ) ) != 0 )
3160 {
3161 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3162 }
3163 p += sizeof( ssl_serialized_session_header );
3164 }
3165
3166 /*
3167 * TLS version identifier
3168 */
3169 if( 1 > (size_t)( end - p ) )
3170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003171 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003172
3173 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003174 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003175 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003176 {
3177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003178 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003179 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003180#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3181
Jerry Yu438ddd82022-07-07 06:55:50 +00003182#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3183 case MBEDTLS_SSL_VERSION_TLS1_3:
3184 return( ssl_tls13_session_load( session, p, remaining_len ) );
3185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3186
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003187 default:
3188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3189 }
3190}
3191
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003192/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003193 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003194 */
3195int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3196 const unsigned char *buf,
3197 size_t len )
3198{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003199 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003200
3201 if( ret != 0 )
3202 mbedtls_ssl_session_free( session );
3203
3204 return( ret );
3205}
3206
3207/*
Paul Bakker1961b702013-01-25 14:49:24 +01003208 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003210MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003211static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3212{
3213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3214
Ronald Cron66dbf912022-02-02 15:33:46 +01003215 /*
3216 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003217 * that were written into the output buffer by the previous handshake step,
3218 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003219 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3220 * We proceed to the next handshake step only when all data from the
3221 * previous one have been sent to the peer, thus we make sure that this is
3222 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3223 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3224 * we have to wait before to go ahead.
3225 * In the case of TLS 1.3, handshake step handlers do not send data to the
3226 * peer. Data are only sent here and through
3227 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003228 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003229 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003230 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3231 return( ret );
3232
3233#if defined(MBEDTLS_SSL_PROTO_DTLS)
3234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3235 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3236 {
3237 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3238 return( ret );
3239 }
3240#endif /* MBEDTLS_SSL_PROTO_DTLS */
3241
3242 return( ret );
3243}
3244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003246{
Hanno Becker41934dd2021-08-07 19:13:43 +01003247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Hanno Becker41934dd2021-08-07 19:13:43 +01003249 if( ssl == NULL ||
3250 ssl->conf == NULL ||
3251 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003252 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003253 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003255 }
3256
3257 ret = ssl_prepare_handshake_step( ssl );
3258 if( ret != 0 )
3259 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003260
Jerry Yue7047812021-09-13 19:26:39 +08003261 ret = mbedtls_ssl_handle_pending_alert( ssl );
3262 if( ret != 0 )
3263 goto cleanup;
3264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003266 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003267 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3269 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003270
Ronald Cron9f0fba32022-02-10 16:45:15 +01003271 switch( ssl->state )
3272 {
3273 case MBEDTLS_SSL_HELLO_REQUEST:
3274 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3275 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003276
Ronald Cron9f0fba32022-02-10 16:45:15 +01003277 case MBEDTLS_SSL_CLIENT_HELLO:
3278 ret = mbedtls_ssl_write_client_hello( ssl );
3279 break;
3280
3281 default:
3282#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003283 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003284 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3285 else
3286 ret = mbedtls_ssl_handshake_client_step( ssl );
3287#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3288 ret = mbedtls_ssl_handshake_client_step( ssl );
3289#else
3290 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3291#endif
3292 }
Jerry Yub9930e72021-08-06 17:11:51 +08003293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003296 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003297 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003298#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003299 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003300 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003301#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003302
3303#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3304 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3305 ret = mbedtls_ssl_handshake_server_step( ssl );
3306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003308#endif
3309
Jerry Yue7047812021-09-13 19:26:39 +08003310 if( ret != 0 )
3311 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003312 /* handshake_step return error. And it is same
3313 * with alert_reason.
3314 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003315 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003316 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003317 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003318 goto cleanup;
3319 }
3320 }
3321
3322cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003323 return( ret );
3324}
3325
3326/*
3327 * Perform the SSL handshake
3328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003330{
3331 int ret = 0;
3332
Hanno Beckera817ea42020-10-20 15:20:23 +01003333 /* Sanity checks */
3334
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003335 if( ssl == NULL || ssl->conf == NULL )
3336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3337
Hanno Beckera817ea42020-10-20 15:20:23 +01003338#if defined(MBEDTLS_SSL_PROTO_DTLS)
3339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3340 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3341 {
3342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3343 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3345 }
3346#endif /* MBEDTLS_SSL_PROTO_DTLS */
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003349
Hanno Beckera817ea42020-10-20 15:20:23 +01003350 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003351 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003354
3355 if( ret != 0 )
3356 break;
3357 }
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
3361 return( ret );
3362}
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_RENEGOTIATION)
3365#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003366/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003367 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003368 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003369MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003371{
Janos Follath865b3eb2019-12-16 11:46:15 +00003372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003375
3376 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3378 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003379
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003380 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003381 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003383 return( ret );
3384 }
3385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003387
3388 return( 0 );
3389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003391
3392/*
3393 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 * - any side: calling mbedtls_ssl_renegotiate(),
3395 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3396 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003397 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003398 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003400 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003401int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003402{
Janos Follath865b3eb2019-12-16 11:46:15 +00003403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003406
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003407 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3408 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003409
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003410 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3411 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003415 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003417 ssl->handshake->out_msg_seq = 1;
3418 else
3419 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003420 }
3421#endif
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3424 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003429 return( ret );
3430 }
3431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003433
3434 return( 0 );
3435}
3436
3437/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003438 * Renegotiate current connection on client,
3439 * or request renegotiation on server
3440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003444
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003445 if( ssl == NULL || ssl->conf == NULL )
3446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003449 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003450 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003451 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003452 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003456
3457 /* Did we already try/start sending HelloRequest? */
3458 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003460
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003461 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003466 /*
3467 * On client, either start the renegotiation process or,
3468 * if already in progress, continue the handshake
3469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003471 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003472 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003474
Hanno Becker40cdaa12020-02-05 10:48:27 +00003475 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003476 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003478 return( ret );
3479 }
3480 }
3481 else
3482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003486 return( ret );
3487 }
3488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003490
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003491 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003494
Gilles Peskine9b562d52018-04-25 20:32:43 +02003495void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003496{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003497 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3498
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003499 if( handshake == NULL )
3500 return;
3501
Brett Warrene0edc842021-08-17 09:53:13 +01003502#if defined(MBEDTLS_ECP_C)
3503#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3504 if ( ssl->handshake->group_list_heap_allocated )
3505 mbedtls_free( (void*) handshake->group_list );
3506 handshake->group_list = NULL;
3507#endif /* MBEDTLS_DEPRECATED_REMOVED */
3508#endif /* MBEDTLS_ECP_C */
3509
Jerry Yuf017ee42022-01-12 15:49:48 +08003510#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3511#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3512 if ( ssl->handshake->sig_algs_heap_allocated )
3513 mbedtls_free( (void*) handshake->sig_algs );
3514 handshake->sig_algs = NULL;
3515#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003516#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3517 if( ssl->handshake->certificate_request_context )
3518 {
3519 mbedtls_free( (void*) handshake->certificate_request_context );
3520 }
3521#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003522#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3523
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003524#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3525 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3526 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003527 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003528 handshake->async_in_progress = 0;
3529 }
3530#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3531
Andrzej Kurek25f27152022-08-17 16:09:31 -04003532#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003533#if defined(MBEDTLS_USE_PSA_CRYPTO)
3534 psa_hash_abort( &handshake->fin_sha256_psa );
3535#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003536 mbedtls_sha256_free( &handshake->fin_sha256 );
3537#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003538#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003539#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003540#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003541 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003542#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003543 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003544#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003545#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547#if defined(MBEDTLS_DHM_C)
3548 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003549#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003550#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003552#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003553#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003554 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003555#if defined(MBEDTLS_SSL_CLI_C)
3556 mbedtls_free( handshake->ecjpake_cache );
3557 handshake->ecjpake_cache = NULL;
3558 handshake->ecjpake_cache_len = 0;
3559#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003560#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003561
Janos Follath4ae5c292016-02-10 11:27:43 +00003562#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3563 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003564 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003566#endif
3567
Gilles Peskineeccd8882020-03-10 12:19:08 +01003568#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003569#if defined(MBEDTLS_USE_PSA_CRYPTO)
3570 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3571 {
3572 /* The maintenance of the external PSK key slot is the
3573 * user's responsibility. */
3574 if( ssl->handshake->psk_opaque_is_internal )
3575 {
3576 psa_destroy_key( ssl->handshake->psk_opaque );
3577 ssl->handshake->psk_opaque_is_internal = 0;
3578 }
3579 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3580 }
Neil Armstronge952a302022-05-03 10:22:14 +02003581#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003582 if( handshake->psk != NULL )
3583 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003584 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003585 mbedtls_free( handshake->psk );
3586 }
Neil Armstronge952a302022-05-03 10:22:14 +02003587#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003588#endif
3589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3591 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003592 /*
3593 * Free only the linked list wrapper, not the keys themselves
3594 * since the belong to the SNI callback
3595 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003596 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003598
Gilles Peskineeccd8882020-03-10 12:19:08 +01003599#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003600 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003601 if( handshake->ecrs_peer_cert != NULL )
3602 {
3603 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3604 mbedtls_free( handshake->ecrs_peer_cert );
3605 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003606#endif
3607
Hanno Becker75173122019-02-06 16:18:31 +00003608#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3609 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3610 mbedtls_pk_free( &handshake->peer_pubkey );
3611#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3612
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003613#if defined(MBEDTLS_SSL_CLI_C) && \
3614 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3615 mbedtls_free( handshake->cookie );
3616#endif /* MBEDTLS_SSL_CLI_C &&
3617 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003618
3619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003620 mbedtls_ssl_flight_free( handshake->flight );
3621 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003622#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003623
Ronald Cronf12b81d2022-03-15 10:42:41 +01003624#if defined(MBEDTLS_ECDH_C) && \
3625 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003626 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003627 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003628#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3629
Ronald Cron6f135e12021-12-08 16:57:54 +01003630#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003631 mbedtls_ssl_transform_free( handshake->transform_handshake );
3632 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003633 mbedtls_free( handshake->transform_earlydata );
3634 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003635#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003636
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003637
3638#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3639 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3640 * processes datagrams and the fact that a datagram is allowed to have
3641 * several records in it, it is possible that the I/O buffers are not
3642 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003643 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3644 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003645#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003646
Jerry Yuba9c7272021-10-30 11:54:10 +08003647 /* mbedtls_platform_zeroize MUST be last one in this function */
3648 mbedtls_platform_zeroize( handshake,
3649 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003650}
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003653{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003654 if( session == NULL )
3655 return;
3656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003658 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003659#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003660
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003661#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003663#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003664
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003665 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003666}
3667
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003668#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003669
3670#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3671#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3672#else
3673#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3674#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3675
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003676#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003677
3678#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3679#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3680#else
3681#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3682#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3683
3684#if defined(MBEDTLS_SSL_ALPN)
3685#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3686#else
3687#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3688#endif /* MBEDTLS_SSL_ALPN */
3689
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3691#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3692#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3693#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3694
3695#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3696 ( (uint32_t) ( \
3697 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3698 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3699 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3700 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3701 0u ) )
3702
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003703static unsigned char ssl_serialized_context_header[] = {
3704 MBEDTLS_VERSION_MAJOR,
3705 MBEDTLS_VERSION_MINOR,
3706 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003707 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3708 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3709 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3710 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3711 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003712};
3713
Paul Bakker5121ce52009-01-03 21:22:43 +00003714/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003715 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003716 *
3717 * The format of the serialized data is:
3718 * (in the presentation language of TLS, RFC 8446 section 3)
3719 *
3720 * // header
3721 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003722 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003723 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003724 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003725 * Note: When updating the format, remember to keep these
3726 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003727 *
3728 * // session sub-structure
3729 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3730 * // transform sub-structure
3731 * uint8 random[64]; // ServerHello.random+ClientHello.random
3732 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3733 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3734 * // fields from ssl_context
3735 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3736 * uint64 in_window_top; // DTLS: last validated record seq_num
3737 * uint64 in_window; // DTLS: bitmask for replay protection
3738 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3739 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3740 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3741 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3742 *
3743 * Note that many fields of the ssl_context or sub-structures are not
3744 * serialized, as they fall in one of the following categories:
3745 *
3746 * 1. forced value (eg in_left must be 0)
3747 * 2. pointer to dynamically-allocated memory (eg session, transform)
3748 * 3. value can be re-derived from other data (eg session keys from MS)
3749 * 4. value was temporary (eg content of input buffer)
3750 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003751 */
3752int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3753 unsigned char *buf,
3754 size_t buf_len,
3755 size_t *olen )
3756{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003757 unsigned char *p = buf;
3758 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003759 size_t session_len;
3760 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003761
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003762 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003763 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3764 * this function's documentation.
3765 *
3766 * These are due to assumptions/limitations in the implementation. Some of
3767 * them are likely to stay (no handshake in progress) some might go away
3768 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003769 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003770 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003771 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003772 {
3773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +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 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003777 {
3778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003780 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003781 /* Double-check that sub-structures are indeed ready */
3782 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003783 {
3784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003786 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003787 /* There must be no pending incoming or outgoing data */
3788 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003789 {
3790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003792 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003793 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003797 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003798 /* Protocol must be DLTS, not TLS */
3799 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003800 {
3801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003803 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003804 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003805 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003806 {
3807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003809 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003810 /* We must be using an AEAD ciphersuite */
3811 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003812 {
3813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003815 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003816 /* Renegotiation must not be enabled */
3817#if defined(MBEDTLS_SSL_RENEGOTIATION)
3818 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003819 {
3820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003822 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003823#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003824
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003825 /*
3826 * Version and format identifier
3827 */
3828 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003829
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003830 if( used <= buf_len )
3831 {
3832 memcpy( p, ssl_serialized_context_header,
3833 sizeof( ssl_serialized_context_header ) );
3834 p += sizeof( ssl_serialized_context_header );
3835 }
3836
3837 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003838 * Session (length + data)
3839 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003840 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003841 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3842 return( ret );
3843
3844 used += 4 + session_len;
3845 if( used <= buf_len )
3846 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003847 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3848 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003849
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003850 ret = ssl_session_save( ssl->session, 1,
3851 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003852 if( ret != 0 )
3853 return( ret );
3854
3855 p += session_len;
3856 }
3857
3858 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003859 * Transform
3860 */
3861 used += sizeof( ssl->transform->randbytes );
3862 if( used <= buf_len )
3863 {
3864 memcpy( p, ssl->transform->randbytes,
3865 sizeof( ssl->transform->randbytes ) );
3866 p += sizeof( ssl->transform->randbytes );
3867 }
3868
3869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3870 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3871 if( used <= buf_len )
3872 {
3873 *p++ = ssl->transform->in_cid_len;
3874 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3875 p += ssl->transform->in_cid_len;
3876
3877 *p++ = ssl->transform->out_cid_len;
3878 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3879 p += ssl->transform->out_cid_len;
3880 }
3881#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3882
3883 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003884 * Saved fields from top-level ssl_context structure
3885 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003886 used += 4;
3887 if( used <= buf_len )
3888 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003889 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3890 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003891 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003892
3893#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3894 used += 16;
3895 if( used <= buf_len )
3896 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003897 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3898 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003899
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003900 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3901 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003902 }
3903#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3904
3905#if defined(MBEDTLS_SSL_PROTO_DTLS)
3906 used += 1;
3907 if( used <= buf_len )
3908 {
3909 *p++ = ssl->disable_datagram_packing;
3910 }
3911#endif /* MBEDTLS_SSL_PROTO_DTLS */
3912
Jerry Yuae0b2e22021-10-08 15:21:19 +08003913 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003914 if( used <= buf_len )
3915 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003916 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3917 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003918 }
3919
3920#if defined(MBEDTLS_SSL_PROTO_DTLS)
3921 used += 2;
3922 if( used <= buf_len )
3923 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003924 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3925 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003926 }
3927#endif /* MBEDTLS_SSL_PROTO_DTLS */
3928
3929#if defined(MBEDTLS_SSL_ALPN)
3930 {
3931 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003932 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003933 : 0;
3934
3935 used += 1 + alpn_len;
3936 if( used <= buf_len )
3937 {
3938 *p++ = alpn_len;
3939
3940 if( ssl->alpn_chosen != NULL )
3941 {
3942 memcpy( p, ssl->alpn_chosen, alpn_len );
3943 p += alpn_len;
3944 }
3945 }
3946 }
3947#endif /* MBEDTLS_SSL_ALPN */
3948
3949 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003950 * Done
3951 */
3952 *olen = used;
3953
3954 if( used > buf_len )
3955 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003956
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003957 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3958
Hanno Becker43aefe22020-02-05 10:44:56 +00003959 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003960}
3961
3962/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003963 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003964 *
3965 * This internal version is wrapped by a public function that cleans up in
3966 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003967 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003968MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003969static int ssl_context_load( mbedtls_ssl_context *ssl,
3970 const unsigned char *buf,
3971 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003972{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003973 const unsigned char *p = buf;
3974 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003975 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003977
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003978 /*
3979 * The context should have been freshly setup or reset.
3980 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003981 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003982 * renegotiating, or if the user mistakenly loaded a session first.)
3983 */
3984 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3985 ssl->session != NULL )
3986 {
3987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3988 }
3989
3990 /*
3991 * We can't check that the config matches the initial one, but we can at
3992 * least check it matches the requirements for serializing.
3993 */
3994 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003995 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
3996 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003997#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02003998 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003999#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004000 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004001 {
4002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4003 }
4004
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004005 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4006
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004007 /*
4008 * Check version identifier
4009 */
4010 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4012
4013 if( memcmp( p, ssl_serialized_context_header,
4014 sizeof( ssl_serialized_context_header ) ) != 0 )
4015 {
4016 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4017 }
4018 p += sizeof( ssl_serialized_context_header );
4019
4020 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004021 * Session
4022 */
4023 if( (size_t)( end - p ) < 4 )
4024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4025
4026 session_len = ( (size_t) p[0] << 24 ) |
4027 ( (size_t) p[1] << 16 ) |
4028 ( (size_t) p[2] << 8 ) |
4029 ( (size_t) p[3] );
4030 p += 4;
4031
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004032 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004033 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004034 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004035 ssl->session_in = ssl->session;
4036 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004037 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004038
4039 if( (size_t)( end - p ) < session_len )
4040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4041
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004042 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004043 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004044 {
4045 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004046 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004047 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004048
4049 p += session_len;
4050
4051 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004052 * Transform
4053 */
4054
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004055 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004056 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004057 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004058 ssl->transform_in = ssl->transform;
4059 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004060 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004061
4062 /* Read random bytes and populate structure */
4063 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004066 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004067 ssl->session->ciphersuite,
4068 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004069#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004070 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004071#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004072 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4073 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004074 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004075 ssl->conf->endpoint,
4076 ssl );
4077 if( ret != 0 )
4078 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004080 p += sizeof( ssl->transform->randbytes );
4081
4082#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4083 /* Read connection IDs and store them */
4084 if( (size_t)( end - p ) < 1 )
4085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4086
4087 ssl->transform->in_cid_len = *p++;
4088
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004089 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4091
4092 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4093 p += ssl->transform->in_cid_len;
4094
4095 ssl->transform->out_cid_len = *p++;
4096
4097 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4099
4100 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4101 p += ssl->transform->out_cid_len;
4102#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4103
4104 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004105 * Saved fields from top-level ssl_context structure
4106 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004107 if( (size_t)( end - p ) < 4 )
4108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4109
4110 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4111 ( (uint32_t) p[1] << 16 ) |
4112 ( (uint32_t) p[2] << 8 ) |
4113 ( (uint32_t) p[3] );
4114 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004115
4116#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4117 if( (size_t)( end - p ) < 16 )
4118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4119
4120 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4121 ( (uint64_t) p[1] << 48 ) |
4122 ( (uint64_t) p[2] << 40 ) |
4123 ( (uint64_t) p[3] << 32 ) |
4124 ( (uint64_t) p[4] << 24 ) |
4125 ( (uint64_t) p[5] << 16 ) |
4126 ( (uint64_t) p[6] << 8 ) |
4127 ( (uint64_t) p[7] );
4128 p += 8;
4129
4130 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4131 ( (uint64_t) p[1] << 48 ) |
4132 ( (uint64_t) p[2] << 40 ) |
4133 ( (uint64_t) p[3] << 32 ) |
4134 ( (uint64_t) p[4] << 24 ) |
4135 ( (uint64_t) p[5] << 16 ) |
4136 ( (uint64_t) p[6] << 8 ) |
4137 ( (uint64_t) p[7] );
4138 p += 8;
4139#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4140
4141#if defined(MBEDTLS_SSL_PROTO_DTLS)
4142 if( (size_t)( end - p ) < 1 )
4143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4144
4145 ssl->disable_datagram_packing = *p++;
4146#endif /* MBEDTLS_SSL_PROTO_DTLS */
4147
Jerry Yud9a94fe2021-09-28 18:58:59 +08004148 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004150 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4151 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004152
4153#if defined(MBEDTLS_SSL_PROTO_DTLS)
4154 if( (size_t)( end - p ) < 2 )
4155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4156
4157 ssl->mtu = ( p[0] << 8 ) | p[1];
4158 p += 2;
4159#endif /* MBEDTLS_SSL_PROTO_DTLS */
4160
4161#if defined(MBEDTLS_SSL_ALPN)
4162 {
4163 uint8_t alpn_len;
4164 const char **cur;
4165
4166 if( (size_t)( end - p ) < 1 )
4167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4168
4169 alpn_len = *p++;
4170
4171 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4172 {
4173 /* alpn_chosen should point to an item in the configured list */
4174 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4175 {
4176 if( strlen( *cur ) == alpn_len &&
4177 memcmp( p, cur, alpn_len ) == 0 )
4178 {
4179 ssl->alpn_chosen = *cur;
4180 break;
4181 }
4182 }
4183 }
4184
4185 /* can only happen on conf mismatch */
4186 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4188
4189 p += alpn_len;
4190 }
4191#endif /* MBEDTLS_SSL_ALPN */
4192
4193 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004194 * Forced fields from top-level ssl_context structure
4195 *
4196 * Most of them already set to the correct value by mbedtls_ssl_init() and
4197 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4198 */
4199 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004200 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004201
Hanno Becker361b10d2019-08-30 10:42:49 +01004202 /* Adjust pointers for header fields of outgoing records to
4203 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004204 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004205
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004206#if defined(MBEDTLS_SSL_PROTO_DTLS)
4207 ssl->in_epoch = 1;
4208#endif
4209
4210 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4211 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004212 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4213 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004214 if( ssl->handshake != NULL )
4215 {
4216 mbedtls_ssl_handshake_free( ssl );
4217 mbedtls_free( ssl->handshake );
4218 ssl->handshake = NULL;
4219 }
4220
4221 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004222 * Done - should have consumed entire buffer
4223 */
4224 if( p != end )
4225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004226
4227 return( 0 );
4228}
4229
4230/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004231 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004232 */
4233int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4234 const unsigned char *buf,
4235 size_t len )
4236{
4237 int ret = ssl_context_load( context, buf, len );
4238
4239 if( ret != 0 )
4240 mbedtls_ssl_free( context );
4241
4242 return( ret );
4243}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004244#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004245
4246/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 * Free an SSL context
4248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004250{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004251 if( ssl == NULL )
4252 return;
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004255
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004256 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 {
sander-visserb8aa2072020-05-06 22:05:13 +02004258#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4259 size_t out_buf_len = ssl->out_buf_len;
4260#else
4261 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4262#endif
4263
Darryl Greenb33cc762019-11-28 14:29:44 +00004264 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004266 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 }
4268
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004269 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 {
sander-visserb8aa2072020-05-06 22:05:13 +02004271#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4272 size_t in_buf_len = ssl->in_buf_len;
4273#else
4274 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4275#endif
4276
Darryl Greenb33cc762019-11-28 14:29:44 +00004277 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004279 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 }
4281
Paul Bakker48916f92012-09-16 19:57:18 +00004282 if( ssl->transform )
4283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 mbedtls_ssl_transform_free( ssl->transform );
4285 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004286 }
4287
4288 if( ssl->handshake )
4289 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004290 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4292 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 mbedtls_free( ssl->handshake );
4295 mbedtls_free( ssl->transform_negotiate );
4296 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004297 }
4298
Ronald Cron6f135e12021-12-08 16:57:54 +01004299#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004300 mbedtls_ssl_transform_free( ssl->transform_application );
4301 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004302#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004303
Paul Bakkerc0463502013-02-14 11:19:38 +01004304 if( ssl->session )
4305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 mbedtls_ssl_session_free( ssl->session );
4307 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004308 }
4309
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004310#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004311 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004313 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004317
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004318#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004320#endif
4321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004323
Paul Bakker86f04f42013-02-14 11:20:09 +01004324 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004325 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004326}
4327
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004328/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004329 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004330 */
4331void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4332{
4333 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4334}
4335
Gilles Peskineae270bf2021-06-02 00:05:29 +02004336/* The selection should be the same as mbedtls_x509_crt_profile_default in
4337 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004338 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004339 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004340 * about this list.
4341 */
Brett Warrene0edc842021-08-17 09:53:13 +01004342static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004343#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004344 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004345#endif
4346#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004347 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004348#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004349#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004350 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004351#endif
4352#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004353 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004354#endif
4355#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004356 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004357#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004358#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004359 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004360#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004361#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004362 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004363#endif
4364#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004365 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004366#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004367 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004368};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004369
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004370static int ssl_preset_suiteb_ciphersuites[] = {
4371 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4372 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4373 0
4374};
4375
Gilles Peskineeccd8882020-03-10 12:19:08 +01004376#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004377
Jerry Yu909df7b2022-01-22 11:56:27 +08004378/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004379 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004380 * rules SHOULD be upheld.
4381 * - No duplicate entries.
4382 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004383 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004384 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004385 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004386static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004387
Andrzej Kurek25f27152022-08-17 16:09:31 -04004388#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 +08004389 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4390 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004391#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004392 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004393
Andrzej Kurek25f27152022-08-17 16:09:31 -04004394#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 +08004395 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4396 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004397#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004398 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4399
Andrzej Kurek25f27152022-08-17 16:09:31 -04004400#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 +08004401 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4402 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004403#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004404 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004405
Andrzej Kurek25f27152022-08-17 16:09:31 -04004406#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 +08004407 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004408#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 +08004409
Andrzej Kurek25f27152022-08-17 16:09:31 -04004410#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 +08004411 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004412#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 +08004413
Andrzej Kurek25f27152022-08-17 16:09:31 -04004414#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 +08004415 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004416#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 +08004417
Andrzej Kurek25f27152022-08-17 16:09:31 -04004418#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 +08004419 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4420#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4421
Andrzej Kurek25f27152022-08-17 16:09:31 -04004422#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 +08004423 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4424#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4425
Andrzej Kurek25f27152022-08-17 16:09:31 -04004426#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 +08004427 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4428#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4429
Gabor Mezei15b95a62022-05-09 16:37:58 +02004430 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004431};
4432
4433/* NOTICE: see above */
4434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4435static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004436#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004437#if defined(MBEDTLS_ECDSA_C)
4438 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004439#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004440#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4441 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4442#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004443#if defined(MBEDTLS_RSA_C)
4444 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4445#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004446#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004447#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004448#if defined(MBEDTLS_ECDSA_C)
4449 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004450#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004451#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4452 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4453#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004454#if defined(MBEDTLS_RSA_C)
4455 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4456#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004457#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004458#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004459#if defined(MBEDTLS_ECDSA_C)
4460 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004461#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004462#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4463 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4464#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004465#if defined(MBEDTLS_RSA_C)
4466 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4467#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004468#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004469 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004470};
4471#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4472/* NOTICE: see above */
4473static uint16_t ssl_preset_suiteb_sig_algs[] = {
4474
Andrzej Kurek25f27152022-08-17 16:09:31 -04004475#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 +08004476 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4477 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004478#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004479 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4480
Andrzej Kurek25f27152022-08-17 16:09:31 -04004481#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 +08004482 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4483 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004484#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004485 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004486
Andrzej Kurek25f27152022-08-17 16:09:31 -04004487#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 +08004488 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004489#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08004490
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#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 +08004492 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004493#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004494
Gabor Mezei15b95a62022-05-09 16:37:58 +02004495 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004496};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004497
Jerry Yu909df7b2022-01-22 11:56:27 +08004498/* NOTICE: see above */
4499#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4500static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004501#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004502#if defined(MBEDTLS_ECDSA_C)
4503 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004504#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004505#if defined(MBEDTLS_RSA_C)
4506 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4507#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004508#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004509#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004510#if defined(MBEDTLS_ECDSA_C)
4511 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004512#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004513#if defined(MBEDTLS_RSA_C)
4514 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4515#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004516#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004517 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004518};
Jerry Yu909df7b2022-01-22 11:56:27 +08004519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4520
Jerry Yu1a8b4812022-01-20 17:56:50 +08004521#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004522
Brett Warrene0edc842021-08-17 09:53:13 +01004523static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004524#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004525 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004526#endif
4527#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004528 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004529#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004530 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004531};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004532
Jerry Yu1a8b4812022-01-20 17:56:50 +08004533#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004534/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004535 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004536MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004537static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004538{
4539 size_t i, j;
4540 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004541
Gabor Mezei15b95a62022-05-09 16:37:58 +02004542 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004543 {
Jerry Yuf377d642022-01-25 10:43:59 +08004544 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004545 {
Jerry Yuf377d642022-01-25 10:43:59 +08004546 if( sig_algs[i] != sig_algs[j] )
4547 continue;
4548 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4549 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4550 sig_algs[i], j, i );
4551 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004552 }
4553 }
4554 return( ret );
4555}
4556
4557#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4558
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004559/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004560 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004561 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004562int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004563 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004564{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004565#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004567#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004568
Jerry Yu1a8b4812022-01-20 17:56:50 +08004569#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004570 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004571 {
4572 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4573 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4574 }
4575
Jerry Yuf377d642022-01-25 10:43:59 +08004576 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004577 {
4578 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4579 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4580 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004581
4582#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004583 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004584 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004585 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004586 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4587 }
4588
Jerry Yuf377d642022-01-25 10:43:59 +08004589 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004590 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004591 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004592 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4593 }
4594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004595#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4596
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004597 /* Use the functions here so that they are covered in tests,
4598 * but otherwise access member directly for efficiency */
4599 mbedtls_ssl_conf_endpoint( conf, endpoint );
4600 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004601
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004602 /*
4603 * Things that are common to all presets
4604 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004605#if defined(MBEDTLS_SSL_CLI_C)
4606 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4607 {
4608 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4609#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4610 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4611#endif
4612 }
4613#endif
4614
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004615#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4616 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4617#endif
4618
4619#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4620 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4621#endif
4622
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004623#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004624 conf->f_cookie_write = ssl_cookie_write_dummy;
4625 conf->f_cookie_check = ssl_cookie_check_dummy;
4626#endif
4627
4628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4629 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4630#endif
4631
Janos Follath088ce432017-04-10 12:42:31 +01004632#if defined(MBEDTLS_SSL_SRV_C)
4633 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004634 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004635#endif
4636
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004637#if defined(MBEDTLS_SSL_PROTO_DTLS)
4638 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4639 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4640#endif
4641
4642#if defined(MBEDTLS_SSL_RENEGOTIATION)
4643 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004644 memset( conf->renego_period, 0x00, 2 );
4645 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004646#endif
4647
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004648#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004649 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4650 {
4651 const unsigned char dhm_p[] =
4652 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4653 const unsigned char dhm_g[] =
4654 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004655
Hanno Beckere2defad2021-07-24 05:59:17 +01004656 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4657 dhm_p, sizeof( dhm_p ),
4658 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4659 {
4660 return( ret );
4661 }
4662 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004663#endif
4664
Ronald Cron6f135e12021-12-08 16:57:54 +01004665#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004666#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004667 mbedtls_ssl_conf_new_session_tickets(
4668 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4669#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004670 /*
4671 * Allow all TLS 1.3 key exchange modes by default.
4672 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004673 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004675
XiaokangQian4d3a6042022-04-21 13:46:17 +00004676 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4677 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004679 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004680 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004681#else
XiaokangQian060d8672022-04-21 09:24:56 +00004682 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004683#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004684 }
4685 else
4686 {
4687#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4688 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4689 {
4690 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4691 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4692 }
4693 else
4694 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4695 {
4696 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4697 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4698 }
4699#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4700 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4701 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4702#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4703 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4704 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4705#else
4706 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4707#endif
4708 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004709
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004710 /*
4711 * Preset-specific defaults
4712 */
4713 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004714 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004715 /*
4716 * NSA Suite B
4717 */
4718 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004719
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004720 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004721
4722#if defined(MBEDTLS_X509_CRT_PARSE_C)
4723 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004724#endif
4725
Gilles Peskineeccd8882020-03-10 12:19:08 +01004726#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4728 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4729 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4730 else
4731#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4732 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004733#endif
4734
Brett Warrene0edc842021-08-17 09:53:13 +01004735#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4736 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004737#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004738 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004739 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004740
4741 /*
4742 * Default
4743 */
4744 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004745
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004746 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004747
4748#if defined(MBEDTLS_X509_CRT_PARSE_C)
4749 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4750#endif
4751
Gilles Peskineeccd8882020-03-10 12:19:08 +01004752#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004753#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4754 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4755 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4756 else
4757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4758 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004759#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004760
Brett Warrene0edc842021-08-17 09:53:13 +01004761#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4762 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004763#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004764 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004765
4766#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4767 conf->dhm_min_bitlen = 1024;
4768#endif
4769 }
4770
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004771 return( 0 );
4772}
4773
4774/*
4775 * Free mbedtls_ssl_config
4776 */
4777void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4778{
4779#if defined(MBEDTLS_DHM_C)
4780 mbedtls_mpi_free( &conf->dhm_P );
4781 mbedtls_mpi_free( &conf->dhm_G );
4782#endif
4783
Gilles Peskineeccd8882020-03-10 12:19:08 +01004784#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004785#if defined(MBEDTLS_USE_PSA_CRYPTO)
4786 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4787 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004788 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4789 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004790#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004791 if( conf->psk != NULL )
4792 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004793 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004794 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004795 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004796 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004797 }
4798
4799 if( conf->psk_identity != NULL )
4800 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004801 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004802 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004803 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004804 conf->psk_identity_len = 0;
4805 }
4806#endif
4807
4808#if defined(MBEDTLS_X509_CRT_PARSE_C)
4809 ssl_key_cert_free( conf->key_cert );
4810#endif
4811
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004812 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004813}
4814
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004815#if defined(MBEDTLS_PK_C) && \
4816 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004817/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#if defined(MBEDTLS_RSA_C)
4823 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4824 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004825#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826#if defined(MBEDTLS_ECDSA_C)
4827 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4828 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004831}
4832
Hanno Becker7e5437a2017-04-28 17:15:26 +01004833unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4834{
4835 switch( type ) {
4836 case MBEDTLS_PK_RSA:
4837 return( MBEDTLS_SSL_SIG_RSA );
4838 case MBEDTLS_PK_ECDSA:
4839 case MBEDTLS_PK_ECKEY:
4840 return( MBEDTLS_SSL_SIG_ECDSA );
4841 default:
4842 return( MBEDTLS_SSL_SIG_ANON );
4843 }
4844}
4845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004847{
4848 switch( sig )
4849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850#if defined(MBEDTLS_RSA_C)
4851 case MBEDTLS_SSL_SIG_RSA:
4852 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854#if defined(MBEDTLS_ECDSA_C)
4855 case MBEDTLS_SSL_SIG_ECDSA:
4856 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004857#endif
4858 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004860 }
4861}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004862#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004863
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004864/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004865 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004868{
4869 switch( hash )
4870 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004871#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872 case MBEDTLS_SSL_HASH_MD5:
4873 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004874#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004875#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 case MBEDTLS_SSL_HASH_SHA1:
4877 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004878#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880 case MBEDTLS_SSL_HASH_SHA224:
4881 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004882#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004883#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884 case MBEDTLS_SSL_HASH_SHA256:
4885 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004886#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004887#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 case MBEDTLS_SSL_HASH_SHA384:
4889 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004891#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 case MBEDTLS_SSL_HASH_SHA512:
4893 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004894#endif
4895 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004897 }
4898}
4899
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004900/*
4901 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4902 */
4903unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4904{
4905 switch( md )
4906 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004907#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004908 case MBEDTLS_MD_MD5:
4909 return( MBEDTLS_SSL_HASH_MD5 );
4910#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004911#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004912 case MBEDTLS_MD_SHA1:
4913 return( MBEDTLS_SSL_HASH_SHA1 );
4914#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004915#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004916 case MBEDTLS_MD_SHA224:
4917 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004918#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004919#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004920 case MBEDTLS_MD_SHA256:
4921 return( MBEDTLS_SSL_HASH_SHA256 );
4922#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004923#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004924 case MBEDTLS_MD_SHA384:
4925 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004926#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004927#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004928 case MBEDTLS_MD_SHA512:
4929 return( MBEDTLS_SSL_HASH_SHA512 );
4930#endif
4931 default:
4932 return( MBEDTLS_SSL_HASH_NONE );
4933 }
4934}
4935
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004936/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004937 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004938 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004939 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004940int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004941{
Brett Warrene0edc842021-08-17 09:53:13 +01004942 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004943
Brett Warrene0edc842021-08-17 09:53:13 +01004944 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004945 return( -1 );
4946
Brett Warrene0edc842021-08-17 09:53:13 +01004947 for( ; *group_list != 0; group_list++ )
4948 {
4949 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004950 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004951 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004952
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004953 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004954}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004955
4956#if defined(MBEDTLS_ECP_C)
4957/*
4958 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4959 */
4960int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4961{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004962 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004963 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004964
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004965 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004966 return -1;
4967
4968 uint16_t tls_id = grp_info->tls_id;
4969
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004970 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4971}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004972#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974#if defined(MBEDTLS_X509_CRT_PARSE_C)
4975int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4976 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004977 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004978 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004979{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004980 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004981 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004982 const char *ext_oid;
4983 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004986 {
4987 /* Server part of the key exchange */
4988 switch( ciphersuite->key_exchange )
4989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 case MBEDTLS_KEY_EXCHANGE_RSA:
4991 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004992 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004993 break;
4994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
4996 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
4997 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
4998 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004999 break;
5000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5002 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005003 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005004 break;
5005
5006 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 case MBEDTLS_KEY_EXCHANGE_NONE:
5008 case MBEDTLS_KEY_EXCHANGE_PSK:
5009 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5010 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005011 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005012 usage = 0;
5013 }
5014 }
5015 else
5016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5018 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005019 }
5020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005022 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005023 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005024 ret = -1;
5025 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5030 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005031 }
5032 else
5033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5035 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005036 }
5037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005039 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005040 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005041 ret = -1;
5042 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005043
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005044 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005047
Jerry Yu148165c2021-09-24 23:20:59 +08005048#if defined(MBEDTLS_USE_PSA_CRYPTO)
5049int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5050 const mbedtls_md_type_t md,
5051 unsigned char *dst,
5052 size_t dst_len,
5053 size_t *olen )
5054{
Ronald Cronf6893e12022-01-07 22:09:01 +01005055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5056 psa_hash_operation_t *hash_operation_to_clone;
5057 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5058
Jerry Yu148165c2021-09-24 23:20:59 +08005059 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005060
5061 switch( md )
5062 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005063#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005064 case MBEDTLS_MD_SHA384:
5065 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5066 break;
5067#endif
5068
Andrzej Kurek25f27152022-08-17 16:09:31 -04005069#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005070 case MBEDTLS_MD_SHA256:
5071 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5072 break;
5073#endif
5074
5075 default:
5076 goto exit;
5077 }
5078
5079 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5080 if( status != PSA_SUCCESS )
5081 goto exit;
5082
5083 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5084 if( status != PSA_SUCCESS )
5085 goto exit;
5086
5087exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005088 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005089}
5090#else /* MBEDTLS_USE_PSA_CRYPTO */
5091
Andrzej Kurek25f27152022-08-17 16:09:31 -04005092#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005093MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005094static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5095 unsigned char *dst,
5096 size_t dst_len,
5097 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005098{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005099 int ret;
5100 mbedtls_sha512_context sha512;
5101
5102 if( dst_len < 48 )
5103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5104
5105 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005106 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005107
5108 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5109 {
5110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5111 goto exit;
5112 }
5113
5114 *olen = 48;
5115
5116exit:
5117
5118 mbedtls_sha512_free( &sha512 );
5119 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005120}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005121#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005122
Andrzej Kurek25f27152022-08-17 16:09:31 -04005123#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005124MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005125static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5126 unsigned char *dst,
5127 size_t dst_len,
5128 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005129{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005130 int ret;
5131 mbedtls_sha256_context sha256;
5132
5133 if( dst_len < 32 )
5134 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5135
5136 mbedtls_sha256_init( &sha256 );
5137 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005138
Jerry Yu24c0ec32021-09-09 14:21:07 +08005139 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5140 {
5141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5142 goto exit;
5143 }
5144
5145 *olen = 32;
5146
5147exit:
5148
5149 mbedtls_sha256_free( &sha256 );
5150 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005151}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005152#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005153
Jerry Yu000f9762021-09-14 11:12:51 +08005154int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5155 const mbedtls_md_type_t md,
5156 unsigned char *dst,
5157 size_t dst_len,
5158 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005159{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005160 switch( md )
5161 {
5162
Andrzej Kurek25f27152022-08-17 16:09:31 -04005163#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005164 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005165 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005166#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005167
Andrzej Kurek25f27152022-08-17 16:09:31 -04005168#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005169 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005170 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005171#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005172
5173 default:
5174 break;
5175 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005177}
XiaokangQian647719a2021-12-07 09:16:29 +00005178
Jerry Yu148165c2021-09-24 23:20:59 +08005179#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005180
Gabor Mezei078e8032022-04-27 21:17:56 +02005181#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5182/* mbedtls_ssl_parse_sig_alg_ext()
5183 *
5184 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5185 * value (TLS 1.3 RFC8446):
5186 * enum {
5187 * ....
5188 * ecdsa_secp256r1_sha256( 0x0403 ),
5189 * ecdsa_secp384r1_sha384( 0x0503 ),
5190 * ecdsa_secp521r1_sha512( 0x0603 ),
5191 * ....
5192 * } SignatureScheme;
5193 *
5194 * struct {
5195 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5196 * } SignatureSchemeList;
5197 *
5198 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5199 * value (TLS 1.2 RFC5246):
5200 * enum {
5201 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5202 * sha512(6), (255)
5203 * } HashAlgorithm;
5204 *
5205 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5206 * SignatureAlgorithm;
5207 *
5208 * struct {
5209 * HashAlgorithm hash;
5210 * SignatureAlgorithm signature;
5211 * } SignatureAndHashAlgorithm;
5212 *
5213 * SignatureAndHashAlgorithm
5214 * supported_signature_algorithms<2..2^16-2>;
5215 *
5216 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5217 * generalization of the TLS 1.2 signature algorithm extension.
5218 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5219 * `SignatureScheme` field of TLS 1.3
5220 *
5221 */
5222int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5223 const unsigned char *buf,
5224 const unsigned char *end )
5225{
5226 const unsigned char *p = buf;
5227 size_t supported_sig_algs_len = 0;
5228 const unsigned char *supported_sig_algs_end;
5229 uint16_t sig_alg;
5230 uint32_t common_idx = 0;
5231
5232 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5233 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5234 p += 2;
5235
5236 memset( ssl->handshake->received_sig_algs, 0,
5237 sizeof(ssl->handshake->received_sig_algs) );
5238
5239 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5240 supported_sig_algs_end = p + supported_sig_algs_len;
5241 while( p < supported_sig_algs_end )
5242 {
5243 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5244 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5245 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005246 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5247 sig_alg,
5248 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005250 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005251 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5252 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5253 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005254 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005255 }
5256#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005257
Jerry Yuf3b46b52022-06-19 16:52:27 +08005258 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5259 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005260
5261 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5262 {
5263 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5264 common_idx += 1;
5265 }
5266 }
5267 /* Check that we consumed all the message. */
5268 if( p != end )
5269 {
5270 MBEDTLS_SSL_DEBUG_MSG( 1,
5271 ( "Signature algorithms extension length misaligned" ) );
5272 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5273 MBEDTLS_ERR_SSL_DECODE_ERROR );
5274 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5275 }
5276
5277 if( common_idx == 0 )
5278 {
5279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5280 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5281 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5282 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5283 }
5284
Gabor Mezei15b95a62022-05-09 16:37:58 +02005285 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005286 return( 0 );
5287}
5288
5289#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5290
Jerry Yudc7bd172022-02-17 13:44:15 +08005291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5292
5293#if defined(MBEDTLS_USE_PSA_CRYPTO)
5294
5295static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5296 mbedtls_svc_key_id_t key,
5297 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005298 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005299 const unsigned char* seed, size_t seed_length,
5300 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005301 const unsigned char* other_secret,
5302 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005303 size_t capacity )
5304{
5305 psa_status_t status;
5306
5307 status = psa_key_derivation_setup( derivation, alg );
5308 if( status != PSA_SUCCESS )
5309 return( status );
5310
5311 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5312 {
5313 status = psa_key_derivation_input_bytes( derivation,
5314 PSA_KEY_DERIVATION_INPUT_SEED,
5315 seed, seed_length );
5316 if( status != PSA_SUCCESS )
5317 return( status );
5318
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005319 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005320 {
5321 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005322 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5323 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005324 if( status != PSA_SUCCESS )
5325 return( status );
5326 }
5327
Jerry Yudc7bd172022-02-17 13:44:15 +08005328 if( mbedtls_svc_key_id_is_null( key ) )
5329 {
5330 status = psa_key_derivation_input_bytes(
5331 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005332 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005333 }
5334 else
5335 {
5336 status = psa_key_derivation_input_key(
5337 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5338 }
5339 if( status != PSA_SUCCESS )
5340 return( status );
5341
5342 status = psa_key_derivation_input_bytes( derivation,
5343 PSA_KEY_DERIVATION_INPUT_LABEL,
5344 label, label_length );
5345 if( status != PSA_SUCCESS )
5346 return( status );
5347 }
5348 else
5349 {
5350 return( PSA_ERROR_NOT_SUPPORTED );
5351 }
5352
5353 status = psa_key_derivation_set_capacity( derivation, capacity );
5354 if( status != PSA_SUCCESS )
5355 return( status );
5356
5357 return( PSA_SUCCESS );
5358}
5359
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005360MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005361static int tls_prf_generic( mbedtls_md_type_t md_type,
5362 const unsigned char *secret, size_t slen,
5363 const char *label,
5364 const unsigned char *random, size_t rlen,
5365 unsigned char *dstbuf, size_t dlen )
5366{
5367 psa_status_t status;
5368 psa_algorithm_t alg;
5369 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5370 psa_key_derivation_operation_t derivation =
5371 PSA_KEY_DERIVATION_OPERATION_INIT;
5372
5373 if( md_type == MBEDTLS_MD_SHA384 )
5374 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5375 else
5376 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5377
5378 /* Normally a "secret" should be long enough to be impossible to
5379 * find by brute force, and in particular should not be empty. But
5380 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5381 * and for this use case it makes sense to have a 0-length "secret".
5382 * Since the key API doesn't allow importing a key of length 0,
5383 * keep master_key=0, which setup_psa_key_derivation() understands
5384 * to mean a 0-length "secret" input. */
5385 if( slen != 0 )
5386 {
5387 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5388 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5389 psa_set_key_algorithm( &key_attributes, alg );
5390 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5391
5392 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5393 if( status != PSA_SUCCESS )
5394 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5395 }
5396
5397 status = setup_psa_key_derivation( &derivation,
5398 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005399 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005400 random, rlen,
5401 (unsigned char const *) label,
5402 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005403 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005404 dlen );
5405 if( status != PSA_SUCCESS )
5406 {
5407 psa_key_derivation_abort( &derivation );
5408 psa_destroy_key( master_key );
5409 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5410 }
5411
5412 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5413 if( status != PSA_SUCCESS )
5414 {
5415 psa_key_derivation_abort( &derivation );
5416 psa_destroy_key( master_key );
5417 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5418 }
5419
5420 status = psa_key_derivation_abort( &derivation );
5421 if( status != PSA_SUCCESS )
5422 {
5423 psa_destroy_key( master_key );
5424 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5425 }
5426
5427 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5428 status = psa_destroy_key( master_key );
5429 if( status != PSA_SUCCESS )
5430 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5431
5432 return( 0 );
5433}
5434
5435#else /* MBEDTLS_USE_PSA_CRYPTO */
5436
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005437MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005438static int tls_prf_generic( mbedtls_md_type_t md_type,
5439 const unsigned char *secret, size_t slen,
5440 const char *label,
5441 const unsigned char *random, size_t rlen,
5442 unsigned char *dstbuf, size_t dlen )
5443{
5444 size_t nb;
5445 size_t i, j, k, md_len;
5446 unsigned char *tmp;
5447 size_t tmp_len = 0;
5448 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5449 const mbedtls_md_info_t *md_info;
5450 mbedtls_md_context_t md_ctx;
5451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5452
5453 mbedtls_md_init( &md_ctx );
5454
5455 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5457
5458 md_len = mbedtls_md_get_size( md_info );
5459
5460 tmp_len = md_len + strlen( label ) + rlen;
5461 tmp = mbedtls_calloc( 1, tmp_len );
5462 if( tmp == NULL )
5463 {
5464 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5465 goto exit;
5466 }
5467
5468 nb = strlen( label );
5469 memcpy( tmp + md_len, label, nb );
5470 memcpy( tmp + md_len + nb, random, rlen );
5471 nb += rlen;
5472
5473 /*
5474 * Compute P_<hash>(secret, label + random)[0..dlen]
5475 */
5476 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5477 goto exit;
5478
5479 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5480 if( ret != 0 )
5481 goto exit;
5482 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5483 if( ret != 0 )
5484 goto exit;
5485 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5486 if( ret != 0 )
5487 goto exit;
5488
5489 for( i = 0; i < dlen; i += md_len )
5490 {
5491 ret = mbedtls_md_hmac_reset ( &md_ctx );
5492 if( ret != 0 )
5493 goto exit;
5494 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5495 if( ret != 0 )
5496 goto exit;
5497 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5498 if( ret != 0 )
5499 goto exit;
5500
5501 ret = mbedtls_md_hmac_reset ( &md_ctx );
5502 if( ret != 0 )
5503 goto exit;
5504 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5505 if( ret != 0 )
5506 goto exit;
5507 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5508 if( ret != 0 )
5509 goto exit;
5510
5511 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5512
5513 for( j = 0; j < k; j++ )
5514 dstbuf[i + j] = h_i[j];
5515 }
5516
5517exit:
5518 mbedtls_md_free( &md_ctx );
5519
5520 mbedtls_platform_zeroize( tmp, tmp_len );
5521 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5522
5523 mbedtls_free( tmp );
5524
5525 return( ret );
5526}
5527#endif /* MBEDTLS_USE_PSA_CRYPTO */
5528
Andrzej Kurek25f27152022-08-17 16:09:31 -04005529#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005530MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005531static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5532 const char *label,
5533 const unsigned char *random, size_t rlen,
5534 unsigned char *dstbuf, size_t dlen )
5535{
5536 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5537 label, random, rlen, dstbuf, dlen ) );
5538}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005539#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005540
Andrzej Kurek25f27152022-08-17 16:09:31 -04005541#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005542MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005543static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5544 const char *label,
5545 const unsigned char *random, size_t rlen,
5546 unsigned char *dstbuf, size_t dlen )
5547{
5548 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5549 label, random, rlen, dstbuf, dlen ) );
5550}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005551#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005552
Jerry Yuf009d862022-02-17 14:01:37 +08005553/*
5554 * Set appropriate PRF function and other SSL / TLS1.2 functions
5555 *
5556 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005557 * - hash associated with the ciphersuite (only used by TLS 1.2)
5558 *
5559 * Outputs:
5560 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5561 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005562MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005563static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005564 mbedtls_md_type_t hash )
5565{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005566#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005567 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005568 {
5569 handshake->tls_prf = tls_prf_sha384;
5570 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5571 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5572 }
5573 else
5574#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005575#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005576 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005577 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005578 handshake->tls_prf = tls_prf_sha256;
5579 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5580 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5581 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005582#else
Jerry Yuf009d862022-02-17 14:01:37 +08005583 {
Jerry Yuf009d862022-02-17 14:01:37 +08005584 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005585 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5587 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005588#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005589
5590 return( 0 );
5591}
Jerry Yud6ab2352022-02-17 14:03:43 +08005592
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005593/*
5594 * Compute master secret if needed
5595 *
5596 * Parameters:
5597 * [in/out] handshake
5598 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5599 * (PSA-PSK) ciphersuite_info, psk_opaque
5600 * [out] premaster (cleared)
5601 * [out] master
5602 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5603 * debug: conf->f_dbg, conf->p_dbg
5604 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005605 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005606 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005607MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005608static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5609 unsigned char *master,
5610 const mbedtls_ssl_context *ssl )
5611{
5612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5613
5614 /* cf. RFC 5246, Section 8.1:
5615 * "The master secret is always exactly 48 bytes in length." */
5616 size_t const master_secret_len = 48;
5617
5618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5619 unsigned char session_hash[48];
5620#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5621
5622 /* The label for the KDF used for key expansion.
5623 * This is either "master secret" or "extended master secret"
5624 * depending on whether the Extended Master Secret extension
5625 * is used. */
5626 char const *lbl = "master secret";
5627
Przemek Stekielae4ed302022-04-05 17:15:55 +02005628 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005629 * - If the Extended Master Secret extension is not used,
5630 * this is ClientHello.Random + ServerHello.Random
5631 * (see Sect. 8.1 in RFC 5246).
5632 * - If the Extended Master Secret extension is used,
5633 * this is the transcript of the handshake so far.
5634 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005635 unsigned char const *seed = handshake->randbytes;
5636 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005637
5638#if !defined(MBEDTLS_DEBUG_C) && \
5639 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5640 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5641 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5642 ssl = NULL; /* make sure we don't use it except for those cases */
5643 (void) ssl;
5644#endif
5645
5646 if( handshake->resume != 0 )
5647 {
5648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5649 return( 0 );
5650 }
5651
5652#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5653 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5654 {
5655 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005656 seed = session_hash;
5657 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005658
5659 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005660 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005661 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005662#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005663
Przemek Stekiel99114f32022-04-22 11:20:09 +02005664#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005665 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005666 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005667 {
5668 /* Perform PSK-to-MS expansion in a single step. */
5669 psa_status_t status;
5670 psa_algorithm_t alg;
5671 mbedtls_svc_key_id_t psk;
5672 psa_key_derivation_operation_t derivation =
5673 PSA_KEY_DERIVATION_OPERATION_INIT;
5674 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5675
5676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5677
5678 psk = mbedtls_ssl_get_opaque_psk( ssl );
5679
5680 if( hash_alg == MBEDTLS_MD_SHA384 )
5681 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5682 else
5683 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5684
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005685 size_t other_secret_len = 0;
5686 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005687
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005688 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005689 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005690 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005691 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005692 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005693 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005694 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005695 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005696 other_secret_len = 48;
5697 other_secret = handshake->premaster + 2;
5698 break;
5699 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005700 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005701 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5702 other_secret = handshake->premaster + 2;
5703 break;
5704 default:
5705 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005706 }
5707
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005708 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005709 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005710 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005711 (unsigned char const *) lbl,
5712 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005713 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005714 master_secret_len );
5715 if( status != PSA_SUCCESS )
5716 {
5717 psa_key_derivation_abort( &derivation );
5718 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5719 }
5720
5721 status = psa_key_derivation_output_bytes( &derivation,
5722 master,
5723 master_secret_len );
5724 if( status != PSA_SUCCESS )
5725 {
5726 psa_key_derivation_abort( &derivation );
5727 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5728 }
5729
5730 status = psa_key_derivation_abort( &derivation );
5731 if( status != PSA_SUCCESS )
5732 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5733 }
5734 else
5735#endif
5736 {
5737 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005738 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005739 master,
5740 master_secret_len );
5741 if( ret != 0 )
5742 {
5743 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5744 return( ret );
5745 }
5746
5747 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5748 handshake->premaster,
5749 handshake->pmslen );
5750
5751 mbedtls_platform_zeroize( handshake->premaster,
5752 sizeof(handshake->premaster) );
5753 }
5754
5755 return( 0 );
5756}
5757
Jerry Yud62f87e2022-02-17 14:09:02 +08005758int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5759{
5760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5761 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5762 ssl->handshake->ciphersuite_info;
5763
5764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5765
5766 /* Set PRF, calc_verify and calc_finished function pointers */
5767 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005768 ciphersuite_info->mac );
5769 if( ret != 0 )
5770 {
5771 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5772 return( ret );
5773 }
5774
5775 /* Compute master secret if needed */
5776 ret = ssl_compute_master( ssl->handshake,
5777 ssl->session_negotiate->master,
5778 ssl );
5779 if( ret != 0 )
5780 {
5781 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5782 return( ret );
5783 }
5784
5785 /* Swap the client and server random values:
5786 * - MS derivation wanted client+server (RFC 5246 8.1)
5787 * - key derivation wants server+client (RFC 5246 6.3) */
5788 {
5789 unsigned char tmp[64];
5790 memcpy( tmp, ssl->handshake->randbytes, 64 );
5791 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5792 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5793 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5794 }
5795
5796 /* Populate transform structure */
5797 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5798 ssl->session_negotiate->ciphersuite,
5799 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005800#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005801 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005802#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005803 ssl->handshake->tls_prf,
5804 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005805 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005806 ssl->conf->endpoint,
5807 ssl );
5808 if( ret != 0 )
5809 {
5810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5811 return( ret );
5812 }
5813
5814 /* We no longer need Server/ClientHello.random values */
5815 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5816 sizeof( ssl->handshake->randbytes ) );
5817
5818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5819
5820 return( 0 );
5821}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005822
Ronald Cron4dcbca92022-03-07 10:21:40 +01005823int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5824{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005825 switch( md )
5826 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005827#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005828 case MBEDTLS_SSL_HASH_SHA384:
5829 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5830 break;
5831#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005833 case MBEDTLS_SSL_HASH_SHA256:
5834 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5835 break;
5836#endif
5837 default:
5838 return( -1 );
5839 }
5840
Ronald Cronc2f13a02022-03-07 10:25:24 +01005841 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005842}
5843
Andrzej Kurek25f27152022-08-17 16:09:31 -04005844#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005845void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5846 unsigned char *hash,
5847 size_t *hlen )
5848{
5849#if defined(MBEDTLS_USE_PSA_CRYPTO)
5850 size_t hash_size;
5851 psa_status_t status;
5852 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5855 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5856 if( status != PSA_SUCCESS )
5857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5859 return;
5860 }
5861
5862 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5863 if( status != PSA_SUCCESS )
5864 {
5865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5866 return;
5867 }
5868
5869 *hlen = 32;
5870 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5872#else
5873 mbedtls_sha256_context sha256;
5874
5875 mbedtls_sha256_init( &sha256 );
5876
5877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5878
5879 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5880 mbedtls_sha256_finish( &sha256, hash );
5881
5882 *hlen = 32;
5883
5884 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5886
5887 mbedtls_sha256_free( &sha256 );
5888#endif /* MBEDTLS_USE_PSA_CRYPTO */
5889 return;
5890}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005891#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005892
Andrzej Kurek25f27152022-08-17 16:09:31 -04005893#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005894void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5895 unsigned char *hash,
5896 size_t *hlen )
5897{
5898#if defined(MBEDTLS_USE_PSA_CRYPTO)
5899 size_t hash_size;
5900 psa_status_t status;
5901 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5902
5903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5904 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5905 if( status != PSA_SUCCESS )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5908 return;
5909 }
5910
5911 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5912 if( status != PSA_SUCCESS )
5913 {
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5915 return;
5916 }
5917
5918 *hlen = 48;
5919 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5921#else
5922 mbedtls_sha512_context sha512;
5923
5924 mbedtls_sha512_init( &sha512 );
5925
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5927
Andrzej Kureka242e832022-08-11 10:03:14 -04005928 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005929 mbedtls_sha512_finish( &sha512, hash );
5930
5931 *hlen = 48;
5932
5933 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5935
5936 mbedtls_sha512_free( &sha512 );
5937#endif /* MBEDTLS_USE_PSA_CRYPTO */
5938 return;
5939}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005940#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005941
Neil Armstrong80f6f322022-05-03 17:56:38 +02005942#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5943 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005944int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5945{
5946 unsigned char *p = ssl->handshake->premaster;
5947 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5948 const unsigned char *psk = NULL;
5949 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005950 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005951
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005952 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005953 {
5954 /*
5955 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005956 * checked before calling this function.
5957 *
5958 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005959 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005960 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005961 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5965 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005966 }
5967
5968 /*
5969 * PMS = struct {
5970 * opaque other_secret<0..2^16-1>;
5971 * opaque psk<0..2^16-1>;
5972 * };
5973 * with "other_secret" depending on the particular key exchange
5974 */
5975#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5976 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5977 {
5978 if( end - p < 2 )
5979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5980
5981 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5982 p += 2;
5983
5984 if( end < p || (size_t)( end - p ) < psk_len )
5985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5986
5987 memset( p, 0, psk_len );
5988 p += psk_len;
5989 }
5990 else
5991#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
5992#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
5993 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5994 {
5995 /*
5996 * other_secret already set by the ClientKeyExchange message,
5997 * and is 48 bytes long
5998 */
5999 if( end - p < 2 )
6000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6001
6002 *p++ = 0;
6003 *p++ = 48;
6004 p += 48;
6005 }
6006 else
6007#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6008#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6009 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6010 {
6011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6012 size_t len;
6013
6014 /* Write length only when we know the actual value */
6015 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6016 p + 2, end - ( p + 2 ), &len,
6017 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6018 {
6019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6020 return( ret );
6021 }
6022 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6023 p += 2 + len;
6024
6025 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6026 }
6027 else
6028#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006029#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006030 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6031 {
6032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6033 size_t zlen;
6034
6035 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6036 p + 2, end - ( p + 2 ),
6037 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6038 {
6039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6040 return( ret );
6041 }
6042
6043 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6044 p += 2 + zlen;
6045
6046 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6047 MBEDTLS_DEBUG_ECDH_Z );
6048 }
6049 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006050#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006051 {
6052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6054 }
6055
6056 /* opaque psk<0..2^16-1>; */
6057 if( end - p < 2 )
6058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6059
6060 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6061 p += 2;
6062
6063 if( end < p || (size_t)( end - p ) < psk_len )
6064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6065
6066 memcpy( p, psk, psk_len );
6067 p += psk_len;
6068
6069 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6070
6071 return( 0 );
6072}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006073#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006074
6075#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006076MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006077static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6078
6079#if defined(MBEDTLS_SSL_PROTO_DTLS)
6080int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6081{
6082 /* If renegotiation is not enforced, retransmit until we would reach max
6083 * timeout if we were using the usual handshake doubling scheme */
6084 if( ssl->conf->renego_max_records < 0 )
6085 {
6086 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6087 unsigned char doublings = 1;
6088
6089 while( ratio != 0 )
6090 {
6091 ++doublings;
6092 ratio >>= 1;
6093 }
6094
6095 if( ++ssl->renego_records_seen > doublings )
6096 {
6097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6098 return( 0 );
6099 }
6100 }
6101
6102 return( ssl_write_hello_request( ssl ) );
6103}
6104#endif
6105#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006106
Jerry Yud9526692022-02-17 14:23:47 +08006107/*
6108 * Handshake functions
6109 */
6110#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6111/* No certificate support -> dummy functions */
6112int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6113{
6114 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6115 ssl->handshake->ciphersuite_info;
6116
6117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6118
6119 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6120 {
6121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6122 ssl->state++;
6123 return( 0 );
6124 }
6125
6126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6127 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6128}
6129
6130int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6131{
6132 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6133 ssl->handshake->ciphersuite_info;
6134
6135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6136
6137 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6138 {
6139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6140 ssl->state++;
6141 return( 0 );
6142 }
6143
6144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6146}
6147
6148#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6149/* Some certificate support -> implement write and parse */
6150
6151int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6152{
6153 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6154 size_t i, n;
6155 const mbedtls_x509_crt *crt;
6156 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6157 ssl->handshake->ciphersuite_info;
6158
6159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6160
6161 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6162 {
6163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6164 ssl->state++;
6165 return( 0 );
6166 }
6167
6168#if defined(MBEDTLS_SSL_CLI_C)
6169 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6170 {
6171 if( ssl->handshake->client_auth == 0 )
6172 {
6173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6174 ssl->state++;
6175 return( 0 );
6176 }
6177 }
6178#endif /* MBEDTLS_SSL_CLI_C */
6179#if defined(MBEDTLS_SSL_SRV_C)
6180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6181 {
6182 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6183 {
6184 /* Should never happen because we shouldn't have picked the
6185 * ciphersuite if we don't have a certificate. */
6186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6187 }
6188 }
6189#endif
6190
6191 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6192
6193 /*
6194 * 0 . 0 handshake type
6195 * 1 . 3 handshake length
6196 * 4 . 6 length of all certs
6197 * 7 . 9 length of cert. 1
6198 * 10 . n-1 peer certificate
6199 * n . n+2 length of cert. 2
6200 * n+3 . ... upper level cert, etc.
6201 */
6202 i = 7;
6203 crt = mbedtls_ssl_own_cert( ssl );
6204
6205 while( crt != NULL )
6206 {
6207 n = crt->raw.len;
6208 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6209 {
6210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6211 " > %" MBEDTLS_PRINTF_SIZET,
6212 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6214 }
6215
6216 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6217 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6218 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6219
6220 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6221 i += n; crt = crt->next;
6222 }
6223
6224 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6225 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6226 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6227
6228 ssl->out_msglen = i;
6229 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6230 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6231
6232 ssl->state++;
6233
6234 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6235 {
6236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6237 return( ret );
6238 }
6239
6240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6241
6242 return( ret );
6243}
6244
6245#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6246
6247#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006248MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006249static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6250 unsigned char *crt_buf,
6251 size_t crt_buf_len )
6252{
6253 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6254
6255 if( peer_crt == NULL )
6256 return( -1 );
6257
6258 if( peer_crt->raw.len != crt_buf_len )
6259 return( -1 );
6260
6261 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6262}
6263#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006264MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006265static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6266 unsigned char *crt_buf,
6267 size_t crt_buf_len )
6268{
6269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6270 unsigned char const * const peer_cert_digest =
6271 ssl->session->peer_cert_digest;
6272 mbedtls_md_type_t const peer_cert_digest_type =
6273 ssl->session->peer_cert_digest_type;
6274 mbedtls_md_info_t const * const digest_info =
6275 mbedtls_md_info_from_type( peer_cert_digest_type );
6276 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6277 size_t digest_len;
6278
6279 if( peer_cert_digest == NULL || digest_info == NULL )
6280 return( -1 );
6281
6282 digest_len = mbedtls_md_get_size( digest_info );
6283 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6284 return( -1 );
6285
6286 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6287 if( ret != 0 )
6288 return( -1 );
6289
6290 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6291}
6292#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6293#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6294
6295/*
6296 * Once the certificate message is read, parse it into a cert chain and
6297 * perform basic checks, but leave actual verification to the caller
6298 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006299MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006300static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6301 mbedtls_x509_crt *chain )
6302{
6303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6304#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6305 int crt_cnt=0;
6306#endif
6307 size_t i, n;
6308 uint8_t alert;
6309
6310 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6311 {
6312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6314 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6315 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6316 }
6317
6318 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6319 {
6320 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6321 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6322 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6323 }
6324
6325 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6329 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6330 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6331 }
6332
6333 i = mbedtls_ssl_hs_hdr_len( ssl );
6334
6335 /*
6336 * Same message structure as in mbedtls_ssl_write_certificate()
6337 */
6338 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6339
6340 if( ssl->in_msg[i] != 0 ||
6341 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6342 {
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6344 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6345 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6346 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6347 }
6348
6349 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6350 i += 3;
6351
6352 /* Iterate through and parse the CRTs in the provided chain. */
6353 while( i < ssl->in_hslen )
6354 {
6355 /* Check that there's room for the next CRT's length fields. */
6356 if ( i + 3 > ssl->in_hslen ) {
6357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6358 mbedtls_ssl_send_alert_message( ssl,
6359 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6360 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6361 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6362 }
6363 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6364 * anything beyond 2**16 ~ 64K. */
6365 if( ssl->in_msg[i] != 0 )
6366 {
6367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6368 mbedtls_ssl_send_alert_message( ssl,
6369 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6370 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6371 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6372 }
6373
6374 /* Read length of the next CRT in the chain. */
6375 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6376 | (unsigned int) ssl->in_msg[i + 2];
6377 i += 3;
6378
6379 if( n < 128 || i + n > ssl->in_hslen )
6380 {
6381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6382 mbedtls_ssl_send_alert_message( ssl,
6383 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6384 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6385 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6386 }
6387
6388 /* Check if we're handling the first CRT in the chain. */
6389#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6390 if( crt_cnt++ == 0 &&
6391 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6392 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6393 {
6394 /* During client-side renegotiation, check that the server's
6395 * end-CRTs hasn't changed compared to the initial handshake,
6396 * mitigating the triple handshake attack. On success, reuse
6397 * the original end-CRT instead of parsing it again. */
6398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6399 if( ssl_check_peer_crt_unchanged( ssl,
6400 &ssl->in_msg[i],
6401 n ) != 0 )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6404 mbedtls_ssl_send_alert_message( ssl,
6405 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6406 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6407 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6408 }
6409
6410 /* Now we can safely free the original chain. */
6411 ssl_clear_peer_cert( ssl->session );
6412 }
6413#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6414
6415 /* Parse the next certificate in the chain. */
6416#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6417 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6418#else
6419 /* If we don't need to store the CRT chain permanently, parse
6420 * it in-place from the input buffer instead of making a copy. */
6421 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6422#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6423 switch( ret )
6424 {
6425 case 0: /*ok*/
6426 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6427 /* Ignore certificate with an unknown algorithm: maybe a
6428 prior certificate was already trusted. */
6429 break;
6430
6431 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6432 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6433 goto crt_parse_der_failed;
6434
6435 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6436 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6437 goto crt_parse_der_failed;
6438
6439 default:
6440 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6441 crt_parse_der_failed:
6442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6443 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6444 return( ret );
6445 }
6446
6447 i += n;
6448 }
6449
6450 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6451 return( 0 );
6452}
6453
6454#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006455MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006456static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6457{
6458 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6459 return( -1 );
6460
6461 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6462 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6463 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6464 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6465 {
Ronald Cron19385882022-06-15 16:26:13 +02006466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006467 return( 0 );
6468 }
6469 return( -1 );
6470}
6471#endif /* MBEDTLS_SSL_SRV_C */
6472
6473/* Check if a certificate message is expected.
6474 * Return either
6475 * - SSL_CERTIFICATE_EXPECTED, or
6476 * - SSL_CERTIFICATE_SKIP
6477 * indicating whether a Certificate message is expected or not.
6478 */
6479#define SSL_CERTIFICATE_EXPECTED 0
6480#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006481MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006482static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6483 int authmode )
6484{
6485 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6486 ssl->handshake->ciphersuite_info;
6487
6488 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6489 return( SSL_CERTIFICATE_SKIP );
6490
6491#if defined(MBEDTLS_SSL_SRV_C)
6492 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6493 {
6494 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6495 return( SSL_CERTIFICATE_SKIP );
6496
6497 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6498 {
6499 ssl->session_negotiate->verify_result =
6500 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6501 return( SSL_CERTIFICATE_SKIP );
6502 }
6503 }
6504#else
6505 ((void) authmode);
6506#endif /* MBEDTLS_SSL_SRV_C */
6507
6508 return( SSL_CERTIFICATE_EXPECTED );
6509}
6510
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006511MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006512static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6513 int authmode,
6514 mbedtls_x509_crt *chain,
6515 void *rs_ctx )
6516{
6517 int ret = 0;
6518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6519 ssl->handshake->ciphersuite_info;
6520 int have_ca_chain = 0;
6521
6522 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6523 void *p_vrfy;
6524
6525 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6526 return( 0 );
6527
6528 if( ssl->f_vrfy != NULL )
6529 {
6530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6531 f_vrfy = ssl->f_vrfy;
6532 p_vrfy = ssl->p_vrfy;
6533 }
6534 else
6535 {
6536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6537 f_vrfy = ssl->conf->f_vrfy;
6538 p_vrfy = ssl->conf->p_vrfy;
6539 }
6540
6541 /*
6542 * Main check: verify certificate
6543 */
6544#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6545 if( ssl->conf->f_ca_cb != NULL )
6546 {
6547 ((void) rs_ctx);
6548 have_ca_chain = 1;
6549
6550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6551 ret = mbedtls_x509_crt_verify_with_ca_cb(
6552 chain,
6553 ssl->conf->f_ca_cb,
6554 ssl->conf->p_ca_cb,
6555 ssl->conf->cert_profile,
6556 ssl->hostname,
6557 &ssl->session_negotiate->verify_result,
6558 f_vrfy, p_vrfy );
6559 }
6560 else
6561#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6562 {
6563 mbedtls_x509_crt *ca_chain;
6564 mbedtls_x509_crl *ca_crl;
6565
6566#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6567 if( ssl->handshake->sni_ca_chain != NULL )
6568 {
6569 ca_chain = ssl->handshake->sni_ca_chain;
6570 ca_crl = ssl->handshake->sni_ca_crl;
6571 }
6572 else
6573#endif
6574 {
6575 ca_chain = ssl->conf->ca_chain;
6576 ca_crl = ssl->conf->ca_crl;
6577 }
6578
6579 if( ca_chain != NULL )
6580 have_ca_chain = 1;
6581
6582 ret = mbedtls_x509_crt_verify_restartable(
6583 chain,
6584 ca_chain, ca_crl,
6585 ssl->conf->cert_profile,
6586 ssl->hostname,
6587 &ssl->session_negotiate->verify_result,
6588 f_vrfy, p_vrfy, rs_ctx );
6589 }
6590
6591 if( ret != 0 )
6592 {
6593 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6594 }
6595
6596#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6597 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6598 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6599#endif
6600
6601 /*
6602 * Secondary checks: always done, but change 'ret' only if it was 0
6603 */
6604
6605#if defined(MBEDTLS_ECP_C)
6606 {
6607 const mbedtls_pk_context *pk = &chain->pk;
6608
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006609 /* If certificate uses an EC key, make sure the curve is OK.
6610 * This is a public key, so it can't be opaque, so can_do() is a good
6611 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006612 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006613 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006614 /* and in the unlikely case the above assumption no longer holds
6615 * we are making sure that pk_ec() here does not return a NULL
6616 */
6617 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6618 if( ec == NULL )
6619 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006621 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6622 }
Jerry Yud9526692022-02-17 14:23:47 +08006623
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006624 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6625 {
6626 ssl->session_negotiate->verify_result |=
6627 MBEDTLS_X509_BADCERT_BAD_KEY;
6628
6629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6630 if( ret == 0 )
6631 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6632 }
Jerry Yud9526692022-02-17 14:23:47 +08006633 }
6634 }
6635#endif /* MBEDTLS_ECP_C */
6636
6637 if( mbedtls_ssl_check_cert_usage( chain,
6638 ciphersuite_info,
6639 ! ssl->conf->endpoint,
6640 &ssl->session_negotiate->verify_result ) != 0 )
6641 {
6642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6643 if( ret == 0 )
6644 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6645 }
6646
6647 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6648 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6649 * with details encoded in the verification flags. All other kinds
6650 * of error codes, including those from the user provided f_vrfy
6651 * functions, are treated as fatal and lead to a failure of
6652 * ssl_parse_certificate even if verification was optional. */
6653 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6654 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6655 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6656 {
6657 ret = 0;
6658 }
6659
6660 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6661 {
6662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6663 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6664 }
6665
6666 if( ret != 0 )
6667 {
6668 uint8_t alert;
6669
6670 /* The certificate may have been rejected for several reasons.
6671 Pick one and send the corresponding alert. Which alert to send
6672 may be a subject of debate in some cases. */
6673 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6674 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6675 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6676 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6677 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6678 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6679 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6680 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6681 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6682 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6683 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6684 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6685 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6686 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6687 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6688 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6689 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6690 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6691 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6692 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6693 else
6694 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6695 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6696 alert );
6697 }
6698
6699#if defined(MBEDTLS_DEBUG_C)
6700 if( ssl->session_negotiate->verify_result != 0 )
6701 {
6702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6703 (unsigned int) ssl->session_negotiate->verify_result ) );
6704 }
6705 else
6706 {
6707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6708 }
6709#endif /* MBEDTLS_DEBUG_C */
6710
6711 return( ret );
6712}
6713
6714#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006715MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006716static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6717 unsigned char *start, size_t len )
6718{
6719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6720 /* Remember digest of the peer's end-CRT. */
6721 ssl->session_negotiate->peer_cert_digest =
6722 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6723 if( ssl->session_negotiate->peer_cert_digest == NULL )
6724 {
6725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6726 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6727 mbedtls_ssl_send_alert_message( ssl,
6728 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6729 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6730
6731 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6732 }
6733
6734 ret = mbedtls_md( mbedtls_md_info_from_type(
6735 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6736 start, len,
6737 ssl->session_negotiate->peer_cert_digest );
6738
6739 ssl->session_negotiate->peer_cert_digest_type =
6740 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6741 ssl->session_negotiate->peer_cert_digest_len =
6742 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6743
6744 return( ret );
6745}
6746
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006747MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006748static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6749 unsigned char *start, size_t len )
6750{
6751 unsigned char *end = start + len;
6752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6753
6754 /* Make a copy of the peer's raw public key. */
6755 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6756 ret = mbedtls_pk_parse_subpubkey( &start, end,
6757 &ssl->handshake->peer_pubkey );
6758 if( ret != 0 )
6759 {
6760 /* We should have parsed the public key before. */
6761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6762 }
6763
6764 return( 0 );
6765}
6766#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6767
6768int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6769{
6770 int ret = 0;
6771 int crt_expected;
6772#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6773 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6774 ? ssl->handshake->sni_authmode
6775 : ssl->conf->authmode;
6776#else
6777 const int authmode = ssl->conf->authmode;
6778#endif
6779 void *rs_ctx = NULL;
6780 mbedtls_x509_crt *chain = NULL;
6781
6782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6783
6784 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6785 if( crt_expected == SSL_CERTIFICATE_SKIP )
6786 {
6787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6788 goto exit;
6789 }
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 {
6795 chain = ssl->handshake->ecrs_peer_cert;
6796 ssl->handshake->ecrs_peer_cert = NULL;
6797 goto crt_verify;
6798 }
6799#endif
6800
6801 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6802 {
6803 /* mbedtls_ssl_read_record may have sent an alert already. We
6804 let it decide whether to alert. */
6805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6806 goto exit;
6807 }
6808
6809#if defined(MBEDTLS_SSL_SRV_C)
6810 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6811 {
6812 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6813
6814 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6815 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6816
6817 goto exit;
6818 }
6819#endif /* MBEDTLS_SSL_SRV_C */
6820
6821 /* Clear existing peer CRT structure in case we tried to
6822 * reuse a session but it failed, and allocate a new one. */
6823 ssl_clear_peer_cert( ssl->session_negotiate );
6824
6825 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6826 if( chain == NULL )
6827 {
6828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6829 sizeof( mbedtls_x509_crt ) ) );
6830 mbedtls_ssl_send_alert_message( ssl,
6831 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6832 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6833
6834 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6835 goto exit;
6836 }
6837 mbedtls_x509_crt_init( chain );
6838
6839 ret = ssl_parse_certificate_chain( ssl, chain );
6840 if( ret != 0 )
6841 goto exit;
6842
6843#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6844 if( ssl->handshake->ecrs_enabled)
6845 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6846
6847crt_verify:
6848 if( ssl->handshake->ecrs_enabled)
6849 rs_ctx = &ssl->handshake->ecrs_ctx;
6850#endif
6851
6852 ret = ssl_parse_certificate_verify( ssl, authmode,
6853 chain, rs_ctx );
6854 if( ret != 0 )
6855 goto exit;
6856
6857#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6858 {
6859 unsigned char *crt_start, *pk_start;
6860 size_t crt_len, pk_len;
6861
6862 /* We parse the CRT chain without copying, so
6863 * these pointers point into the input buffer,
6864 * and are hence still valid after freeing the
6865 * CRT chain. */
6866
6867 crt_start = chain->raw.p;
6868 crt_len = chain->raw.len;
6869
6870 pk_start = chain->pk_raw.p;
6871 pk_len = chain->pk_raw.len;
6872
6873 /* Free the CRT structures before computing
6874 * digest and copying the peer's public key. */
6875 mbedtls_x509_crt_free( chain );
6876 mbedtls_free( chain );
6877 chain = NULL;
6878
6879 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6880 if( ret != 0 )
6881 goto exit;
6882
6883 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6884 if( ret != 0 )
6885 goto exit;
6886 }
6887#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6888 /* Pass ownership to session structure. */
6889 ssl->session_negotiate->peer_cert = chain;
6890 chain = NULL;
6891#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6892
6893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6894
6895exit:
6896
6897 if( ret == 0 )
6898 ssl->state++;
6899
6900#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6901 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6902 {
6903 ssl->handshake->ecrs_peer_cert = chain;
6904 chain = NULL;
6905 }
6906#endif
6907
6908 if( chain != NULL )
6909 {
6910 mbedtls_x509_crt_free( chain );
6911 mbedtls_free( chain );
6912 }
6913
6914 return( ret );
6915}
6916#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6917
Andrzej Kurek25f27152022-08-17 16:09:31 -04006918#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006919static void ssl_calc_finished_tls_sha256(
6920 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6921{
6922 int len = 12;
6923 const char *sender;
6924 unsigned char padbuf[32];
6925#if defined(MBEDTLS_USE_PSA_CRYPTO)
6926 size_t hash_size;
6927 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6928 psa_status_t status;
6929#else
6930 mbedtls_sha256_context sha256;
6931#endif
6932
6933 mbedtls_ssl_session *session = ssl->session_negotiate;
6934 if( !session )
6935 session = ssl->session;
6936
6937 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6938 ? "client finished"
6939 : "server finished";
6940
6941#if defined(MBEDTLS_USE_PSA_CRYPTO)
6942 sha256_psa = psa_hash_operation_init();
6943
6944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6945
6946 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6947 if( status != PSA_SUCCESS )
6948 {
6949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6950 return;
6951 }
6952
6953 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6954 if( status != PSA_SUCCESS )
6955 {
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6957 return;
6958 }
6959 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6960#else
6961
6962 mbedtls_sha256_init( &sha256 );
6963
6964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6965
6966 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6967
6968 /*
6969 * TLSv1.2:
6970 * hash = PRF( master, finished_label,
6971 * Hash( handshake ) )[0.11]
6972 */
6973
6974#if !defined(MBEDTLS_SHA256_ALT)
6975 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6976 sha256.state, sizeof( sha256.state ) );
6977#endif
6978
6979 mbedtls_sha256_finish( &sha256, padbuf );
6980 mbedtls_sha256_free( &sha256 );
6981#endif /* MBEDTLS_USE_PSA_CRYPTO */
6982
6983 ssl->handshake->tls_prf( session->master, 48, sender,
6984 padbuf, 32, buf, len );
6985
6986 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6987
6988 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6989
6990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
6991}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006992#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08006993
Jerry Yub7ba49e2022-02-17 14:25:53 +08006994
Andrzej Kurek25f27152022-08-17 16:09:31 -04006995#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08006996static void ssl_calc_finished_tls_sha384(
6997 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6998{
6999 int len = 12;
7000 const char *sender;
7001 unsigned char padbuf[48];
7002#if defined(MBEDTLS_USE_PSA_CRYPTO)
7003 size_t hash_size;
7004 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7005 psa_status_t status;
7006#else
7007 mbedtls_sha512_context sha512;
7008#endif
7009
7010 mbedtls_ssl_session *session = ssl->session_negotiate;
7011 if( !session )
7012 session = ssl->session;
7013
7014 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7015 ? "client finished"
7016 : "server finished";
7017
7018#if defined(MBEDTLS_USE_PSA_CRYPTO)
7019 sha384_psa = psa_hash_operation_init();
7020
7021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7022
7023 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7024 if( status != PSA_SUCCESS )
7025 {
7026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7027 return;
7028 }
7029
7030 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7031 if( status != PSA_SUCCESS )
7032 {
7033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7034 return;
7035 }
7036 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7037#else
7038 mbedtls_sha512_init( &sha512 );
7039
7040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7041
Andrzej Kureka242e832022-08-11 10:03:14 -04007042 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007043
7044 /*
7045 * TLSv1.2:
7046 * hash = PRF( master, finished_label,
7047 * Hash( handshake ) )[0.11]
7048 */
7049
7050#if !defined(MBEDTLS_SHA512_ALT)
7051 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7052 sha512.state, sizeof( sha512.state ) );
7053#endif
7054 mbedtls_sha512_finish( &sha512, padbuf );
7055
7056 mbedtls_sha512_free( &sha512 );
7057#endif
7058
7059 ssl->handshake->tls_prf( session->master, 48, sender,
7060 padbuf, 48, buf, len );
7061
7062 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7063
7064 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7065
7066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7067}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007068#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007069
Jerry Yuaef00152022-02-17 14:27:31 +08007070void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7071{
7072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7073
7074 /*
7075 * Free our handshake params
7076 */
7077 mbedtls_ssl_handshake_free( ssl );
7078 mbedtls_free( ssl->handshake );
7079 ssl->handshake = NULL;
7080
7081 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007082 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007083 */
7084 if( ssl->transform )
7085 {
7086 mbedtls_ssl_transform_free( ssl->transform );
7087 mbedtls_free( ssl->transform );
7088 }
7089 ssl->transform = ssl->transform_negotiate;
7090 ssl->transform_negotiate = NULL;
7091
7092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7093}
7094
Jerry Yu2a9fff52022-02-17 14:28:51 +08007095void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7096{
7097 int resume = ssl->handshake->resume;
7098
7099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7100
7101#if defined(MBEDTLS_SSL_RENEGOTIATION)
7102 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7103 {
7104 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7105 ssl->renego_records_seen = 0;
7106 }
7107#endif
7108
7109 /*
7110 * Free the previous session and switch in the current one
7111 */
7112 if( ssl->session )
7113 {
7114#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7115 /* RFC 7366 3.1: keep the EtM state */
7116 ssl->session_negotiate->encrypt_then_mac =
7117 ssl->session->encrypt_then_mac;
7118#endif
7119
7120 mbedtls_ssl_session_free( ssl->session );
7121 mbedtls_free( ssl->session );
7122 }
7123 ssl->session = ssl->session_negotiate;
7124 ssl->session_negotiate = NULL;
7125
7126 /*
7127 * Add cache entry
7128 */
7129 if( ssl->conf->f_set_cache != NULL &&
7130 ssl->session->id_len != 0 &&
7131 resume == 0 )
7132 {
7133 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7134 ssl->session->id,
7135 ssl->session->id_len,
7136 ssl->session ) != 0 )
7137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7138 }
7139
7140#if defined(MBEDTLS_SSL_PROTO_DTLS)
7141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7142 ssl->handshake->flight != NULL )
7143 {
7144 /* Cancel handshake timer */
7145 mbedtls_ssl_set_timer( ssl, 0 );
7146
7147 /* Keep last flight around in case we need to resend it:
7148 * we need the handshake and transform structures for that */
7149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7150 }
7151 else
7152#endif
7153 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7154
7155 ssl->state++;
7156
7157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7158}
7159
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007160int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7161{
7162 int ret, hash_len;
7163
7164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7165
7166 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7167
7168 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7169
7170 /*
7171 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7172 * may define some other value. Currently (early 2016), no defined
7173 * ciphersuite does this (and this is unlikely to change as activity has
7174 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7175 */
7176 hash_len = 12;
7177
7178#if defined(MBEDTLS_SSL_RENEGOTIATION)
7179 ssl->verify_data_len = hash_len;
7180 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7181#endif
7182
7183 ssl->out_msglen = 4 + hash_len;
7184 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7185 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7186
7187 /*
7188 * In case of session resuming, invert the client and server
7189 * ChangeCipherSpec messages order.
7190 */
7191 if( ssl->handshake->resume != 0 )
7192 {
7193#if defined(MBEDTLS_SSL_CLI_C)
7194 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7195 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7196#endif
7197#if defined(MBEDTLS_SSL_SRV_C)
7198 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7199 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7200#endif
7201 }
7202 else
7203 ssl->state++;
7204
7205 /*
7206 * Switch to our negotiated transform and session parameters for outbound
7207 * data.
7208 */
7209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7210
7211#if defined(MBEDTLS_SSL_PROTO_DTLS)
7212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7213 {
7214 unsigned char i;
7215
7216 /* Remember current epoch settings for resending */
7217 ssl->handshake->alt_transform_out = ssl->transform_out;
7218 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7219 sizeof( ssl->handshake->alt_out_ctr ) );
7220
7221 /* Set sequence_number to zero */
7222 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7223
7224
7225 /* Increment epoch */
7226 for( i = 2; i > 0; i-- )
7227 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7228 break;
7229
7230 /* The loop goes to its end iff the counter is wrapping */
7231 if( i == 0 )
7232 {
7233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7234 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7235 }
7236 }
7237 else
7238#endif /* MBEDTLS_SSL_PROTO_DTLS */
7239 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7240
7241 ssl->transform_out = ssl->transform_negotiate;
7242 ssl->session_out = ssl->session_negotiate;
7243
7244#if defined(MBEDTLS_SSL_PROTO_DTLS)
7245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7246 mbedtls_ssl_send_flight_completed( ssl );
7247#endif
7248
7249 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7250 {
7251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7252 return( ret );
7253 }
7254
7255#if defined(MBEDTLS_SSL_PROTO_DTLS)
7256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7257 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7258 {
7259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7260 return( ret );
7261 }
7262#endif
7263
7264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7265
7266 return( 0 );
7267}
7268
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007269#define SSL_MAX_HASH_LEN 12
7270
7271int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7272{
7273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7274 unsigned int hash_len = 12;
7275 unsigned char buf[SSL_MAX_HASH_LEN];
7276
7277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7278
7279 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7280
7281 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7282 {
7283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7284 goto exit;
7285 }
7286
7287 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7288 {
7289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7290 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7291 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7292 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7293 goto exit;
7294 }
7295
7296 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7297 {
7298 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7299 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7300 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7301 goto exit;
7302 }
7303
7304 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7305 {
7306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7308 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7309 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7310 goto exit;
7311 }
7312
7313 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7314 buf, hash_len ) != 0 )
7315 {
7316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7318 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7319 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7320 goto exit;
7321 }
7322
7323#if defined(MBEDTLS_SSL_RENEGOTIATION)
7324 ssl->verify_data_len = hash_len;
7325 memcpy( ssl->peer_verify_data, buf, hash_len );
7326#endif
7327
7328 if( ssl->handshake->resume != 0 )
7329 {
7330#if defined(MBEDTLS_SSL_CLI_C)
7331 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7332 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7333#endif
7334#if defined(MBEDTLS_SSL_SRV_C)
7335 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7336 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7337#endif
7338 }
7339 else
7340 ssl->state++;
7341
7342#if defined(MBEDTLS_SSL_PROTO_DTLS)
7343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7344 mbedtls_ssl_recv_flight_completed( ssl );
7345#endif
7346
7347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7348
7349exit:
7350 mbedtls_platform_zeroize( buf, hash_len );
7351 return( ret );
7352}
7353
Jerry Yu392112c2022-02-17 14:34:10 +08007354#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7355/*
7356 * Helper to get TLS 1.2 PRF from ciphersuite
7357 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7358 */
7359static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7360{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007361#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007362 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7363 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7364
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007365 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007366 return( tls_prf_sha384 );
7367#else
7368 (void) ciphersuite_id;
7369#endif
7370 return( tls_prf_sha256 );
7371}
7372#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007373
7374static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7375{
7376 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007377#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007378 if( tls_prf == tls_prf_sha384 )
7379 {
7380 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7381 }
7382 else
7383#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007384#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007385 if( tls_prf == tls_prf_sha256 )
7386 {
7387 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7388 }
7389 else
7390#endif
7391 return( MBEDTLS_SSL_TLS_PRF_NONE );
7392}
7393
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007394/*
7395 * Populate a transform structure with session keys and all the other
7396 * necessary information.
7397 *
7398 * Parameters:
7399 * - [in/out]: transform: structure to populate
7400 * [in] must be just initialised with mbedtls_ssl_transform_init()
7401 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7402 * - [in] ciphersuite
7403 * - [in] master
7404 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007405 * - [in] tls_prf: pointer to PRF to use for key derivation
7406 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007407 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007408 * - [in] endpoint: client or server
7409 * - [in] ssl: used for:
7410 * - ssl->conf->{f,p}_export_keys
7411 * [in] optionally used for:
7412 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7413 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007414MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007415static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7416 int ciphersuite,
7417 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007418#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007419 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007420#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007421 ssl_tls_prf_t tls_prf,
7422 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007423 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007424 unsigned endpoint,
7425 const mbedtls_ssl_context *ssl )
7426{
7427 int ret = 0;
7428 unsigned char keyblk[256];
7429 unsigned char *key1;
7430 unsigned char *key2;
7431 unsigned char *mac_enc;
7432 unsigned char *mac_dec;
7433 size_t mac_key_len = 0;
7434 size_t iv_copy_len;
7435 size_t keylen;
7436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007437 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007438#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007439 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007440 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007441#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007442
7443#if defined(MBEDTLS_USE_PSA_CRYPTO)
7444 psa_key_type_t key_type;
7445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7446 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007447 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007448 size_t key_bits;
7449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7450#endif
7451
7452#if !defined(MBEDTLS_DEBUG_C) && \
7453 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7454 if( ssl->f_export_keys == NULL )
7455 {
7456 ssl = NULL; /* make sure we don't use it except for these cases */
7457 (void) ssl;
7458 }
7459#endif
7460
7461 /*
7462 * Some data just needs copying into the structure
7463 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007464#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007465 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007466#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007467 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007468
7469#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7470 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7471#endif
7472
7473#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007474 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007475 {
7476 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7477 * generation separate. This should never happen. */
7478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7479 }
7480#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7481
7482 /*
7483 * Get various info structures
7484 */
7485 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7486 if( ciphersuite_info == NULL )
7487 {
7488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7489 ciphersuite ) );
7490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7491 }
7492
Neil Armstrongab555e02022-04-04 11:07:59 +02007493 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007494#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007495 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007496#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007497 ciphersuite_info );
7498
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007499 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7500 transform->taglen =
7501 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7502
7503#if defined(MBEDTLS_USE_PSA_CRYPTO)
7504 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7505 transform->taglen,
7506 &alg,
7507 &key_type,
7508 &key_bits ) ) != PSA_SUCCESS )
7509 {
7510 ret = psa_ssl_status_to_mbedtls( status );
7511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7512 goto end;
7513 }
7514#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007515 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7516 if( cipher_info == NULL )
7517 {
7518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7519 ciphersuite_info->cipher ) );
7520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7521 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007522#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007523
Neil Armstronge4512952022-03-08 09:08:22 +01007524#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007525 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007526 if( mac_alg == 0 )
7527 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007529 (unsigned) ciphersuite_info->mac ) );
7530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7531 }
7532#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007533 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7534 if( md_info == NULL )
7535 {
7536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7537 (unsigned) ciphersuite_info->mac ) );
7538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7539 }
Neil Armstronge4512952022-03-08 09:08:22 +01007540#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007541
7542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7543 /* Copy own and peer's CID if the use of the CID
7544 * extension has been negotiated. */
7545 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7546 {
7547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7548
7549 transform->in_cid_len = ssl->own_cid_len;
7550 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7551 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7552 transform->in_cid_len );
7553
7554 transform->out_cid_len = ssl->handshake->peer_cid_len;
7555 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7556 ssl->handshake->peer_cid_len );
7557 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7558 transform->out_cid_len );
7559 }
7560#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7561
7562 /*
7563 * Compute key block using the PRF
7564 */
7565 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7566 if( ret != 0 )
7567 {
7568 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7569 return( ret );
7570 }
7571
7572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7573 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7574 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7575 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7576 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7577
7578 /*
7579 * Determine the appropriate key, IV and MAC length.
7580 */
7581
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007582#if defined(MBEDTLS_USE_PSA_CRYPTO)
7583 keylen = PSA_BITS_TO_BYTES(key_bits);
7584#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007585 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007586#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007587
7588#if defined(MBEDTLS_GCM_C) || \
7589 defined(MBEDTLS_CCM_C) || \
7590 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007591 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007592 {
7593 size_t explicit_ivlen;
7594
7595 transform->maclen = 0;
7596 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007597
7598 /* All modes haves 96-bit IVs, but the length of the static parts vary
7599 * with mode and version:
7600 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7601 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7602 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7603 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7604 * sequence number).
7605 */
7606 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007607#if defined(MBEDTLS_USE_PSA_CRYPTO)
7608 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7609#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007610 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007611#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007612 transform->fixed_ivlen = 12;
7613 else
7614 transform->fixed_ivlen = 4;
7615
7616 /* Minimum length of encrypted record */
7617 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7618 transform->minlen = explicit_ivlen + transform->taglen;
7619 }
7620 else
7621#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7622#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007623 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7624 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7625 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007626 {
Neil Armstronge4512952022-03-08 09:08:22 +01007627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007628 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007629#else
7630 size_t block_size = cipher_info->block_size;
7631#endif /* MBEDTLS_USE_PSA_CRYPTO */
7632
7633#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007634 /* Get MAC length */
7635 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7636#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007637 /* Initialize HMAC contexts */
7638 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7639 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7640 {
7641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7642 goto end;
7643 }
7644
7645 /* Get MAC length */
7646 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007647#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007648 transform->maclen = mac_key_len;
7649
7650 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007651#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007652 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007653#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007654 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007655#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007656
7657 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007658 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007659 transform->minlen = transform->maclen;
7660 else
7661 {
7662 /*
7663 * GenericBlockCipher:
7664 * 1. if EtM is in use: one block plus MAC
7665 * otherwise: * first multiple of blocklen greater than maclen
7666 * 2. IV
7667 */
7668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007669 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007670 {
7671 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007672 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007673 }
7674 else
7675#endif
7676 {
7677 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007678 + block_size
7679 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007680 }
7681
Glenn Strauss07c64162022-03-14 12:34:51 -04007682 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007683 {
7684 transform->minlen += transform->ivlen;
7685 }
7686 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687 {
7688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7689 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7690 goto end;
7691 }
7692 }
7693 }
7694 else
7695#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7696 {
7697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7699 }
7700
7701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7702 (unsigned) keylen,
7703 (unsigned) transform->minlen,
7704 (unsigned) transform->ivlen,
7705 (unsigned) transform->maclen ) );
7706
7707 /*
7708 * Finally setup the cipher contexts, IVs and MAC secrets.
7709 */
7710#if defined(MBEDTLS_SSL_CLI_C)
7711 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7712 {
7713 key1 = keyblk + mac_key_len * 2;
7714 key2 = keyblk + mac_key_len * 2 + keylen;
7715
7716 mac_enc = keyblk;
7717 mac_dec = keyblk + mac_key_len;
7718
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007719 iv_copy_len = ( transform->fixed_ivlen ) ?
7720 transform->fixed_ivlen : transform->ivlen;
7721 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7722 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7723 iv_copy_len );
7724 }
7725 else
7726#endif /* MBEDTLS_SSL_CLI_C */
7727#if defined(MBEDTLS_SSL_SRV_C)
7728 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7729 {
7730 key1 = keyblk + mac_key_len * 2 + keylen;
7731 key2 = keyblk + mac_key_len * 2;
7732
7733 mac_enc = keyblk + mac_key_len;
7734 mac_dec = keyblk;
7735
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007736 iv_copy_len = ( transform->fixed_ivlen ) ?
7737 transform->fixed_ivlen : transform->ivlen;
7738 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7739 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7740 iv_copy_len );
7741 }
7742 else
7743#endif /* MBEDTLS_SSL_SRV_C */
7744 {
7745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7746 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7747 goto end;
7748 }
7749
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007750 if( ssl != NULL && ssl->f_export_keys != NULL )
7751 {
7752 ssl->f_export_keys( ssl->p_export_keys,
7753 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7754 master, 48,
7755 randbytes + 32,
7756 randbytes,
7757 tls_prf_get_type( tls_prf ) );
7758 }
7759
7760#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 transform->psa_alg = alg;
7762
7763 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7764 {
7765 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7766 psa_set_key_algorithm( &attributes, alg );
7767 psa_set_key_type( &attributes, key_type );
7768
7769 if( ( status = psa_import_key( &attributes,
7770 key1,
7771 PSA_BITS_TO_BYTES( key_bits ),
7772 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7773 {
7774 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7775 ret = psa_ssl_status_to_mbedtls( status );
7776 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7777 goto end;
7778 }
7779
7780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7781
7782 if( ( status = psa_import_key( &attributes,
7783 key2,
7784 PSA_BITS_TO_BYTES( key_bits ),
7785 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7786 {
7787 ret = psa_ssl_status_to_mbedtls( status );
7788 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7789 goto end;
7790 }
7791 }
7792#else
7793 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7794 cipher_info ) ) != 0 )
7795 {
7796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7797 goto end;
7798 }
7799
7800 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7801 cipher_info ) ) != 0 )
7802 {
7803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7804 goto end;
7805 }
7806
7807 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7808 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7809 MBEDTLS_ENCRYPT ) ) != 0 )
7810 {
7811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7812 goto end;
7813 }
7814
7815 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7816 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7817 MBEDTLS_DECRYPT ) ) != 0 )
7818 {
7819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7820 goto end;
7821 }
7822
7823#if defined(MBEDTLS_CIPHER_MODE_CBC)
7824 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7825 {
7826 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7827 MBEDTLS_PADDING_NONE ) ) != 0 )
7828 {
7829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7830 goto end;
7831 }
7832
7833 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7834 MBEDTLS_PADDING_NONE ) ) != 0 )
7835 {
7836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7837 goto end;
7838 }
7839 }
7840#endif /* MBEDTLS_CIPHER_MODE_CBC */
7841#endif /* MBEDTLS_USE_PSA_CRYPTO */
7842
Neil Armstrong29c0c042022-03-17 17:47:28 +01007843#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7844 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7845 For AEAD-based ciphersuites, there is nothing to do here. */
7846 if( mac_key_len != 0 )
7847 {
7848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007849 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007850
7851 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007852 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007853 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7854
7855 if( ( status = psa_import_key( &attributes,
7856 mac_enc, mac_key_len,
7857 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7858 {
7859 ret = psa_ssl_status_to_mbedtls( status );
7860 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7861 goto end;
7862 }
7863
Ronald Cronfb39f152022-03-25 14:36:28 +01007864 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007865 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7866#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7867 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7868#endif
7869 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007870 /* mbedtls_ct_hmac() requires the key to be exportable */
7871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7872 PSA_KEY_USAGE_VERIFY_HASH );
7873 else
7874 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7875
7876 if( ( status = psa_import_key( &attributes,
7877 mac_dec, mac_key_len,
7878 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7879 {
7880 ret = psa_ssl_status_to_mbedtls( status );
7881 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7882 goto end;
7883 }
7884#else
7885 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7886 if( ret != 0 )
7887 goto end;
7888 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7889 if( ret != 0 )
7890 goto end;
7891#endif /* MBEDTLS_USE_PSA_CRYPTO */
7892 }
7893#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7894
7895 ((void) mac_dec);
7896 ((void) mac_enc);
7897
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007898end:
7899 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7900 return( ret );
7901}
7902
Jerry Yuee40f9d2022-02-17 14:55:16 +08007903#if defined(MBEDTLS_USE_PSA_CRYPTO)
7904int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7905 unsigned char *hash, size_t *hashlen,
7906 unsigned char *data, size_t data_len,
7907 mbedtls_md_type_t md_alg )
7908{
7909 psa_status_t status;
7910 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007911 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007912
7913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7914
7915 if( ( status = psa_hash_setup( &hash_operation,
7916 hash_alg ) ) != PSA_SUCCESS )
7917 {
7918 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7919 goto exit;
7920 }
7921
7922 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7923 64 ) ) != PSA_SUCCESS )
7924 {
7925 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7926 goto exit;
7927 }
7928
7929 if( ( status = psa_hash_update( &hash_operation,
7930 data, data_len ) ) != PSA_SUCCESS )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7933 goto exit;
7934 }
7935
7936 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7937 hashlen ) ) != PSA_SUCCESS )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7940 goto exit;
7941 }
7942
7943exit:
7944 if( status != PSA_SUCCESS )
7945 {
7946 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7947 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7948 switch( status )
7949 {
7950 case PSA_ERROR_NOT_SUPPORTED:
7951 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7952 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7953 case PSA_ERROR_BUFFER_TOO_SMALL:
7954 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7955 case PSA_ERROR_INSUFFICIENT_MEMORY:
7956 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7957 default:
7958 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7959 }
7960 }
7961 return( 0 );
7962}
7963
7964#else
7965
7966int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7967 unsigned char *hash, size_t *hashlen,
7968 unsigned char *data, size_t data_len,
7969 mbedtls_md_type_t md_alg )
7970{
7971 int ret = 0;
7972 mbedtls_md_context_t ctx;
7973 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7974 *hashlen = mbedtls_md_get_size( md_info );
7975
7976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7977
7978 mbedtls_md_init( &ctx );
7979
7980 /*
7981 * digitally-signed struct {
7982 * opaque client_random[32];
7983 * opaque server_random[32];
7984 * ServerDHParams params;
7985 * };
7986 */
7987 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7988 {
7989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7990 goto exit;
7991 }
7992 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
7993 {
7994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
7995 goto exit;
7996 }
7997 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
7998 {
7999 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8000 goto exit;
8001 }
8002 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8003 {
8004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8005 goto exit;
8006 }
8007 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8008 {
8009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8010 goto exit;
8011 }
8012
8013exit:
8014 mbedtls_md_free( &ctx );
8015
8016 if( ret != 0 )
8017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8018 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8019
8020 return( ret );
8021}
8022#endif /* MBEDTLS_USE_PSA_CRYPTO */
8023
Jerry Yud9d91da2022-02-17 14:57:06 +08008024#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8025
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008026/* Find the preferred hash for a given signature algorithm. */
8027unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8028 mbedtls_ssl_context *ssl,
8029 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008030{
Gabor Mezei078e8032022-04-27 21:17:56 +02008031 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008032 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008033
8034 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008035 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008036
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008037 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008038 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008039 unsigned int hash_alg_received =
8040 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8041 received_sig_algs[i] );
8042 unsigned int sig_alg_received =
8043 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8044 received_sig_algs[i] );
8045
8046 if( sig_alg == sig_alg_received )
8047 {
8048#if defined(MBEDTLS_USE_PSA_CRYPTO)
8049 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8050 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008051 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008052 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008053
Neil Armstrong96eceb82022-06-30 18:05:05 +02008054 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8055 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8056 PSA_ALG_ECDSA( psa_hash_alg ),
8057 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008058 continue;
8059
Neil Armstrong96eceb82022-06-30 18:05:05 +02008060 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008061 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8062 PSA_ALG_RSA_PKCS1V15_SIGN(
8063 psa_hash_alg ),
8064 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008065 continue;
8066 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008067#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008068
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008069 return( hash_alg_received );
8070 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008071 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008072
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008073 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008074}
8075
8076#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8077
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008078/* Serialization of TLS 1.2 sessions:
8079 *
8080 * struct {
8081 * uint64 start_time;
8082 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008083 * uint8 session_id_len; // at most 32
8084 * opaque session_id[32];
8085 * opaque master[48]; // fixed length in the standard
8086 * uint32 verify_result;
8087 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8088 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8089 * uint32 ticket_lifetime;
8090 * uint8 mfl_code; // up to 255 according to standard
8091 * uint8 encrypt_then_mac; // 0 or 1
8092 * } serialized_session_tls12;
8093 *
8094 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008095static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008096 unsigned char *buf,
8097 size_t buf_len )
8098{
8099 unsigned char *p = buf;
8100 size_t used = 0;
8101
8102#if defined(MBEDTLS_HAVE_TIME)
8103 uint64_t start;
8104#endif
8105#if defined(MBEDTLS_X509_CRT_PARSE_C)
8106#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8107 size_t cert_len;
8108#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8109#endif /* MBEDTLS_X509_CRT_PARSE_C */
8110
8111 /*
8112 * Time
8113 */
8114#if defined(MBEDTLS_HAVE_TIME)
8115 used += 8;
8116
8117 if( used <= buf_len )
8118 {
8119 start = (uint64_t) session->start;
8120
8121 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8122 p += 8;
8123 }
8124#endif /* MBEDTLS_HAVE_TIME */
8125
8126 /*
8127 * Basic mandatory fields
8128 */
8129 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008130 + 1 /* id_len */
8131 + sizeof( session->id )
8132 + sizeof( session->master )
8133 + 4; /* verify_result */
8134
8135 if( used <= buf_len )
8136 {
8137 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8138 p += 2;
8139
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008140 *p++ = MBEDTLS_BYTE_0( session->id_len );
8141 memcpy( p, session->id, 32 );
8142 p += 32;
8143
8144 memcpy( p, session->master, 48 );
8145 p += 48;
8146
8147 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8148 p += 4;
8149 }
8150
8151 /*
8152 * Peer's end-entity certificate
8153 */
8154#if defined(MBEDTLS_X509_CRT_PARSE_C)
8155#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8156 if( session->peer_cert == NULL )
8157 cert_len = 0;
8158 else
8159 cert_len = session->peer_cert->raw.len;
8160
8161 used += 3 + cert_len;
8162
8163 if( used <= buf_len )
8164 {
8165 *p++ = MBEDTLS_BYTE_2( cert_len );
8166 *p++ = MBEDTLS_BYTE_1( cert_len );
8167 *p++ = MBEDTLS_BYTE_0( cert_len );
8168
8169 if( session->peer_cert != NULL )
8170 {
8171 memcpy( p, session->peer_cert->raw.p, cert_len );
8172 p += cert_len;
8173 }
8174 }
8175#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8176 if( session->peer_cert_digest != NULL )
8177 {
8178 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8179 if( used <= buf_len )
8180 {
8181 *p++ = (unsigned char) session->peer_cert_digest_type;
8182 *p++ = (unsigned char) session->peer_cert_digest_len;
8183 memcpy( p, session->peer_cert_digest,
8184 session->peer_cert_digest_len );
8185 p += session->peer_cert_digest_len;
8186 }
8187 }
8188 else
8189 {
8190 used += 2;
8191 if( used <= buf_len )
8192 {
8193 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8194 *p++ = 0;
8195 }
8196 }
8197#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8198#endif /* MBEDTLS_X509_CRT_PARSE_C */
8199
8200 /*
8201 * Session ticket if any, plus associated data
8202 */
8203#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8204 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8205
8206 if( used <= buf_len )
8207 {
8208 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8209 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8210 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8211
8212 if( session->ticket != NULL )
8213 {
8214 memcpy( p, session->ticket, session->ticket_len );
8215 p += session->ticket_len;
8216 }
8217
8218 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8219 p += 4;
8220 }
8221#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8222
8223 /*
8224 * Misc extension-related info
8225 */
8226#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8227 used += 1;
8228
8229 if( used <= buf_len )
8230 *p++ = session->mfl_code;
8231#endif
8232
8233#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8234 used += 1;
8235
8236 if( used <= buf_len )
8237 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8238#endif
8239
8240 return( used );
8241}
8242
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008243MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008244static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008245 const unsigned char *buf,
8246 size_t len )
8247{
8248#if defined(MBEDTLS_HAVE_TIME)
8249 uint64_t start;
8250#endif
8251#if defined(MBEDTLS_X509_CRT_PARSE_C)
8252#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8253 size_t cert_len;
8254#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8255#endif /* MBEDTLS_X509_CRT_PARSE_C */
8256
8257 const unsigned char *p = buf;
8258 const unsigned char * const end = buf + len;
8259
8260 /*
8261 * Time
8262 */
8263#if defined(MBEDTLS_HAVE_TIME)
8264 if( 8 > (size_t)( end - p ) )
8265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8266
8267 start = ( (uint64_t) p[0] << 56 ) |
8268 ( (uint64_t) p[1] << 48 ) |
8269 ( (uint64_t) p[2] << 40 ) |
8270 ( (uint64_t) p[3] << 32 ) |
8271 ( (uint64_t) p[4] << 24 ) |
8272 ( (uint64_t) p[5] << 16 ) |
8273 ( (uint64_t) p[6] << 8 ) |
8274 ( (uint64_t) p[7] );
8275 p += 8;
8276
8277 session->start = (time_t) start;
8278#endif /* MBEDTLS_HAVE_TIME */
8279
8280 /*
8281 * Basic mandatory fields
8282 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008283 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8285
8286 session->ciphersuite = ( p[0] << 8 ) | p[1];
8287 p += 2;
8288
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008289 session->id_len = *p++;
8290 memcpy( session->id, p, 32 );
8291 p += 32;
8292
8293 memcpy( session->master, p, 48 );
8294 p += 48;
8295
8296 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8297 ( (uint32_t) p[1] << 16 ) |
8298 ( (uint32_t) p[2] << 8 ) |
8299 ( (uint32_t) p[3] );
8300 p += 4;
8301
8302 /* Immediately clear invalid pointer values that have been read, in case
8303 * we exit early before we replaced them with valid ones. */
8304#if defined(MBEDTLS_X509_CRT_PARSE_C)
8305#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8306 session->peer_cert = NULL;
8307#else
8308 session->peer_cert_digest = NULL;
8309#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8310#endif /* MBEDTLS_X509_CRT_PARSE_C */
8311#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8312 session->ticket = NULL;
8313#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8314
8315 /*
8316 * Peer certificate
8317 */
8318#if defined(MBEDTLS_X509_CRT_PARSE_C)
8319#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8320 /* Deserialize CRT from the end of the ticket. */
8321 if( 3 > (size_t)( end - p ) )
8322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8323
8324 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8325 p += 3;
8326
8327 if( cert_len != 0 )
8328 {
8329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8330
8331 if( cert_len > (size_t)( end - p ) )
8332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8333
8334 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8335
8336 if( session->peer_cert == NULL )
8337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8338
8339 mbedtls_x509_crt_init( session->peer_cert );
8340
8341 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8342 p, cert_len ) ) != 0 )
8343 {
8344 mbedtls_x509_crt_free( session->peer_cert );
8345 mbedtls_free( session->peer_cert );
8346 session->peer_cert = NULL;
8347 return( ret );
8348 }
8349
8350 p += cert_len;
8351 }
8352#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8353 /* Deserialize CRT digest from the end of the ticket. */
8354 if( 2 > (size_t)( end - p ) )
8355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356
8357 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8358 session->peer_cert_digest_len = (size_t) *p++;
8359
8360 if( session->peer_cert_digest_len != 0 )
8361 {
8362 const mbedtls_md_info_t *md_info =
8363 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8364 if( md_info == NULL )
8365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8366 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8368
8369 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8371
8372 session->peer_cert_digest =
8373 mbedtls_calloc( 1, session->peer_cert_digest_len );
8374 if( session->peer_cert_digest == NULL )
8375 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8376
8377 memcpy( session->peer_cert_digest, p,
8378 session->peer_cert_digest_len );
8379 p += session->peer_cert_digest_len;
8380 }
8381#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8382#endif /* MBEDTLS_X509_CRT_PARSE_C */
8383
8384 /*
8385 * Session ticket and associated data
8386 */
8387#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8388 if( 3 > (size_t)( end - p ) )
8389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8390
8391 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8392 p += 3;
8393
8394 if( session->ticket_len != 0 )
8395 {
8396 if( session->ticket_len > (size_t)( end - p ) )
8397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8398
8399 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8400 if( session->ticket == NULL )
8401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8402
8403 memcpy( session->ticket, p, session->ticket_len );
8404 p += session->ticket_len;
8405 }
8406
8407 if( 4 > (size_t)( end - p ) )
8408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8409
8410 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8411 ( (uint32_t) p[1] << 16 ) |
8412 ( (uint32_t) p[2] << 8 ) |
8413 ( (uint32_t) p[3] );
8414 p += 4;
8415#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8416
8417 /*
8418 * Misc extension-related info
8419 */
8420#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8421 if( 1 > (size_t)( end - p ) )
8422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8423
8424 session->mfl_code = *p++;
8425#endif
8426
8427#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8428 if( 1 > (size_t)( end - p ) )
8429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8430
8431 session->encrypt_then_mac = *p++;
8432#endif
8433
8434 /* Done, should have consumed entire buffer */
8435 if( p != end )
8436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8437
8438 return( 0 );
8439}
Jerry Yudc7bd172022-02-17 13:44:15 +08008440#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8441
XiaokangQian75d40ef2022-04-20 11:05:24 +00008442int mbedtls_ssl_validate_ciphersuite(
8443 const mbedtls_ssl_context *ssl,
8444 const mbedtls_ssl_ciphersuite_t *suite_info,
8445 mbedtls_ssl_protocol_version min_tls_version,
8446 mbedtls_ssl_protocol_version max_tls_version )
8447{
8448 (void) ssl;
8449
8450 if( suite_info == NULL )
8451 return( -1 );
8452
8453 if( ( suite_info->min_tls_version > max_tls_version ) ||
8454 ( suite_info->max_tls_version < min_tls_version ) )
8455 {
8456 return( -1 );
8457 }
8458
XiaokangQian060d8672022-04-21 09:24:56 +00008459#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008460#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8461 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8462 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8463 {
8464 return( -1 );
8465 }
8466#endif
8467
8468 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8469#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8470 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8471 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8472 {
8473 return( -1 );
8474 }
8475#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8477
8478 return( 0 );
8479}
8480
XiaokangQianeaf36512022-04-24 09:07:44 +00008481#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8482/*
8483 * Function for writing a signature algorithm extension.
8484 *
8485 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8486 * value (TLS 1.3 RFC8446):
8487 * enum {
8488 * ....
8489 * ecdsa_secp256r1_sha256( 0x0403 ),
8490 * ecdsa_secp384r1_sha384( 0x0503 ),
8491 * ecdsa_secp521r1_sha512( 0x0603 ),
8492 * ....
8493 * } SignatureScheme;
8494 *
8495 * struct {
8496 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8497 * } SignatureSchemeList;
8498 *
8499 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8500 * value (TLS 1.2 RFC5246):
8501 * enum {
8502 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8503 * sha512(6), (255)
8504 * } HashAlgorithm;
8505 *
8506 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8507 * SignatureAlgorithm;
8508 *
8509 * struct {
8510 * HashAlgorithm hash;
8511 * SignatureAlgorithm signature;
8512 * } SignatureAndHashAlgorithm;
8513 *
8514 * SignatureAndHashAlgorithm
8515 * supported_signature_algorithms<2..2^16-2>;
8516 *
8517 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8518 * generalization of the TLS 1.2 signature algorithm extension.
8519 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8520 * `SignatureScheme` field of TLS 1.3
8521 *
8522 */
8523int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8524 const unsigned char *end, size_t *out_len )
8525{
8526 unsigned char *p = buf;
8527 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8528 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8529
8530 *out_len = 0;
8531
8532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8533
8534 /* Check if we have space for header and length field:
8535 * - extension_type (2 bytes)
8536 * - extension_data_length (2 bytes)
8537 * - supported_signature_algorithms_length (2 bytes)
8538 */
8539 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8540 p += 6;
8541
8542 /*
8543 * Write supported_signature_algorithms
8544 */
8545 supported_sig_alg = p;
8546 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8547 if( sig_alg == NULL )
8548 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8549
8550 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8551 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8553 *sig_alg,
8554 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008555 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8556 continue;
8557 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8558 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8559 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008561 *sig_alg,
8562 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008563 }
8564
8565 /* Length of supported_signature_algorithms */
8566 supported_sig_alg_len = p - supported_sig_alg;
8567 if( supported_sig_alg_len == 0 )
8568 {
8569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8571 }
8572
XiaokangQianeaf36512022-04-24 09:07:44 +00008573 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008574 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008575 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8576
XiaokangQianeaf36512022-04-24 09:07:44 +00008577 *out_len = p - buf;
8578
8579#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8580 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8581#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8582 return( 0 );
8583}
8584#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8585
XiaokangQian40a35232022-05-07 09:02:40 +00008586#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008587/*
8588 * mbedtls_ssl_parse_server_name_ext
8589 *
8590 * Structure of server_name extension:
8591 *
8592 * enum {
8593 * host_name(0), (255)
8594 * } NameType;
8595 * opaque HostName<1..2^16-1>;
8596 *
8597 * struct {
8598 * NameType name_type;
8599 * select (name_type) {
8600 * case host_name: HostName;
8601 * } name;
8602 * } ServerName;
8603 * struct {
8604 * ServerName server_name_list<1..2^16-1>
8605 * } ServerNameList;
8606 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008607MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008608int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8609 const unsigned char *buf,
8610 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008611{
8612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8613 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008614 size_t server_name_list_len, hostname_len;
8615 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008616
XiaokangQianf2a94202022-05-20 06:44:24 +00008617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008618
8619 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008620 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008621 p += 2;
8622
XiaokangQian9b2b7712022-05-17 02:57:00 +00008623 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8624 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008625 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008626 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008628 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008629 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8630 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008631
8632 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8633 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008634 /* sni_name is intended to be used only during the parsing of the
8635 * ClientHello message (it is reset to NULL before the end of
8636 * the message parsing). Thus it is ok to just point to the
8637 * reception buffer and not make a copy of it.
8638 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008639 ssl->handshake->sni_name = p + 3;
8640 ssl->handshake->sni_name_len = hostname_len;
8641 if( ssl->conf->f_sni == NULL )
8642 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008643 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008644 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008645 if( ret != 0 )
8646 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008647 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008648 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8649 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008650 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8651 }
8652 return( 0 );
8653 }
8654
8655 p += hostname_len + 3;
8656 }
8657
8658 return( 0 );
8659}
8660#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8661
XiaokangQianacb39922022-06-17 10:18:48 +00008662#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008663MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008664int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8665 const unsigned char *buf,
8666 const unsigned char *end )
8667{
8668 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008669 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008670 const unsigned char *protocol_name_list;
8671 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008672 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008673
8674 /* If ALPN not configured, just ignore the extension */
8675 if( ssl->conf->alpn_list == NULL )
8676 return( 0 );
8677
8678 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008679 * RFC7301, section 3.1
8680 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008681 *
XiaokangQianc7403452022-06-23 03:24:12 +00008682 * struct {
8683 * ProtocolName protocol_name_list<2..2^16-1>
8684 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008685 */
8686
XiaokangQianc7403452022-06-23 03:24:12 +00008687 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008688 * protocol_name_list_len 2 bytes
8689 * protocol_name_len 1 bytes
8690 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008691 */
XiaokangQianacb39922022-06-17 10:18:48 +00008692 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8693
XiaokangQianc7403452022-06-23 03:24:12 +00008694 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008695 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008696 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008697 protocol_name_list = p;
8698 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008699
8700 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008701 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008702 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008703 protocol_name_len = *p++;
8704 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8705 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008706 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008707 {
8708 MBEDTLS_SSL_PEND_FATAL_ALERT(
8709 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8710 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008711 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008712 }
8713
8714 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008715 }
8716
8717 /* Use our order of preference */
8718 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8719 {
8720 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008721 p = protocol_name_list;
8722 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008723 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008724 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008725 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008726 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008727 {
8728 ssl->alpn_chosen = *alpn;
8729 return( 0 );
8730 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008731
8732 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008733 }
8734 }
8735
XiaokangQian95d5f542022-06-24 02:29:26 +00008736 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008737 MBEDTLS_SSL_PEND_FATAL_ALERT(
8738 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8739 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8740 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8741}
8742
8743int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8744 unsigned char *buf,
8745 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008746 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008747{
8748 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008749 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008750 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008751
8752 if( ssl->alpn_chosen == NULL )
8753 {
8754 return( 0 );
8755 }
8756
XiaokangQian95d5f542022-06-24 02:29:26 +00008757 protocol_name_len = strlen( ssl->alpn_chosen );
8758 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008759
8760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8761 /*
8762 * 0 . 1 ext identifier
8763 * 2 . 3 ext length
8764 * 4 . 5 protocol list length
8765 * 6 . 6 protocol name length
8766 * 7 . 7+n protocol name
8767 */
8768 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8769
XiaokangQian95d5f542022-06-24 02:29:26 +00008770 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008771
XiaokangQian95d5f542022-06-24 02:29:26 +00008772 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8773 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008774 /* Note: the length of the chosen protocol has been checked to be less
8775 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8776 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008777 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008778
XiaokangQian95d5f542022-06-24 02:29:26 +00008779 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008780 return ( 0 );
8781}
8782#endif /* MBEDTLS_SSL_ALPN */
8783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#endif /* MBEDTLS_SSL_TLS_C */