blob: 892a868482f0143c1bf373bb26e156bf15d147dc [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
Jerry Yu19ae6f62022-09-30 09:22:21 +08001376#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1377 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1378 ( ( ret = mbedtls_ssl_tls13_ciphersuite_to_alg(
1379 ssl, session->ciphersuite, NULL ) ) != 0 ) )
1380 {
1381 return( ret );
1382 }
1383#endif
1384
Hanno Becker52055ae2019-02-06 14:30:46 +00001385 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1386 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001387 return( ret );
1388
Paul Bakker0a597072012-09-25 21:55:46 +00001389 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001390
1391 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001395void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1396 const int *ciphersuites )
1397{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001398 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001399}
1400
Ronald Cron6f135e12021-12-08 16:57:54 +01001401#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001402void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001403 const int kex_modes )
1404{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001405 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001406}
Ronald Cron6f135e12021-12-08 16:57:54 +01001407#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001410void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001411 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001412{
1413 conf->cert_profile = profile;
1414}
1415
Glenn Strauss36872db2022-01-22 05:06:31 -05001416static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1417{
1418 mbedtls_ssl_key_cert *cur = key_cert, *next;
1419
1420 while( cur != NULL )
1421 {
1422 next = cur->next;
1423 mbedtls_free( cur );
1424 cur = next;
1425 }
1426}
1427
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001428/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001429MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001430static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1431 mbedtls_x509_crt *cert,
1432 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001433{
niisato8ee24222018-06-25 19:05:48 +09001434 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001435
Glenn Strauss36872db2022-01-22 05:06:31 -05001436 if( cert == NULL )
1437 {
1438 /* Free list if cert is null */
1439 ssl_key_cert_free( *head );
1440 *head = NULL;
1441 return( 0 );
1442 }
1443
niisato8ee24222018-06-25 19:05:48 +09001444 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1445 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001446 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001447
niisato8ee24222018-06-25 19:05:48 +09001448 new_cert->cert = cert;
1449 new_cert->key = key;
1450 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001451
Glenn Strauss36872db2022-01-22 05:06:31 -05001452 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001453 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001454 {
niisato8ee24222018-06-25 19:05:48 +09001455 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001456 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001457 else
1458 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001459 mbedtls_ssl_key_cert *cur = *head;
1460 while( cur->next != NULL )
1461 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001462 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001463 }
1464
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001465 return( 0 );
1466}
1467
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001468int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001469 mbedtls_x509_crt *own_cert,
1470 mbedtls_pk_context *pk_key )
1471{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001472 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001473}
1474
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001475void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001476 mbedtls_x509_crt *ca_chain,
1477 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001478{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001479 conf->ca_chain = ca_chain;
1480 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001481
1482#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1483 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1484 * cannot be used together. */
1485 conf->f_ca_cb = NULL;
1486 conf->p_ca_cb = NULL;
1487#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001488}
Hanno Becker5adaad92019-03-27 16:54:37 +00001489
1490#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1491void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001492 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001493 void *p_ca_cb )
1494{
1495 conf->f_ca_cb = f_ca_cb;
1496 conf->p_ca_cb = p_ca_cb;
1497
1498 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1499 * cannot be used together. */
1500 conf->ca_chain = NULL;
1501 conf->ca_crl = NULL;
1502}
1503#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001505
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001506#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001507const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1508 size_t *name_len )
1509{
1510 *name_len = ssl->handshake->sni_name_len;
1511 return( ssl->handshake->sni_name );
1512}
1513
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001514int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1515 mbedtls_x509_crt *own_cert,
1516 mbedtls_pk_context *pk_key )
1517{
1518 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1519 own_cert, pk_key ) );
1520}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001521
1522void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1523 mbedtls_x509_crt *ca_chain,
1524 mbedtls_x509_crl *ca_crl )
1525{
1526 ssl->handshake->sni_ca_chain = ca_chain;
1527 ssl->handshake->sni_ca_crl = ca_crl;
1528}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001529
Glenn Strauss999ef702022-03-11 01:37:23 -05001530#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1531void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1532 const mbedtls_x509_crt *crt)
1533{
1534 ssl->handshake->dn_hints = crt;
1535}
1536#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1537
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001538void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1539 int authmode )
1540{
1541 ssl->handshake->sni_authmode = authmode;
1542}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001543#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1544
Hanno Becker8927c832019-04-03 12:52:50 +01001545#if defined(MBEDTLS_X509_CRT_PARSE_C)
1546void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1547 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1548 void *p_vrfy )
1549{
1550 ssl->f_vrfy = f_vrfy;
1551 ssl->p_vrfy = p_vrfy;
1552}
1553#endif
1554
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001555#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001556/*
1557 * Set EC J-PAKE password for current handshake
1558 */
1559int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1560 const unsigned char *pw,
1561 size_t pw_len )
1562{
1563 mbedtls_ecjpake_role role;
1564
Janos Follath8eb64132016-06-03 15:40:57 +01001565 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1567
1568 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1569 role = MBEDTLS_ECJPAKE_SERVER;
1570 else
1571 role = MBEDTLS_ECJPAKE_CLIENT;
1572
1573 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1574 role,
1575 MBEDTLS_MD_SHA256,
1576 MBEDTLS_ECP_DP_SECP256R1,
1577 pw, pw_len ) );
1578}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001579#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001580
Gilles Peskineeccd8882020-03-10 12:19:08 +01001581#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001582
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001583MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001584static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1585{
1586#if defined(MBEDTLS_USE_PSA_CRYPTO)
1587 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1588 return( 1 );
1589#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001590 if( conf->psk != NULL )
1591 return( 1 );
1592
1593 return( 0 );
1594}
1595
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001596static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1597{
1598 /* Remove reference to existing PSK, if any. */
1599#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001600 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001601 {
1602 /* The maintenance of the PSK key slot is the
1603 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001604 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001605 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001606#endif /* MBEDTLS_USE_PSA_CRYPTO */
1607 if( conf->psk != NULL )
1608 {
1609 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1610
1611 mbedtls_free( conf->psk );
1612 conf->psk = NULL;
1613 conf->psk_len = 0;
1614 }
1615
1616 /* Remove reference to PSK identity, if any. */
1617 if( conf->psk_identity != NULL )
1618 {
1619 mbedtls_free( conf->psk_identity );
1620 conf->psk_identity = NULL;
1621 conf->psk_identity_len = 0;
1622 }
1623}
1624
Hanno Becker7390c712018-11-15 13:33:04 +00001625/* This function assumes that PSK identity in the SSL config is unset.
1626 * It checks that the provided identity is well-formed and attempts
1627 * to make a copy of it in the SSL config.
1628 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001629MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001630static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1631 unsigned char const *psk_identity,
1632 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001633{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001634 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001635 if( psk_identity == NULL ||
1636 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001637 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001638 {
1639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1640 }
1641
Hanno Becker7390c712018-11-15 13:33:04 +00001642 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1643 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001644 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001645
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001646 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001647 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001648
1649 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001650}
1651
Hanno Becker7390c712018-11-15 13:33:04 +00001652int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1653 const unsigned char *psk, size_t psk_len,
1654 const unsigned char *psk_identity, size_t psk_identity_len )
1655{
Janos Follath865b3eb2019-12-16 11:46:15 +00001656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001657
1658 /* We currently only support one PSK, raw or opaque. */
1659 if( ssl_conf_psk_is_configured( conf ) )
1660 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001661
1662 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001663 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001665 if( psk_len == 0 )
1666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1667 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1669
Hanno Becker7390c712018-11-15 13:33:04 +00001670 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1671 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1672 conf->psk_len = psk_len;
1673 memcpy( conf->psk, psk, conf->psk_len );
1674
1675 /* Check and set PSK Identity */
1676 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1677 if( ret != 0 )
1678 ssl_conf_remove_psk( conf );
1679
1680 return( ret );
1681}
1682
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001683static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1684{
1685#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001687 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001688 /* The maintenance of the external PSK key slot is the
1689 * user's responsibility. */
1690 if( ssl->handshake->psk_opaque_is_internal )
1691 {
1692 psa_destroy_key( ssl->handshake->psk_opaque );
1693 ssl->handshake->psk_opaque_is_internal = 0;
1694 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001695 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001696 }
Neil Armstronge952a302022-05-03 10:22:14 +02001697#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001698 if( ssl->handshake->psk != NULL )
1699 {
1700 mbedtls_platform_zeroize( ssl->handshake->psk,
1701 ssl->handshake->psk_len );
1702 mbedtls_free( ssl->handshake->psk );
1703 ssl->handshake->psk_len = 0;
1704 }
Neil Armstronge952a302022-05-03 10:22:14 +02001705#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001706}
1707
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001708int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1709 const unsigned char *psk, size_t psk_len )
1710{
Neil Armstrong501c9322022-05-03 09:35:09 +02001711#if defined(MBEDTLS_USE_PSA_CRYPTO)
1712 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001714 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001716#endif /* MBEDTLS_USE_PSA_CRYPTO */
1717
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001718 if( psk == NULL || ssl->handshake == NULL )
1719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1720
1721 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1723
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001724 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001725
Neil Armstrong501c9322022-05-03 09:35:09 +02001726#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1728 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1729 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001730 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001731 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1732 else
1733 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1734 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1735 }
1736#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001737
Jerry Yu568ec252022-07-22 21:27:34 +08001738#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001739 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1740 {
1741 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1742 psa_set_key_usage_flags( &key_attributes,
1743 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1744 }
1745#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1746
Neil Armstrong501c9322022-05-03 09:35:09 +02001747 psa_set_key_algorithm( &key_attributes, alg );
1748 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1749
1750 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1751 if( status != PSA_SUCCESS )
1752 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1753
1754 /* Allow calling psa_destroy_key() on psk remove */
1755 ssl->handshake->psk_opaque_is_internal = 1;
1756 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1757#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001758 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001759 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001760
1761 ssl->handshake->psk_len = psk_len;
1762 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1763
1764 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001765#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001766}
1767
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001768#if defined(MBEDTLS_USE_PSA_CRYPTO)
1769int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001770 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001771 const unsigned char *psk_identity,
1772 size_t psk_identity_len )
1773{
Janos Follath865b3eb2019-12-16 11:46:15 +00001774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001775
1776 /* We currently only support one PSK, raw or opaque. */
1777 if( ssl_conf_psk_is_configured( conf ) )
1778 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001779
Hanno Becker7390c712018-11-15 13:33:04 +00001780 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001781 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001783 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001784
1785 /* Check and set PSK Identity */
1786 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1787 psk_identity_len );
1788 if( ret != 0 )
1789 ssl_conf_remove_psk( conf );
1790
1791 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001792}
1793
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001794int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1795 mbedtls_svc_key_id_t psk )
1796{
1797 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1798 ( ssl->handshake == NULL ) )
1799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1800
1801 ssl_remove_psk( ssl );
1802 ssl->handshake->psk_opaque = psk;
1803 return( 0 );
1804}
1805#endif /* MBEDTLS_USE_PSA_CRYPTO */
1806
Jerry Yu8897c072022-08-12 13:56:53 +08001807#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001808void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1809 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1810 size_t),
1811 void *p_psk )
1812{
1813 conf->f_psk = f_psk;
1814 conf->p_psk = p_psk;
1815}
Jerry Yu8897c072022-08-12 13:56:53 +08001816#endif /* MBEDTLS_SSL_SRV_C */
1817
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001818#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1819
1820#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001821static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1822 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001823{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001824#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001825 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001826 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001827#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001828 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001829 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001830 return( MBEDTLS_SSL_MODE_STREAM );
1831}
1832
1833#else /* MBEDTLS_USE_PSA_CRYPTO */
1834
1835static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1836 mbedtls_cipher_mode_t mode )
1837{
1838#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1839 if( mode == MBEDTLS_MODE_CBC )
1840 return( MBEDTLS_SSL_MODE_CBC );
1841#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1842
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843#if defined(MBEDTLS_GCM_C) || \
1844 defined(MBEDTLS_CCM_C) || \
1845 defined(MBEDTLS_CHACHAPOLY_C)
1846 if( mode == MBEDTLS_MODE_GCM ||
1847 mode == MBEDTLS_MODE_CCM ||
1848 mode == MBEDTLS_MODE_CHACHAPOLY )
1849 return( MBEDTLS_SSL_MODE_AEAD );
1850#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001851
1852 return( MBEDTLS_SSL_MODE_STREAM );
1853}
Gilles Peskine301711e2022-04-26 16:57:05 +02001854#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001855
Gilles Peskinee108d982022-04-26 16:50:40 +02001856static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1857 mbedtls_ssl_mode_t base_mode,
1858 int encrypt_then_mac )
1859{
1860#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1861 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1862 base_mode == MBEDTLS_SSL_MODE_CBC )
1863 {
1864 return( MBEDTLS_SSL_MODE_CBC_ETM );
1865 }
1866#else
1867 (void) encrypt_then_mac;
1868#endif
1869 return( base_mode );
1870}
1871
Neil Armstrongab555e02022-04-04 11:07:59 +02001872mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001873 const mbedtls_ssl_transform *transform )
1874{
Gilles Peskinee108d982022-04-26 16:50:40 +02001875 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001877 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001878#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001879 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1880#endif
1881 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001882
Gilles Peskinee108d982022-04-26 16:50:40 +02001883 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001884#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001885 encrypt_then_mac = transform->encrypt_then_mac;
1886#endif
1887 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001888}
1889
Neil Armstrongab555e02022-04-04 11:07:59 +02001890mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001891#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001892 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001893#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001894 const mbedtls_ssl_ciphersuite_t *suite )
1895{
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1897
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001898#if defined(MBEDTLS_USE_PSA_CRYPTO)
1899 psa_status_t status;
1900 psa_algorithm_t alg;
1901 psa_key_type_t type;
1902 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001903 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1904 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001905 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001906#else
1907 const mbedtls_cipher_info_t *cipher =
1908 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001909 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001910 {
1911 base_mode =
1912 mbedtls_ssl_get_base_mode(
1913 mbedtls_cipher_info_get_mode( cipher ) );
1914 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001915#endif /* MBEDTLS_USE_PSA_CRYPTO */
1916
Gilles Peskinee108d982022-04-26 16:50:40 +02001917#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1918 int encrypt_then_mac = 0;
1919#endif
1920 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001921}
1922
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001923#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001924
1925#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001926/* Serialization of TLS 1.3 sessions:
1927 *
1928 * struct {
1929 * uint64 ticket_received;
1930 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001931 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001932 * } ClientOnlyData;
1933 *
1934 * struct {
1935 * uint8 endpoint;
1936 * uint8 ciphersuite[2];
1937 * uint32 ticket_age_add;
1938 * uint8 ticket_flags;
1939 * opaque resumption_key<0..255>;
1940 * select ( endpoint ) {
1941 * case client: ClientOnlyData;
1942 * case server: uint64 start_time;
1943 * };
1944 * } serialized_session_tls13;
1945 *
1946 */
1947#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001948MBEDTLS_CHECK_RETURN_CRITICAL
1949static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1950 unsigned char *buf,
1951 size_t buf_len,
1952 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001953{
1954 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001955 size_t needed = 1 /* endpoint */
1956 + 2 /* ciphersuite */
1957 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001958 + 1 /* ticket_flags */
1959 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001960 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001961
1962 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1964 needed += session->resumption_key_len; /* resumption_key */
1965
Jerry Yu438ddd82022-07-07 06:55:50 +00001966#if defined(MBEDTLS_HAVE_TIME)
1967 needed += 8; /* start_time or ticket_received */
1968#endif
1969
1970#if defined(MBEDTLS_SSL_CLI_C)
1971 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1972 {
1973 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001974 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001975
1976 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001977 if( session->ticket_len > SIZE_MAX - needed )
1978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001979
Jerry Yue28d9742022-08-18 15:44:03 +08001980 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001981 }
1982#endif /* MBEDTLS_SSL_CLI_C */
1983
Jerry Yue36fdd62022-08-17 21:31:36 +08001984 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001985 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001986 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001987
1988 p[0] = session->endpoint;
1989 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1990 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1991 p[7] = session->ticket_flags;
1992
1993 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08001994 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001995 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001996 memcpy( p, session->resumption_key, session->resumption_key_len );
1997 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00001998
1999#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2000 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2001 {
2002 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2003 p += 8;
2004 }
2005#endif /* MBEDTLS_HAVE_TIME */
2006
2007#if defined(MBEDTLS_SSL_CLI_C)
2008 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2009 {
2010#if defined(MBEDTLS_HAVE_TIME)
2011 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2012 p += 8;
2013#endif
2014 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2015 p += 4;
2016
2017 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2018 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002019
2020 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002021 {
2022 memcpy( p, session->ticket, session->ticket_len );
2023 p += session->ticket_len;
2024 }
2025 }
2026#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002027 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002028}
2029
2030MBEDTLS_CHECK_RETURN_CRITICAL
2031static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2032 const unsigned char *buf,
2033 size_t len )
2034{
2035 const unsigned char *p = buf;
2036 const unsigned char *end = buf + len;
2037
2038 if( end - p < 9 )
2039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2040 session->endpoint = p[0];
2041 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2042 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2043 session->ticket_flags = p[7];
2044
2045 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002046 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002047 p += 9;
2048
Jerry Yubc7c1a42022-07-21 22:57:37 +08002049 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2051
Jerry Yubc7c1a42022-07-21 22:57:37 +08002052 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002054 memcpy( session->resumption_key, p, session->resumption_key_len );
2055 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002056
2057#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2058 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2059 {
2060 if( end - p < 8 )
2061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2062 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2063 p += 8;
2064 }
2065#endif /* MBEDTLS_HAVE_TIME */
2066
2067#if defined(MBEDTLS_SSL_CLI_C)
2068 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2069 {
2070#if defined(MBEDTLS_HAVE_TIME)
2071 if( end - p < 8 )
2072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2073 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2074 p += 8;
2075#endif
2076 if( end - p < 4 )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2079 p += 4;
2080
2081 if( end - p < 2 )
2082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2083 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2084 p += 2;
2085
2086 if( end - p < ( long int )session->ticket_len )
2087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2088 if( session->ticket_len > 0 )
2089 {
2090 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2091 if( session->ticket == NULL )
2092 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2093 memcpy( session->ticket, p, session->ticket_len );
2094 p += session->ticket_len;
2095 }
2096 }
2097#endif /* MBEDTLS_SSL_CLI_C */
2098
2099 return( 0 );
2100
2101}
2102#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002103MBEDTLS_CHECK_RETURN_CRITICAL
2104static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2105 unsigned char *buf,
2106 size_t buf_len,
2107 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002108{
2109 ((void) session);
2110 ((void) buf);
2111 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002112 *olen = 0;
2113 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002114}
Jerry Yu438ddd82022-07-07 06:55:50 +00002115
2116static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2117 unsigned char *buf,
2118 size_t buf_len )
2119{
2120 ((void) session);
2121 ((void) buf);
2122 ((void) buf_len);
2123 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2124}
2125#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002126#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2127
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002128psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002129 size_t taglen,
2130 psa_algorithm_t *alg,
2131 psa_key_type_t *key_type,
2132 size_t *key_size )
2133{
2134 switch ( mbedtls_cipher_type )
2135 {
2136 case MBEDTLS_CIPHER_AES_128_CBC:
2137 *alg = PSA_ALG_CBC_NO_PADDING;
2138 *key_type = PSA_KEY_TYPE_AES;
2139 *key_size = 128;
2140 break;
2141 case MBEDTLS_CIPHER_AES_128_CCM:
2142 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2143 *key_type = PSA_KEY_TYPE_AES;
2144 *key_size = 128;
2145 break;
2146 case MBEDTLS_CIPHER_AES_128_GCM:
2147 *alg = PSA_ALG_GCM;
2148 *key_type = PSA_KEY_TYPE_AES;
2149 *key_size = 128;
2150 break;
2151 case MBEDTLS_CIPHER_AES_192_CCM:
2152 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2153 *key_type = PSA_KEY_TYPE_AES;
2154 *key_size = 192;
2155 break;
2156 case MBEDTLS_CIPHER_AES_192_GCM:
2157 *alg = PSA_ALG_GCM;
2158 *key_type = PSA_KEY_TYPE_AES;
2159 *key_size = 192;
2160 break;
2161 case MBEDTLS_CIPHER_AES_256_CBC:
2162 *alg = PSA_ALG_CBC_NO_PADDING;
2163 *key_type = PSA_KEY_TYPE_AES;
2164 *key_size = 256;
2165 break;
2166 case MBEDTLS_CIPHER_AES_256_CCM:
2167 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2168 *key_type = PSA_KEY_TYPE_AES;
2169 *key_size = 256;
2170 break;
2171 case MBEDTLS_CIPHER_AES_256_GCM:
2172 *alg = PSA_ALG_GCM;
2173 *key_type = PSA_KEY_TYPE_AES;
2174 *key_size = 256;
2175 break;
2176 case MBEDTLS_CIPHER_ARIA_128_CBC:
2177 *alg = PSA_ALG_CBC_NO_PADDING;
2178 *key_type = PSA_KEY_TYPE_ARIA;
2179 *key_size = 128;
2180 break;
2181 case MBEDTLS_CIPHER_ARIA_128_CCM:
2182 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2183 *key_type = PSA_KEY_TYPE_ARIA;
2184 *key_size = 128;
2185 break;
2186 case MBEDTLS_CIPHER_ARIA_128_GCM:
2187 *alg = PSA_ALG_GCM;
2188 *key_type = PSA_KEY_TYPE_ARIA;
2189 *key_size = 128;
2190 break;
2191 case MBEDTLS_CIPHER_ARIA_192_CCM:
2192 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2193 *key_type = PSA_KEY_TYPE_ARIA;
2194 *key_size = 192;
2195 break;
2196 case MBEDTLS_CIPHER_ARIA_192_GCM:
2197 *alg = PSA_ALG_GCM;
2198 *key_type = PSA_KEY_TYPE_ARIA;
2199 *key_size = 192;
2200 break;
2201 case MBEDTLS_CIPHER_ARIA_256_CBC:
2202 *alg = PSA_ALG_CBC_NO_PADDING;
2203 *key_type = PSA_KEY_TYPE_ARIA;
2204 *key_size = 256;
2205 break;
2206 case MBEDTLS_CIPHER_ARIA_256_CCM:
2207 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2208 *key_type = PSA_KEY_TYPE_ARIA;
2209 *key_size = 256;
2210 break;
2211 case MBEDTLS_CIPHER_ARIA_256_GCM:
2212 *alg = PSA_ALG_GCM;
2213 *key_type = PSA_KEY_TYPE_ARIA;
2214 *key_size = 256;
2215 break;
2216 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2217 *alg = PSA_ALG_CBC_NO_PADDING;
2218 *key_type = PSA_KEY_TYPE_CAMELLIA;
2219 *key_size = 128;
2220 break;
2221 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2222 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2223 *key_type = PSA_KEY_TYPE_CAMELLIA;
2224 *key_size = 128;
2225 break;
2226 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2227 *alg = PSA_ALG_GCM;
2228 *key_type = PSA_KEY_TYPE_CAMELLIA;
2229 *key_size = 128;
2230 break;
2231 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2232 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2233 *key_type = PSA_KEY_TYPE_CAMELLIA;
2234 *key_size = 192;
2235 break;
2236 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2237 *alg = PSA_ALG_GCM;
2238 *key_type = PSA_KEY_TYPE_CAMELLIA;
2239 *key_size = 192;
2240 break;
2241 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2242 *alg = PSA_ALG_CBC_NO_PADDING;
2243 *key_type = PSA_KEY_TYPE_CAMELLIA;
2244 *key_size = 256;
2245 break;
2246 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2247 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2248 *key_type = PSA_KEY_TYPE_CAMELLIA;
2249 *key_size = 256;
2250 break;
2251 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2252 *alg = PSA_ALG_GCM;
2253 *key_type = PSA_KEY_TYPE_CAMELLIA;
2254 *key_size = 256;
2255 break;
2256 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2257 *alg = PSA_ALG_CHACHA20_POLY1305;
2258 *key_type = PSA_KEY_TYPE_CHACHA20;
2259 *key_size = 256;
2260 break;
2261 case MBEDTLS_CIPHER_NULL:
2262 *alg = MBEDTLS_SSL_NULL_CIPHER;
2263 *key_type = 0;
2264 *key_size = 0;
2265 break;
2266 default:
2267 return PSA_ERROR_NOT_SUPPORTED;
2268 }
2269
2270 return PSA_SUCCESS;
2271}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002272#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002273
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002274#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002275int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2276 const unsigned char *dhm_P, size_t P_len,
2277 const unsigned char *dhm_G, size_t G_len )
2278{
Janos Follath865b3eb2019-12-16 11:46:15 +00002279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002280
Glenn Strausscee11292021-12-20 01:43:17 -05002281 mbedtls_mpi_free( &conf->dhm_P );
2282 mbedtls_mpi_free( &conf->dhm_G );
2283
Hanno Beckera90658f2017-10-04 15:29:08 +01002284 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2285 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2286 {
2287 mbedtls_mpi_free( &conf->dhm_P );
2288 mbedtls_mpi_free( &conf->dhm_G );
2289 return( ret );
2290 }
2291
2292 return( 0 );
2293}
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002295int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002296{
Janos Follath865b3eb2019-12-16 11:46:15 +00002297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002298
Glenn Strausscee11292021-12-20 01:43:17 -05002299 mbedtls_mpi_free( &conf->dhm_P );
2300 mbedtls_mpi_free( &conf->dhm_G );
2301
Gilles Peskinee5702482021-06-11 21:59:08 +02002302 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2303 &conf->dhm_P ) ) != 0 ||
2304 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2305 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002306 {
2307 mbedtls_mpi_free( &conf->dhm_P );
2308 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002309 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002310 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002311
2312 return( 0 );
2313}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002314#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002315
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002316#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2317/*
2318 * Set the minimum length for Diffie-Hellman parameters
2319 */
2320void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2321 unsigned int bitlen )
2322{
2323 conf->dhm_min_bitlen = bitlen;
2324}
2325#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2326
Gilles Peskineeccd8882020-03-10 12:19:08 +01002327#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002328#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002329/*
2330 * Set allowed/preferred hashes for handshake signatures
2331 */
2332void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2333 const int *hashes )
2334{
2335 conf->sig_hashes = hashes;
2336}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002337#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002338
Jerry Yuf017ee42022-01-12 15:49:48 +08002339/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002340void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2341 const uint16_t* sig_algs )
2342{
Jerry Yuf017ee42022-01-12 15:49:48 +08002343#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2344 conf->sig_hashes = NULL;
2345#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2346 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002347}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002348#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002349
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002350#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002351#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002352/*
2353 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002354 *
2355 * mbedtls_ssl_setup() takes the provided list
2356 * and translates it to a list of IANA TLS group identifiers,
2357 * stored in ssl->handshake->group_list.
2358 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002359 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002360void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002361 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002362{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002363 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002364 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002365}
Brett Warrene0edc842021-08-17 09:53:13 +01002366#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002367#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002368
Brett Warrene0edc842021-08-17 09:53:13 +01002369/*
2370 * Set the allowed groups
2371 */
2372void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2373 const uint16_t *group_list )
2374{
2375#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2376 conf->curve_list = NULL;
2377#endif
2378 conf->group_list = group_list;
2379}
2380
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002381#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002383{
Hanno Becker947194e2017-04-07 13:25:49 +01002384 /* Initialize to suppress unnecessary compiler warning */
2385 size_t hostname_len = 0;
2386
2387 /* Check if new hostname is valid before
2388 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002389 if( hostname != NULL )
2390 {
2391 hostname_len = strlen( hostname );
2392
2393 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2395 }
2396
2397 /* Now it's clear that we will overwrite the old hostname,
2398 * so we can free it safely */
2399
2400 if( ssl->hostname != NULL )
2401 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002402 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002403 mbedtls_free( ssl->hostname );
2404 }
2405
2406 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002407
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002409 {
2410 ssl->hostname = NULL;
2411 }
2412 else
2413 {
2414 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002415 if( ssl->hostname == NULL )
2416 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002417
Hanno Becker947194e2017-04-07 13:25:49 +01002418 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002419
Hanno Becker947194e2017-04-07 13:25:49 +01002420 ssl->hostname[hostname_len] = '\0';
2421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
2423 return( 0 );
2424}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002425#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002427#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002430 const unsigned char *, size_t),
2431 void *p_sni )
2432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002433 conf->f_sni = f_sni;
2434 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002439int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002440{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002441 size_t cur_len, tot_len;
2442 const char **p;
2443
2444 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002445 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2446 * MUST NOT be truncated."
2447 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002448 */
2449 tot_len = 0;
2450 for( p = protos; *p != NULL; p++ )
2451 {
2452 cur_len = strlen( *p );
2453 tot_len += cur_len;
2454
Ronald Cron8216dd32020-04-23 16:41:44 +02002455 if( ( cur_len == 0 ) ||
2456 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2457 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002459 }
2460
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002461 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002462
2463 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002464}
2465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002467{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002468 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002471
Johan Pascalb62bb512015-12-03 21:56:45 +01002472#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002473void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2474 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002475{
2476 conf->dtls_srtp_mki_support = support_mki_value;
2477}
2478
Ron Eldoref72faf2018-07-12 11:54:20 +03002479int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2480 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002481 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002482{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002483 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002484 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002486 }
Ron Eldor591f1622018-01-22 12:30:04 +02002487
2488 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002489 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002490 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002491 }
Ron Eldor591f1622018-01-22 12:30:04 +02002492
2493 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2494 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002495 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002496}
2497
Ron Eldoref72faf2018-07-12 11:54:20 +03002498int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002499 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002500{
Johan Pascal253d0262020-09-22 13:04:45 +02002501 const mbedtls_ssl_srtp_profile *p;
2502 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002503
Johan Pascal253d0262020-09-22 13:04:45 +02002504 /* check the profiles list: all entry must be valid,
2505 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002506 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2507 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2508 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002509 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002510 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002511 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002512 list_size++;
2513 }
2514 else
2515 {
2516 /* unsupported value, stop parsing and set the size to an error value */
2517 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002518 }
2519 }
2520
Johan Pascal5ef72d22020-10-28 17:05:47 +01002521 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002522 {
Johan Pascal253d0262020-09-22 13:04:45 +02002523 conf->dtls_srtp_profile_list = NULL;
2524 conf->dtls_srtp_profile_list_len = 0;
2525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2526 }
2527
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002528 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002529 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002530
2531 return( 0 );
2532}
2533
Johan Pascal5ef72d22020-10-28 17:05:47 +01002534void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2535 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002536{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002537 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2538 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002539 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002540 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002541 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002542 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002543 else
2544 {
2545 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002546 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2547 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002548 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002549}
Johan Pascalb62bb512015-12-03 21:56:45 +01002550#endif /* MBEDTLS_SSL_DTLS_SRTP */
2551
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302552#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002553void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002554{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002555 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002556}
2557
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002558void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002559{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002560 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002561}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302562#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002563
Janos Follath088ce432017-04-10 12:42:31 +01002564#if defined(MBEDTLS_SSL_SRV_C)
2565void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2566 char cert_req_ca_list )
2567{
2568 conf->cert_req_ca_list = cert_req_ca_list;
2569}
2570#endif
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002573void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002575 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002576}
2577#endif
2578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002580void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002581{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002582 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002583}
2584#endif
2585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002587int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002590 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002593 }
2594
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002595 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002596
2597 return( 0 );
2598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002601void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002603 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002604}
2605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002607void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002609 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002610}
2611
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002612void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002614 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002615}
2616
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002617void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002618 const unsigned char period[8] )
2619{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002620 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002625#if defined(MBEDTLS_SSL_CLI_C)
2626void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002627{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002628 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002629}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002630#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002631
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002632#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002633
Jerry Yud0766ec2022-09-22 10:46:57 +08002634#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002635void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2636 uint16_t num_tickets )
2637{
Jerry Yud0766ec2022-09-22 10:46:57 +08002638 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002639}
2640#endif
2641
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002642void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2643 mbedtls_ssl_ticket_write_t *f_ticket_write,
2644 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2645 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002646{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002647 conf->f_ticket_write = f_ticket_write;
2648 conf->f_ticket_parse = f_ticket_parse;
2649 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002650}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002651#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002653
Hanno Becker7e6c1782021-06-08 09:24:55 +01002654void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2655 mbedtls_ssl_export_keys_t *f_export_keys,
2656 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002657{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002658 ssl->f_export_keys = f_export_keys;
2659 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002660}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002661
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002662#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002663void mbedtls_ssl_conf_async_private_cb(
2664 mbedtls_ssl_config *conf,
2665 mbedtls_ssl_async_sign_t *f_async_sign,
2666 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2667 mbedtls_ssl_async_resume_t *f_async_resume,
2668 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002669 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002670{
2671 conf->f_async_sign_start = f_async_sign;
2672 conf->f_async_decrypt_start = f_async_decrypt;
2673 conf->f_async_resume = f_async_resume;
2674 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002675 conf->p_async_config_data = async_config_data;
2676}
2677
Gilles Peskine8f97af72018-04-26 11:46:10 +02002678void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2679{
2680 return( conf->p_async_config_data );
2681}
2682
Gilles Peskine1febfef2018-04-30 11:54:39 +02002683void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002684{
2685 if( ssl->handshake == NULL )
2686 return( NULL );
2687 else
2688 return( ssl->handshake->user_async_ctx );
2689}
2690
Gilles Peskine1febfef2018-04-30 11:54:39 +02002691void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002692 void *ctx )
2693{
2694 if( ssl->handshake != NULL )
2695 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002696}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002697#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002698
Paul Bakker5121ce52009-01-03 21:22:43 +00002699/*
2700 * SSL get accessors
2701 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002702uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002703{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002704 if( ssl->session != NULL )
2705 return( ssl->session->verify_result );
2706
2707 if( ssl->session_negotiate != NULL )
2708 return( ssl->session_negotiate->verify_result );
2709
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002710 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711}
2712
Glenn Strauss8f526902022-01-13 00:04:49 -05002713int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2714{
2715 if( ssl == NULL || ssl->session == NULL )
2716 return( 0 );
2717
2718 return( ssl->session->ciphersuite );
2719}
2720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002722{
Paul Bakker926c8e42013-03-06 10:23:34 +01002723 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002724 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002727}
2728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002733 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002734 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002735 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002736 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002737 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002738 default:
2739 return( "unknown (DTLS)" );
2740 }
2741 }
2742#endif
2743
Glenn Strauss60bfe602022-03-14 19:04:24 -04002744 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002745 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002746 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002747 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002748 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002749 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002750 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002751 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002752 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002753}
2754
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002755#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002756size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2757{
David Horstmann95d516f2021-05-04 18:36:56 +01002758 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002759 size_t read_mfl;
2760
2761 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2762 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2763 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2764 {
2765 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2766 }
2767
2768 /* Check if a smaller max length was negotiated */
2769 if( ssl->session_out != NULL )
2770 {
2771 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2772 if( read_mfl < max_len )
2773 {
2774 max_len = read_mfl;
2775 }
2776 }
2777
2778 // During a handshake, use the value being negotiated
2779 if( ssl->session_negotiate != NULL )
2780 {
2781 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2782 if( read_mfl < max_len )
2783 {
2784 max_len = read_mfl;
2785 }
2786 }
2787
2788 return( max_len );
2789}
2790
2791size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002792{
2793 size_t max_len;
2794
2795 /*
2796 * Assume mfl_code is correct since it was checked when set
2797 */
Angus Grattond8213d02016-05-25 20:56:48 +10002798 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002799
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002800 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002801 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002802 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002803 {
Angus Grattond8213d02016-05-25 20:56:48 +10002804 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002805 }
2806
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002807 /* During a handshake, use the value being negotiated */
2808 if( ssl->session_negotiate != NULL &&
2809 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2810 {
2811 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2812 }
2813
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002814 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002815}
2816#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2817
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002819size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002820{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002821 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2823 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2824 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2825 return ( 0 );
2826
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002827 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2828 return( ssl->mtu );
2829
2830 if( ssl->mtu == 0 )
2831 return( ssl->handshake->mtu );
2832
2833 return( ssl->mtu < ssl->handshake->mtu ?
2834 ssl->mtu : ssl->handshake->mtu );
2835}
2836#endif /* MBEDTLS_SSL_PROTO_DTLS */
2837
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002838int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2839{
2840 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2841
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002842#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2843 !defined(MBEDTLS_SSL_PROTO_DTLS)
2844 (void) ssl;
2845#endif
2846
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002847#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002848 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002849
2850 if( max_len > mfl )
2851 max_len = mfl;
2852#endif
2853
2854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002855 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002856 {
Hanno Becker89490712020-02-05 10:50:12 +00002857 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002858 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2859 const size_t overhead = (size_t) ret;
2860
2861 if( ret < 0 )
2862 return( ret );
2863
2864 if( mtu <= overhead )
2865 {
2866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2867 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2868 }
2869
2870 if( max_len > mtu - overhead )
2871 max_len = mtu - overhead;
2872 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002873#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002874
Hanno Becker0defedb2018-08-10 12:35:02 +01002875#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2876 !defined(MBEDTLS_SSL_PROTO_DTLS)
2877 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002878#endif
2879
2880 return( (int) max_len );
2881}
2882
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002883int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2884{
2885 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2886
2887#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2888 (void) ssl;
2889#endif
2890
2891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2892 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2893
2894 if( max_len > mfl )
2895 max_len = mfl;
2896#endif
2897
2898 return( (int) max_len );
2899}
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#if defined(MBEDTLS_X509_CRT_PARSE_C)
2902const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002903{
2904 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002905 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002906
Hanno Beckere6824572019-02-07 13:18:46 +00002907#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002908 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002909#else
2910 return( NULL );
2911#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002916int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2917 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002918{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002919 int ret;
2920
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002921 if( ssl == NULL ||
2922 dst == NULL ||
2923 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002924 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002927 }
2928
Hanno Beckere810bbc2021-05-14 16:01:05 +01002929 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2930 * idempotent: Each session can only be exported once.
2931 *
2932 * (This is in preparation for TLS 1.3 support where we will
2933 * need the ability to export multiple sessions (aka tickets),
2934 * which will be achieved by calling mbedtls_ssl_get_session()
2935 * multiple times until it fails.)
2936 *
2937 * Check whether we have already exported the current session,
2938 * and fail if so.
2939 */
2940 if( ssl->session->exported == 1 )
2941 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2942
2943 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2944 if( ret != 0 )
2945 return( ret );
2946
2947 /* Remember that we've exported the session. */
2948 ssl->session->exported = 1;
2949 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002950}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002952
Paul Bakker5121ce52009-01-03 21:22:43 +00002953/*
Hanno Beckera835da52019-05-16 12:39:07 +01002954 * Define ticket header determining Mbed TLS version
2955 * and structure of the ticket.
2956 */
2957
Hanno Becker94ef3b32019-05-16 12:50:45 +01002958/*
Hanno Becker50b59662019-05-28 14:30:45 +01002959 * Define bitflag determining compile-time settings influencing
2960 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002961 */
2962
Hanno Becker50b59662019-05-28 14:30:45 +01002963#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002964#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002965#else
Hanno Becker3e088662019-05-29 11:10:18 +01002966#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002967#endif /* MBEDTLS_HAVE_TIME */
2968
2969#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002970#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002971#else
Hanno Becker3e088662019-05-29 11:10:18 +01002972#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002973#endif /* MBEDTLS_X509_CRT_PARSE_C */
2974
2975#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002976#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002977#else
Hanno Becker3e088662019-05-29 11:10:18 +01002978#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2980
2981#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002982#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002983#else
Hanno Becker3e088662019-05-29 11:10:18 +01002984#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002985#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2986
Hanno Becker94ef3b32019-05-16 12:50:45 +01002987#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002988#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002989#else
Hanno Becker3e088662019-05-29 11:10:18 +01002990#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002991#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
2992
Hanno Becker94ef3b32019-05-16 12:50:45 +01002993#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2994#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
2995#else
2996#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
2997#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2998
Hanno Becker3e088662019-05-29 11:10:18 +01002999#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3000#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3001#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3002#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003003#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3004#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003005
Hanno Becker50b59662019-05-28 14:30:45 +01003006#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003007 ( (uint16_t) ( \
3008 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3009 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3010 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3011 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003012 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003013 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003014
Hanno Beckerf8787072019-05-16 12:41:07 +01003015static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003016 MBEDTLS_VERSION_MAJOR,
3017 MBEDTLS_VERSION_MINOR,
3018 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003019 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3020 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003021};
Hanno Beckera835da52019-05-16 12:39:07 +01003022
3023/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003024 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003025 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003026 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003027 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003028 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003029 * opaque mbedtls_version[3]; // library version: major, minor, patch
3030 * opaque session_format[2]; // library-version specific 16-bit field
3031 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003032 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003033 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003034 * Note: When updating the format, remember to keep
3035 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003036 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003037 * // In this version, `session_format` determines
3038 * // the setting of those compile-time
3039 * // configuration options which influence
3040 * // the structure of mbedtls_ssl_session.
3041 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003042 * uint8_t minor_ver; // Protocol minor version. Possible values:
3043 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003044 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003045 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003046 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003047 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003048 * serialized_session_tls12 data;
3049 *
3050 * };
3051 *
3052 * } serialized_session;
3053 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003054 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003055
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003056MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003057static int ssl_session_save( const mbedtls_ssl_session *session,
3058 unsigned char omit_header,
3059 unsigned char *buf,
3060 size_t buf_len,
3061 size_t *olen )
3062{
3063 unsigned char *p = buf;
3064 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003065 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003066#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3067 size_t out_len;
3068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3069#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003070 if( session == NULL )
3071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3072
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003073 if( !omit_header )
3074 {
3075 /*
3076 * Add Mbed TLS version identifier
3077 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003078 used += sizeof( ssl_serialized_session_header );
3079
3080 if( used <= buf_len )
3081 {
3082 memcpy( p, ssl_serialized_session_header,
3083 sizeof( ssl_serialized_session_header ) );
3084 p += sizeof( ssl_serialized_session_header );
3085 }
3086 }
3087
3088 /*
3089 * TLS version identifier
3090 */
3091 used += 1;
3092 if( used <= buf_len )
3093 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003094 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003095 }
3096
3097 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003098 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003099 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003100 {
3101#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003102 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003103 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003104 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3106
Jerry Yu251a12e2022-07-13 15:15:48 +08003107#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3108 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003109 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3110 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003111 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003112 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003113 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003114#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3115
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003116 default:
3117 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3118 }
3119
3120 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003121 if( used > buf_len )
3122 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003123
3124 return( 0 );
3125}
3126
3127/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003128 * Public wrapper for ssl_session_save()
3129 */
3130int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3131 unsigned char *buf,
3132 size_t buf_len,
3133 size_t *olen )
3134{
3135 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3136}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003137
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003138/*
3139 * Deserialize session, see mbedtls_ssl_session_save() for format.
3140 *
3141 * This internal version is wrapped by a public function that cleans up in
3142 * case of error, and has an extra option omit_header.
3143 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003144MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003145static int ssl_session_load( mbedtls_ssl_session *session,
3146 unsigned char omit_header,
3147 const unsigned char *buf,
3148 size_t len )
3149{
3150 const unsigned char *p = buf;
3151 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003152 size_t remaining_len;
3153
3154
3155 if( session == NULL )
3156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003157
3158 if( !omit_header )
3159 {
3160 /*
3161 * Check Mbed TLS version identifier
3162 */
3163
3164 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3166
3167 if( memcmp( p, ssl_serialized_session_header,
3168 sizeof( ssl_serialized_session_header ) ) != 0 )
3169 {
3170 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3171 }
3172 p += sizeof( ssl_serialized_session_header );
3173 }
3174
3175 /*
3176 * TLS version identifier
3177 */
3178 if( 1 > (size_t)( end - p ) )
3179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003180 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003181
3182 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003183 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003184 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003185 {
3186#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003187 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003188 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003189#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3190
Jerry Yu438ddd82022-07-07 06:55:50 +00003191#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3192 case MBEDTLS_SSL_VERSION_TLS1_3:
3193 return( ssl_tls13_session_load( session, p, remaining_len ) );
3194#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3195
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003196 default:
3197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3198 }
3199}
3200
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003201/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003202 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003203 */
3204int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3205 const unsigned char *buf,
3206 size_t len )
3207{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003208 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003209
3210 if( ret != 0 )
3211 mbedtls_ssl_session_free( session );
3212
3213 return( ret );
3214}
3215
3216/*
Paul Bakker1961b702013-01-25 14:49:24 +01003217 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003219MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003220static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3221{
3222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3223
Ronald Cron66dbf912022-02-02 15:33:46 +01003224 /*
3225 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003226 * that were written into the output buffer by the previous handshake step,
3227 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003228 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3229 * We proceed to the next handshake step only when all data from the
3230 * previous one have been sent to the peer, thus we make sure that this is
3231 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3232 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3233 * we have to wait before to go ahead.
3234 * In the case of TLS 1.3, handshake step handlers do not send data to the
3235 * peer. Data are only sent here and through
3236 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003237 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003238 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003239 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3240 return( ret );
3241
3242#if defined(MBEDTLS_SSL_PROTO_DTLS)
3243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3244 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3245 {
3246 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3247 return( ret );
3248 }
3249#endif /* MBEDTLS_SSL_PROTO_DTLS */
3250
3251 return( ret );
3252}
3253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255{
Hanno Becker41934dd2021-08-07 19:13:43 +01003256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Hanno Becker41934dd2021-08-07 19:13:43 +01003258 if( ssl == NULL ||
3259 ssl->conf == NULL ||
3260 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003261 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003262 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003264 }
3265
3266 ret = ssl_prepare_handshake_step( ssl );
3267 if( ret != 0 )
3268 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003269
Jerry Yue7047812021-09-13 19:26:39 +08003270 ret = mbedtls_ssl_handle_pending_alert( ssl );
3271 if( ret != 0 )
3272 goto cleanup;
3273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003275 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003276 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3278 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003279
Ronald Cron9f0fba32022-02-10 16:45:15 +01003280 switch( ssl->state )
3281 {
3282 case MBEDTLS_SSL_HELLO_REQUEST:
3283 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3284 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003285
Ronald Cron9f0fba32022-02-10 16:45:15 +01003286 case MBEDTLS_SSL_CLIENT_HELLO:
3287 ret = mbedtls_ssl_write_client_hello( ssl );
3288 break;
3289
3290 default:
3291#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003292 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003293 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3294 else
3295 ret = mbedtls_ssl_handshake_client_step( ssl );
3296#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3297 ret = mbedtls_ssl_handshake_client_step( ssl );
3298#else
3299 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3300#endif
3301 }
Jerry Yub9930e72021-08-06 17:11:51 +08003302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003306 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003307#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003308 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003309 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003310#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003311
3312#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3313 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3314 ret = mbedtls_ssl_handshake_server_step( ssl );
3315#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3316 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003317#endif
3318
Jerry Yue7047812021-09-13 19:26:39 +08003319 if( ret != 0 )
3320 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003321 /* handshake_step return error. And it is same
3322 * with alert_reason.
3323 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003324 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003325 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003326 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003327 goto cleanup;
3328 }
3329 }
3330
3331cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003332 return( ret );
3333}
3334
3335/*
3336 * Perform the SSL handshake
3337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003339{
3340 int ret = 0;
3341
Hanno Beckera817ea42020-10-20 15:20:23 +01003342 /* Sanity checks */
3343
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003344 if( ssl == NULL || ssl->conf == NULL )
3345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3346
Hanno Beckera817ea42020-10-20 15:20:23 +01003347#if defined(MBEDTLS_SSL_PROTO_DTLS)
3348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3349 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3350 {
3351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3352 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3354 }
3355#endif /* MBEDTLS_SSL_PROTO_DTLS */
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003358
Hanno Beckera817ea42020-10-20 15:20:23 +01003359 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003360 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003363
3364 if( ret != 0 )
3365 break;
3366 }
3367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 return( ret );
3371}
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373#if defined(MBEDTLS_SSL_RENEGOTIATION)
3374#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003375/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003376 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003377 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003378MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003380{
Janos Follath865b3eb2019-12-16 11:46:15 +00003381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003384
3385 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3387 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003388
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003389 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003390 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003392 return( ret );
3393 }
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003396
3397 return( 0 );
3398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003400
3401/*
3402 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 * - any side: calling mbedtls_ssl_renegotiate(),
3404 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3405 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003406 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003407 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003409 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003410int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003411{
Janos Follath865b3eb2019-12-16 11:46:15 +00003412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003415
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003416 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3417 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003418
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003419 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3420 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003424 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003425 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003426 ssl->handshake->out_msg_seq = 1;
3427 else
3428 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003429 }
3430#endif
3431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3433 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003438 return( ret );
3439 }
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003442
3443 return( 0 );
3444}
3445
3446/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003447 * Renegotiate current connection on client,
3448 * or request renegotiation on server
3449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003453
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003454 if( ssl == NULL || ssl->conf == NULL )
3455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003458 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003459 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003460 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003461 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003465
3466 /* Did we already try/start sending HelloRequest? */
3467 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003469
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003470 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475 /*
3476 * On client, either start the renegotiation process or,
3477 * if already in progress, continue the handshake
3478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003480 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003481 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003483
Hanno Becker40cdaa12020-02-05 10:48:27 +00003484 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003485 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003487 return( ret );
3488 }
3489 }
3490 else
3491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003495 return( ret );
3496 }
3497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003499
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003500 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003503
Gilles Peskine9b562d52018-04-25 20:32:43 +02003504void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003505{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003506 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3507
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003508 if( handshake == NULL )
3509 return;
3510
Brett Warrene0edc842021-08-17 09:53:13 +01003511#if defined(MBEDTLS_ECP_C)
3512#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3513 if ( ssl->handshake->group_list_heap_allocated )
3514 mbedtls_free( (void*) handshake->group_list );
3515 handshake->group_list = NULL;
3516#endif /* MBEDTLS_DEPRECATED_REMOVED */
3517#endif /* MBEDTLS_ECP_C */
3518
Jerry Yuf017ee42022-01-12 15:49:48 +08003519#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3520#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3521 if ( ssl->handshake->sig_algs_heap_allocated )
3522 mbedtls_free( (void*) handshake->sig_algs );
3523 handshake->sig_algs = NULL;
3524#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003525#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3526 if( ssl->handshake->certificate_request_context )
3527 {
3528 mbedtls_free( (void*) handshake->certificate_request_context );
3529 }
3530#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003531#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3532
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003533#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3534 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3535 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003536 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003537 handshake->async_in_progress = 0;
3538 }
3539#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3540
Andrzej Kurek25f27152022-08-17 16:09:31 -04003541#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003542#if defined(MBEDTLS_USE_PSA_CRYPTO)
3543 psa_hash_abort( &handshake->fin_sha256_psa );
3544#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003545 mbedtls_sha256_free( &handshake->fin_sha256 );
3546#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003547#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003548#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003550 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003551#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003552 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003553#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003554#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#if defined(MBEDTLS_DHM_C)
3557 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003558#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003559#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003561#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003562#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003563 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003564#if defined(MBEDTLS_SSL_CLI_C)
3565 mbedtls_free( handshake->ecjpake_cache );
3566 handshake->ecjpake_cache = NULL;
3567 handshake->ecjpake_cache_len = 0;
3568#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003569#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003570
Janos Follath4ae5c292016-02-10 11:27:43 +00003571#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3572 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003573 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003575#endif
3576
Gilles Peskineeccd8882020-03-10 12:19:08 +01003577#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003578#if defined(MBEDTLS_USE_PSA_CRYPTO)
3579 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3580 {
3581 /* The maintenance of the external PSK key slot is the
3582 * user's responsibility. */
3583 if( ssl->handshake->psk_opaque_is_internal )
3584 {
3585 psa_destroy_key( ssl->handshake->psk_opaque );
3586 ssl->handshake->psk_opaque_is_internal = 0;
3587 }
3588 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3589 }
Neil Armstronge952a302022-05-03 10:22:14 +02003590#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003591 if( handshake->psk != NULL )
3592 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003593 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003594 mbedtls_free( handshake->psk );
3595 }
Neil Armstronge952a302022-05-03 10:22:14 +02003596#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003597#endif
3598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3600 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003601 /*
3602 * Free only the linked list wrapper, not the keys themselves
3603 * since the belong to the SNI callback
3604 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003605 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003607
Gilles Peskineeccd8882020-03-10 12:19:08 +01003608#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003609 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003610 if( handshake->ecrs_peer_cert != NULL )
3611 {
3612 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3613 mbedtls_free( handshake->ecrs_peer_cert );
3614 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003615#endif
3616
Hanno Becker75173122019-02-06 16:18:31 +00003617#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3618 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3619 mbedtls_pk_free( &handshake->peer_pubkey );
3620#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3621
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003622#if defined(MBEDTLS_SSL_CLI_C) && \
3623 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3624 mbedtls_free( handshake->cookie );
3625#endif /* MBEDTLS_SSL_CLI_C &&
3626 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003627
3628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003629 mbedtls_ssl_flight_free( handshake->flight );
3630 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003631#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003632
Ronald Cronf12b81d2022-03-15 10:42:41 +01003633#if defined(MBEDTLS_ECDH_C) && \
3634 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003635 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003636 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003637#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3638
Ronald Cron6f135e12021-12-08 16:57:54 +01003639#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003640 mbedtls_ssl_transform_free( handshake->transform_handshake );
3641 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003642 mbedtls_free( handshake->transform_earlydata );
3643 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003644#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003645
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003646
3647#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3648 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3649 * processes datagrams and the fact that a datagram is allowed to have
3650 * several records in it, it is possible that the I/O buffers are not
3651 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003652 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3653 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003654#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003655
Jerry Yuba9c7272021-10-30 11:54:10 +08003656 /* mbedtls_platform_zeroize MUST be last one in this function */
3657 mbedtls_platform_zeroize( handshake,
3658 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003659}
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003662{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003663 if( session == NULL )
3664 return;
3665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003667 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003668#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003669
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003670#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003672#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003673
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003674 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003675}
3676
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003677#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003678
3679#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3680#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3681#else
3682#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3683#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3684
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003685#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003686
3687#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3688#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3689#else
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3691#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3692
3693#if defined(MBEDTLS_SSL_ALPN)
3694#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3695#else
3696#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3697#endif /* MBEDTLS_SSL_ALPN */
3698
3699#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3700#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3701#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3702#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3703
3704#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3705 ( (uint32_t) ( \
3706 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3707 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3708 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3709 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3710 0u ) )
3711
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003712static unsigned char ssl_serialized_context_header[] = {
3713 MBEDTLS_VERSION_MAJOR,
3714 MBEDTLS_VERSION_MINOR,
3715 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003716 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3717 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3718 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3719 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3720 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003721};
3722
Paul Bakker5121ce52009-01-03 21:22:43 +00003723/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003724 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003725 *
3726 * The format of the serialized data is:
3727 * (in the presentation language of TLS, RFC 8446 section 3)
3728 *
3729 * // header
3730 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003731 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003732 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003733 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003734 * Note: When updating the format, remember to keep these
3735 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003736 *
3737 * // session sub-structure
3738 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3739 * // transform sub-structure
3740 * uint8 random[64]; // ServerHello.random+ClientHello.random
3741 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3742 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3743 * // fields from ssl_context
3744 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3745 * uint64 in_window_top; // DTLS: last validated record seq_num
3746 * uint64 in_window; // DTLS: bitmask for replay protection
3747 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3748 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3749 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3750 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3751 *
3752 * Note that many fields of the ssl_context or sub-structures are not
3753 * serialized, as they fall in one of the following categories:
3754 *
3755 * 1. forced value (eg in_left must be 0)
3756 * 2. pointer to dynamically-allocated memory (eg session, transform)
3757 * 3. value can be re-derived from other data (eg session keys from MS)
3758 * 4. value was temporary (eg content of input buffer)
3759 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003760 */
3761int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3762 unsigned char *buf,
3763 size_t buf_len,
3764 size_t *olen )
3765{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003766 unsigned char *p = buf;
3767 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003768 size_t session_len;
3769 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003770
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003771 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003772 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3773 * this function's documentation.
3774 *
3775 * These are due to assumptions/limitations in the implementation. Some of
3776 * them are likely to stay (no handshake in progress) some might go away
3777 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003778 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003779 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003780 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003781 {
3782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003784 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003785 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003789 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003790 /* Double-check that sub-structures are indeed ready */
3791 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003795 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796 /* There must be no pending incoming or outgoing data */
3797 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003798 {
3799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003801 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003802 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003806 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003807 /* Protocol must be DLTS, not TLS */
3808 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003809 {
3810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003812 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003814 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003815 {
3816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003818 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003819 /* We must be using an AEAD ciphersuite */
3820 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003821 {
3822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003824 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003825 /* Renegotiation must not be enabled */
3826#if defined(MBEDTLS_SSL_RENEGOTIATION)
3827 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003828 {
3829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003831 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003832#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003833
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003834 /*
3835 * Version and format identifier
3836 */
3837 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003838
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003839 if( used <= buf_len )
3840 {
3841 memcpy( p, ssl_serialized_context_header,
3842 sizeof( ssl_serialized_context_header ) );
3843 p += sizeof( ssl_serialized_context_header );
3844 }
3845
3846 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003847 * Session (length + data)
3848 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003849 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003850 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3851 return( ret );
3852
3853 used += 4 + session_len;
3854 if( used <= buf_len )
3855 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003856 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3857 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003858
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003859 ret = ssl_session_save( ssl->session, 1,
3860 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003861 if( ret != 0 )
3862 return( ret );
3863
3864 p += session_len;
3865 }
3866
3867 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003868 * Transform
3869 */
3870 used += sizeof( ssl->transform->randbytes );
3871 if( used <= buf_len )
3872 {
3873 memcpy( p, ssl->transform->randbytes,
3874 sizeof( ssl->transform->randbytes ) );
3875 p += sizeof( ssl->transform->randbytes );
3876 }
3877
3878#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3879 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3880 if( used <= buf_len )
3881 {
3882 *p++ = ssl->transform->in_cid_len;
3883 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3884 p += ssl->transform->in_cid_len;
3885
3886 *p++ = ssl->transform->out_cid_len;
3887 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3888 p += ssl->transform->out_cid_len;
3889 }
3890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3891
3892 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003893 * Saved fields from top-level ssl_context structure
3894 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003895 used += 4;
3896 if( used <= buf_len )
3897 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003898 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3899 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003900 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003901
3902#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3903 used += 16;
3904 if( used <= buf_len )
3905 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003906 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3907 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003908
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003909 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3910 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003911 }
3912#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3913
3914#if defined(MBEDTLS_SSL_PROTO_DTLS)
3915 used += 1;
3916 if( used <= buf_len )
3917 {
3918 *p++ = ssl->disable_datagram_packing;
3919 }
3920#endif /* MBEDTLS_SSL_PROTO_DTLS */
3921
Jerry Yuae0b2e22021-10-08 15:21:19 +08003922 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003923 if( used <= buf_len )
3924 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003925 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3926 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003927 }
3928
3929#if defined(MBEDTLS_SSL_PROTO_DTLS)
3930 used += 2;
3931 if( used <= buf_len )
3932 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003933 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3934 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003935 }
3936#endif /* MBEDTLS_SSL_PROTO_DTLS */
3937
3938#if defined(MBEDTLS_SSL_ALPN)
3939 {
3940 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003941 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003942 : 0;
3943
3944 used += 1 + alpn_len;
3945 if( used <= buf_len )
3946 {
3947 *p++ = alpn_len;
3948
3949 if( ssl->alpn_chosen != NULL )
3950 {
3951 memcpy( p, ssl->alpn_chosen, alpn_len );
3952 p += alpn_len;
3953 }
3954 }
3955 }
3956#endif /* MBEDTLS_SSL_ALPN */
3957
3958 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003959 * Done
3960 */
3961 *olen = used;
3962
3963 if( used > buf_len )
3964 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003965
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003966 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3967
Hanno Becker43aefe22020-02-05 10:44:56 +00003968 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003969}
3970
3971/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003972 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003973 *
3974 * This internal version is wrapped by a public function that cleans up in
3975 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003976 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003977MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003978static int ssl_context_load( mbedtls_ssl_context *ssl,
3979 const unsigned char *buf,
3980 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003981{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003982 const unsigned char *p = buf;
3983 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003984 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003985 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003986
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003987 /*
3988 * The context should have been freshly setup or reset.
3989 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003990 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003991 * renegotiating, or if the user mistakenly loaded a session first.)
3992 */
3993 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
3994 ssl->session != NULL )
3995 {
3996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3997 }
3998
3999 /*
4000 * We can't check that the config matches the initial one, but we can at
4001 * least check it matches the requirements for serializing.
4002 */
4003 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004004 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4005 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004006#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004007 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004008#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004009 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004010 {
4011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4012 }
4013
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004014 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4015
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004016 /*
4017 * Check version identifier
4018 */
4019 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4021
4022 if( memcmp( p, ssl_serialized_context_header,
4023 sizeof( ssl_serialized_context_header ) ) != 0 )
4024 {
4025 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4026 }
4027 p += sizeof( ssl_serialized_context_header );
4028
4029 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004030 * Session
4031 */
4032 if( (size_t)( end - p ) < 4 )
4033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4034
4035 session_len = ( (size_t) p[0] << 24 ) |
4036 ( (size_t) p[1] << 16 ) |
4037 ( (size_t) p[2] << 8 ) |
4038 ( (size_t) p[3] );
4039 p += 4;
4040
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004041 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004042 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004043 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004044 ssl->session_in = ssl->session;
4045 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004046 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047
4048 if( (size_t)( end - p ) < session_len )
4049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4050
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004051 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004052 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004053 {
4054 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004055 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004056 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004057
4058 p += session_len;
4059
4060 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004061 * Transform
4062 */
4063
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004064 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004065 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004066 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004067 ssl->transform_in = ssl->transform;
4068 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004069 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004070
4071 /* Read random bytes and populate structure */
4072 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004074#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004075 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004076 ssl->session->ciphersuite,
4077 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004078#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004079 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004080#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004081 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4082 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004083 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004084 ssl->conf->endpoint,
4085 ssl );
4086 if( ret != 0 )
4087 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004089 p += sizeof( ssl->transform->randbytes );
4090
4091#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4092 /* Read connection IDs and store them */
4093 if( (size_t)( end - p ) < 1 )
4094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4095
4096 ssl->transform->in_cid_len = *p++;
4097
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004098 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4100
4101 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4102 p += ssl->transform->in_cid_len;
4103
4104 ssl->transform->out_cid_len = *p++;
4105
4106 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4108
4109 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4110 p += ssl->transform->out_cid_len;
4111#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4112
4113 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004114 * Saved fields from top-level ssl_context structure
4115 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004116 if( (size_t)( end - p ) < 4 )
4117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4118
4119 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4120 ( (uint32_t) p[1] << 16 ) |
4121 ( (uint32_t) p[2] << 8 ) |
4122 ( (uint32_t) p[3] );
4123 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004124
4125#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4126 if( (size_t)( end - p ) < 16 )
4127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4128
4129 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4130 ( (uint64_t) p[1] << 48 ) |
4131 ( (uint64_t) p[2] << 40 ) |
4132 ( (uint64_t) p[3] << 32 ) |
4133 ( (uint64_t) p[4] << 24 ) |
4134 ( (uint64_t) p[5] << 16 ) |
4135 ( (uint64_t) p[6] << 8 ) |
4136 ( (uint64_t) p[7] );
4137 p += 8;
4138
4139 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4140 ( (uint64_t) p[1] << 48 ) |
4141 ( (uint64_t) p[2] << 40 ) |
4142 ( (uint64_t) p[3] << 32 ) |
4143 ( (uint64_t) p[4] << 24 ) |
4144 ( (uint64_t) p[5] << 16 ) |
4145 ( (uint64_t) p[6] << 8 ) |
4146 ( (uint64_t) p[7] );
4147 p += 8;
4148#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4149
4150#if defined(MBEDTLS_SSL_PROTO_DTLS)
4151 if( (size_t)( end - p ) < 1 )
4152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4153
4154 ssl->disable_datagram_packing = *p++;
4155#endif /* MBEDTLS_SSL_PROTO_DTLS */
4156
Jerry Yud9a94fe2021-09-28 18:58:59 +08004157 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004159 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4160 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004161
4162#if defined(MBEDTLS_SSL_PROTO_DTLS)
4163 if( (size_t)( end - p ) < 2 )
4164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4165
4166 ssl->mtu = ( p[0] << 8 ) | p[1];
4167 p += 2;
4168#endif /* MBEDTLS_SSL_PROTO_DTLS */
4169
4170#if defined(MBEDTLS_SSL_ALPN)
4171 {
4172 uint8_t alpn_len;
4173 const char **cur;
4174
4175 if( (size_t)( end - p ) < 1 )
4176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4177
4178 alpn_len = *p++;
4179
4180 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4181 {
4182 /* alpn_chosen should point to an item in the configured list */
4183 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4184 {
4185 if( strlen( *cur ) == alpn_len &&
4186 memcmp( p, cur, alpn_len ) == 0 )
4187 {
4188 ssl->alpn_chosen = *cur;
4189 break;
4190 }
4191 }
4192 }
4193
4194 /* can only happen on conf mismatch */
4195 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4197
4198 p += alpn_len;
4199 }
4200#endif /* MBEDTLS_SSL_ALPN */
4201
4202 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004203 * Forced fields from top-level ssl_context structure
4204 *
4205 * Most of them already set to the correct value by mbedtls_ssl_init() and
4206 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4207 */
4208 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004209 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004210
Hanno Becker361b10d2019-08-30 10:42:49 +01004211 /* Adjust pointers for header fields of outgoing records to
4212 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004213 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004214
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004215#if defined(MBEDTLS_SSL_PROTO_DTLS)
4216 ssl->in_epoch = 1;
4217#endif
4218
4219 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4220 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004221 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4222 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004223 if( ssl->handshake != NULL )
4224 {
4225 mbedtls_ssl_handshake_free( ssl );
4226 mbedtls_free( ssl->handshake );
4227 ssl->handshake = NULL;
4228 }
4229
4230 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004231 * Done - should have consumed entire buffer
4232 */
4233 if( p != end )
4234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004235
4236 return( 0 );
4237}
4238
4239/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004240 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004241 */
4242int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4243 const unsigned char *buf,
4244 size_t len )
4245{
4246 int ret = ssl_context_load( context, buf, len );
4247
4248 if( ret != 0 )
4249 mbedtls_ssl_free( context );
4250
4251 return( ret );
4252}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004253#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004254
4255/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 * Free an SSL context
4257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004259{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004260 if( ssl == NULL )
4261 return;
4262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004265 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 {
sander-visserb8aa2072020-05-06 22:05:13 +02004267#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4268 size_t out_buf_len = ssl->out_buf_len;
4269#else
4270 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4271#endif
4272
Darryl Greenb33cc762019-11-28 14:29:44 +00004273 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004275 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 }
4277
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004278 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 {
sander-visserb8aa2072020-05-06 22:05:13 +02004280#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4281 size_t in_buf_len = ssl->in_buf_len;
4282#else
4283 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4284#endif
4285
Darryl Greenb33cc762019-11-28 14:29:44 +00004286 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004288 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 }
4290
Paul Bakker48916f92012-09-16 19:57:18 +00004291 if( ssl->transform )
4292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 mbedtls_ssl_transform_free( ssl->transform );
4294 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004295 }
4296
4297 if( ssl->handshake )
4298 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004299 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4301 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 mbedtls_free( ssl->handshake );
4304 mbedtls_free( ssl->transform_negotiate );
4305 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004306 }
4307
Ronald Cron6f135e12021-12-08 16:57:54 +01004308#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004309 mbedtls_ssl_transform_free( ssl->transform_application );
4310 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004311#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004312
Paul Bakkerc0463502013-02-14 11:19:38 +01004313 if( ssl->session )
4314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 mbedtls_ssl_session_free( ssl->session );
4316 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004317 }
4318
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004319#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004320 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004322 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004326
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004327#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004329#endif
4330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004332
Paul Bakker86f04f42013-02-14 11:20:09 +01004333 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004334 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004335}
4336
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004337/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004338 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004339 */
4340void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4341{
4342 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4343}
4344
Gilles Peskineae270bf2021-06-02 00:05:29 +02004345/* The selection should be the same as mbedtls_x509_crt_profile_default in
4346 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004347 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004348 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004349 * about this list.
4350 */
Brett Warrene0edc842021-08-17 09:53:13 +01004351static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004352#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004353 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004354#endif
4355#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004356 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004357#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004358#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004359 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004360#endif
4361#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004362 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004363#endif
4364#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004365 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004366#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004367#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004368 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004369#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004370#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004371 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004372#endif
4373#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004374 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004375#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004376 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004377};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004378
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004379static int ssl_preset_suiteb_ciphersuites[] = {
4380 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4381 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4382 0
4383};
4384
Gilles Peskineeccd8882020-03-10 12:19:08 +01004385#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004386
Jerry Yu909df7b2022-01-22 11:56:27 +08004387/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004388 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004389 * rules SHOULD be upheld.
4390 * - No duplicate entries.
4391 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004392 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004393 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004394 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004395static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004396
Andrzej Kurek25f27152022-08-17 16:09:31 -04004397#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 +08004398 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4399 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004400#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004401 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004402
Andrzej Kurek25f27152022-08-17 16:09:31 -04004403#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 +08004404 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4405 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004406#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004407 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4408
Andrzej Kurek25f27152022-08-17 16:09:31 -04004409#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 +08004410 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4411 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004412#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004413 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004414
Andrzej Kurek25f27152022-08-17 16:09:31 -04004415#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 +08004416 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004417#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 +08004418
Andrzej Kurek25f27152022-08-17 16:09:31 -04004419#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 +08004420 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004421#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 +08004422
Andrzej Kurek25f27152022-08-17 16:09:31 -04004423#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 +08004424 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004425#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 +08004426
Andrzej Kurek25f27152022-08-17 16:09:31 -04004427#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 +08004428 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4429#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4430
Andrzej Kurek25f27152022-08-17 16:09:31 -04004431#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 +08004432 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4433#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4434
Andrzej Kurek25f27152022-08-17 16:09:31 -04004435#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 +08004436 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4437#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4438
Gabor Mezei15b95a62022-05-09 16:37:58 +02004439 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004440};
4441
4442/* NOTICE: see above */
4443#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4444static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004445#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004446#if defined(MBEDTLS_ECDSA_C)
4447 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004448#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004449#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4450 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4451#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004452#if defined(MBEDTLS_RSA_C)
4453 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4454#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004455#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004456#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004457#if defined(MBEDTLS_ECDSA_C)
4458 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004459#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004460#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4461 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4462#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004463#if defined(MBEDTLS_RSA_C)
4464 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4465#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004466#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004467#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004468#if defined(MBEDTLS_ECDSA_C)
4469 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004470#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004471#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4472 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4473#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004474#if defined(MBEDTLS_RSA_C)
4475 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4476#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004477#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004478 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004479};
4480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4481/* NOTICE: see above */
4482static uint16_t ssl_preset_suiteb_sig_algs[] = {
4483
Andrzej Kurek25f27152022-08-17 16:09:31 -04004484#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 +08004485 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4486 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004487#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004488 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4489
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#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 +08004491 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4492 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004493#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004494 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#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 +08004497 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004498#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 +08004499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004502#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004503
Gabor Mezei15b95a62022-05-09 16:37:58 +02004504 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004505};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004506
Jerry Yu909df7b2022-01-22 11:56:27 +08004507/* NOTICE: see above */
4508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4509static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004510#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004511#if defined(MBEDTLS_ECDSA_C)
4512 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004513#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004514#if defined(MBEDTLS_RSA_C)
4515 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4516#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004517#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004519#if defined(MBEDTLS_ECDSA_C)
4520 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004521#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004522#if defined(MBEDTLS_RSA_C)
4523 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4524#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004525#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004526 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004527};
Jerry Yu909df7b2022-01-22 11:56:27 +08004528#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4529
Jerry Yu1a8b4812022-01-20 17:56:50 +08004530#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004531
Brett Warrene0edc842021-08-17 09:53:13 +01004532static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004533#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004534 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004535#endif
4536#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004537 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004538#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004539 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004540};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004541
Jerry Yu1a8b4812022-01-20 17:56:50 +08004542#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004543/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004544 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004545MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004546static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004547{
4548 size_t i, j;
4549 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004550
Gabor Mezei15b95a62022-05-09 16:37:58 +02004551 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004552 {
Jerry Yuf377d642022-01-25 10:43:59 +08004553 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004554 {
Jerry Yuf377d642022-01-25 10:43:59 +08004555 if( sig_algs[i] != sig_algs[j] )
4556 continue;
4557 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4558 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4559 sig_algs[i], j, i );
4560 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004561 }
4562 }
4563 return( ret );
4564}
4565
4566#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4567
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004568/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004569 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004570 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004571int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004572 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004573{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004574#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004576#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004577
Jerry Yu1a8b4812022-01-20 17:56:50 +08004578#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004579 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004580 {
4581 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4582 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4583 }
4584
Jerry Yuf377d642022-01-25 10:43:59 +08004585 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004586 {
4587 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4588 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4589 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004590
4591#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004592 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004593 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004594 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004595 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4596 }
4597
Jerry Yuf377d642022-01-25 10:43:59 +08004598 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004599 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004600 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004601 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4602 }
4603#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004604#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4605
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004606 /* Use the functions here so that they are covered in tests,
4607 * but otherwise access member directly for efficiency */
4608 mbedtls_ssl_conf_endpoint( conf, endpoint );
4609 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004610
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004611 /*
4612 * Things that are common to all presets
4613 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004614#if defined(MBEDTLS_SSL_CLI_C)
4615 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4616 {
4617 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4618#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4619 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4620#endif
4621 }
4622#endif
4623
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004624#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4625 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4626#endif
4627
4628#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4629 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4630#endif
4631
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004632#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004633 conf->f_cookie_write = ssl_cookie_write_dummy;
4634 conf->f_cookie_check = ssl_cookie_check_dummy;
4635#endif
4636
4637#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4638 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4639#endif
4640
Janos Follath088ce432017-04-10 12:42:31 +01004641#if defined(MBEDTLS_SSL_SRV_C)
4642 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004643 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004644#endif
4645
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004646#if defined(MBEDTLS_SSL_PROTO_DTLS)
4647 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4648 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4649#endif
4650
4651#if defined(MBEDTLS_SSL_RENEGOTIATION)
4652 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004653 memset( conf->renego_period, 0x00, 2 );
4654 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004655#endif
4656
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004657#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004658 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4659 {
4660 const unsigned char dhm_p[] =
4661 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4662 const unsigned char dhm_g[] =
4663 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004664
Hanno Beckere2defad2021-07-24 05:59:17 +01004665 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4666 dhm_p, sizeof( dhm_p ),
4667 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4668 {
4669 return( ret );
4670 }
4671 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004672#endif
4673
Ronald Cron6f135e12021-12-08 16:57:54 +01004674#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004675#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004676 mbedtls_ssl_conf_new_session_tickets(
4677 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4678#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004679 /*
4680 * Allow all TLS 1.3 key exchange modes by default.
4681 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004682 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004683#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004684
XiaokangQian4d3a6042022-04-21 13:46:17 +00004685 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4686 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004687#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004688 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004689 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004690#else
XiaokangQian060d8672022-04-21 09:24:56 +00004691 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004692#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004693 }
4694 else
4695 {
4696#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4697 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4698 {
4699 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4700 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4701 }
4702 else
4703 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4704 {
4705 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4706 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4707 }
4708#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4709 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4710 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4711#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4712 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4713 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4714#else
4715 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4716#endif
4717 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004718
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004719 /*
4720 * Preset-specific defaults
4721 */
4722 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004723 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004724 /*
4725 * NSA Suite B
4726 */
4727 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004729 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004730
4731#if defined(MBEDTLS_X509_CRT_PARSE_C)
4732 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004733#endif
4734
Gilles Peskineeccd8882020-03-10 12:19:08 +01004735#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004736#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4737 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4738 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4739 else
4740#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4741 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004742#endif
4743
Brett Warrene0edc842021-08-17 09:53:13 +01004744#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4745 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004746#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004747 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004748 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004749
4750 /*
4751 * Default
4752 */
4753 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004754
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004755 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004756
4757#if defined(MBEDTLS_X509_CRT_PARSE_C)
4758 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4759#endif
4760
Gilles Peskineeccd8882020-03-10 12:19:08 +01004761#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4763 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4764 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4765 else
4766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4767 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004768#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004769
Brett Warrene0edc842021-08-17 09:53:13 +01004770#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4771 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004772#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004773 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004774
4775#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4776 conf->dhm_min_bitlen = 1024;
4777#endif
4778 }
4779
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004780 return( 0 );
4781}
4782
4783/*
4784 * Free mbedtls_ssl_config
4785 */
4786void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4787{
4788#if defined(MBEDTLS_DHM_C)
4789 mbedtls_mpi_free( &conf->dhm_P );
4790 mbedtls_mpi_free( &conf->dhm_G );
4791#endif
4792
Gilles Peskineeccd8882020-03-10 12:19:08 +01004793#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004794#if defined(MBEDTLS_USE_PSA_CRYPTO)
4795 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4796 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004797 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4798 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004799#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004800 if( conf->psk != NULL )
4801 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004802 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004803 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004804 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004805 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004806 }
4807
4808 if( conf->psk_identity != NULL )
4809 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004810 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004811 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004812 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004813 conf->psk_identity_len = 0;
4814 }
4815#endif
4816
4817#if defined(MBEDTLS_X509_CRT_PARSE_C)
4818 ssl_key_cert_free( conf->key_cert );
4819#endif
4820
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004821 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004822}
4823
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004824#if defined(MBEDTLS_PK_C) && \
4825 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004826/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831#if defined(MBEDTLS_RSA_C)
4832 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4833 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004834#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835#if defined(MBEDTLS_ECDSA_C)
4836 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4837 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004840}
4841
Hanno Becker7e5437a2017-04-28 17:15:26 +01004842unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4843{
4844 switch( type ) {
4845 case MBEDTLS_PK_RSA:
4846 return( MBEDTLS_SSL_SIG_RSA );
4847 case MBEDTLS_PK_ECDSA:
4848 case MBEDTLS_PK_ECKEY:
4849 return( MBEDTLS_SSL_SIG_ECDSA );
4850 default:
4851 return( MBEDTLS_SSL_SIG_ANON );
4852 }
4853}
4854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004856{
4857 switch( sig )
4858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_RSA_C)
4860 case MBEDTLS_SSL_SIG_RSA:
4861 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_ECDSA_C)
4864 case MBEDTLS_SSL_SIG_ECDSA:
4865 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004866#endif
4867 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004869 }
4870}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004871#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004872
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004873/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004874 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004877{
4878 switch( hash )
4879 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004880#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 case MBEDTLS_SSL_HASH_MD5:
4882 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004883#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004884#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 case MBEDTLS_SSL_HASH_SHA1:
4886 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004887#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 case MBEDTLS_SSL_HASH_SHA224:
4890 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004891#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004892#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 case MBEDTLS_SSL_HASH_SHA256:
4894 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004895#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004896#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 case MBEDTLS_SSL_HASH_SHA384:
4898 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004899#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004900#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 case MBEDTLS_SSL_HASH_SHA512:
4902 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004903#endif
4904 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004906 }
4907}
4908
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004909/*
4910 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4911 */
4912unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4913{
4914 switch( md )
4915 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004916#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004917 case MBEDTLS_MD_MD5:
4918 return( MBEDTLS_SSL_HASH_MD5 );
4919#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004920#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004921 case MBEDTLS_MD_SHA1:
4922 return( MBEDTLS_SSL_HASH_SHA1 );
4923#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004924#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004925 case MBEDTLS_MD_SHA224:
4926 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004927#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004928#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004929 case MBEDTLS_MD_SHA256:
4930 return( MBEDTLS_SSL_HASH_SHA256 );
4931#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004932#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004933 case MBEDTLS_MD_SHA384:
4934 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004935#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004936#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004937 case MBEDTLS_MD_SHA512:
4938 return( MBEDTLS_SSL_HASH_SHA512 );
4939#endif
4940 default:
4941 return( MBEDTLS_SSL_HASH_NONE );
4942 }
4943}
4944
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004945/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004946 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004947 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004948 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004949int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004950{
Brett Warrene0edc842021-08-17 09:53:13 +01004951 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004952
Brett Warrene0edc842021-08-17 09:53:13 +01004953 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004954 return( -1 );
4955
Brett Warrene0edc842021-08-17 09:53:13 +01004956 for( ; *group_list != 0; group_list++ )
4957 {
4958 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004959 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004960 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004961
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004962 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004963}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004964
4965#if defined(MBEDTLS_ECP_C)
4966/*
4967 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4968 */
4969int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4970{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004971 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004972 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004973
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004974 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004975 return -1;
4976
4977 uint16_t tls_id = grp_info->tls_id;
4978
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004979 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4980}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004981#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if defined(MBEDTLS_X509_CRT_PARSE_C)
4984int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4985 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004986 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004987 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004988{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004989 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004991 const char *ext_oid;
4992 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004995 {
4996 /* Server part of the key exchange */
4997 switch( ciphersuite->key_exchange )
4998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 case MBEDTLS_KEY_EXCHANGE_RSA:
5000 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005001 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005002 break;
5003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5005 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5006 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5007 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005008 break;
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5011 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005012 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005013 break;
5014
5015 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 case MBEDTLS_KEY_EXCHANGE_NONE:
5017 case MBEDTLS_KEY_EXCHANGE_PSK:
5018 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5019 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005020 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005021 usage = 0;
5022 }
5023 }
5024 else
5025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5027 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005028 }
5029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005031 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005032 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005033 ret = -1;
5034 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5039 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005040 }
5041 else
5042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5044 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005045 }
5046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005048 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005049 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005050 ret = -1;
5051 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005052
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005053 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005056
Jerry Yu148165c2021-09-24 23:20:59 +08005057#if defined(MBEDTLS_USE_PSA_CRYPTO)
5058int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5059 const mbedtls_md_type_t md,
5060 unsigned char *dst,
5061 size_t dst_len,
5062 size_t *olen )
5063{
Ronald Cronf6893e12022-01-07 22:09:01 +01005064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5065 psa_hash_operation_t *hash_operation_to_clone;
5066 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5067
Jerry Yu148165c2021-09-24 23:20:59 +08005068 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005069
5070 switch( md )
5071 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005072#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005073 case MBEDTLS_MD_SHA384:
5074 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5075 break;
5076#endif
5077
Andrzej Kurek25f27152022-08-17 16:09:31 -04005078#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005079 case MBEDTLS_MD_SHA256:
5080 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5081 break;
5082#endif
5083
5084 default:
5085 goto exit;
5086 }
5087
5088 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5089 if( status != PSA_SUCCESS )
5090 goto exit;
5091
5092 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5093 if( status != PSA_SUCCESS )
5094 goto exit;
5095
5096exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005097 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005098}
5099#else /* MBEDTLS_USE_PSA_CRYPTO */
5100
Andrzej Kurek25f27152022-08-17 16:09:31 -04005101#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005102MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005103static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5104 unsigned char *dst,
5105 size_t dst_len,
5106 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005107{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005108 int ret;
5109 mbedtls_sha512_context sha512;
5110
5111 if( dst_len < 48 )
5112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5113
5114 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005115 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005116
5117 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5118 {
5119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5120 goto exit;
5121 }
5122
5123 *olen = 48;
5124
5125exit:
5126
5127 mbedtls_sha512_free( &sha512 );
5128 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005129}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005130#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005131
Andrzej Kurek25f27152022-08-17 16:09:31 -04005132#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005133MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005134static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5135 unsigned char *dst,
5136 size_t dst_len,
5137 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005138{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005139 int ret;
5140 mbedtls_sha256_context sha256;
5141
5142 if( dst_len < 32 )
5143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5144
5145 mbedtls_sha256_init( &sha256 );
5146 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005147
Jerry Yu24c0ec32021-09-09 14:21:07 +08005148 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5149 {
5150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5151 goto exit;
5152 }
5153
5154 *olen = 32;
5155
5156exit:
5157
5158 mbedtls_sha256_free( &sha256 );
5159 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005160}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005161#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005162
Jerry Yu000f9762021-09-14 11:12:51 +08005163int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5164 const mbedtls_md_type_t md,
5165 unsigned char *dst,
5166 size_t dst_len,
5167 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005168{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005169 switch( md )
5170 {
5171
Andrzej Kurek25f27152022-08-17 16:09:31 -04005172#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005173 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005174 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005175#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005176
Andrzej Kurek25f27152022-08-17 16:09:31 -04005177#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005178 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005179 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005180#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005181
5182 default:
5183 break;
5184 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005186}
XiaokangQian647719a2021-12-07 09:16:29 +00005187
Jerry Yu148165c2021-09-24 23:20:59 +08005188#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005189
Gabor Mezei078e8032022-04-27 21:17:56 +02005190#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5191/* mbedtls_ssl_parse_sig_alg_ext()
5192 *
5193 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5194 * value (TLS 1.3 RFC8446):
5195 * enum {
5196 * ....
5197 * ecdsa_secp256r1_sha256( 0x0403 ),
5198 * ecdsa_secp384r1_sha384( 0x0503 ),
5199 * ecdsa_secp521r1_sha512( 0x0603 ),
5200 * ....
5201 * } SignatureScheme;
5202 *
5203 * struct {
5204 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5205 * } SignatureSchemeList;
5206 *
5207 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5208 * value (TLS 1.2 RFC5246):
5209 * enum {
5210 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5211 * sha512(6), (255)
5212 * } HashAlgorithm;
5213 *
5214 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5215 * SignatureAlgorithm;
5216 *
5217 * struct {
5218 * HashAlgorithm hash;
5219 * SignatureAlgorithm signature;
5220 * } SignatureAndHashAlgorithm;
5221 *
5222 * SignatureAndHashAlgorithm
5223 * supported_signature_algorithms<2..2^16-2>;
5224 *
5225 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5226 * generalization of the TLS 1.2 signature algorithm extension.
5227 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5228 * `SignatureScheme` field of TLS 1.3
5229 *
5230 */
5231int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5232 const unsigned char *buf,
5233 const unsigned char *end )
5234{
5235 const unsigned char *p = buf;
5236 size_t supported_sig_algs_len = 0;
5237 const unsigned char *supported_sig_algs_end;
5238 uint16_t sig_alg;
5239 uint32_t common_idx = 0;
5240
5241 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5242 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5243 p += 2;
5244
5245 memset( ssl->handshake->received_sig_algs, 0,
5246 sizeof(ssl->handshake->received_sig_algs) );
5247
5248 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5249 supported_sig_algs_end = p + supported_sig_algs_len;
5250 while( p < supported_sig_algs_end )
5251 {
5252 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5253 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5254 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005255 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5256 sig_alg,
5257 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005259 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005260 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5261 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5262 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005263 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005264 }
5265#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005266
Jerry Yuf3b46b52022-06-19 16:52:27 +08005267 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5268 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005269
5270 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5271 {
5272 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5273 common_idx += 1;
5274 }
5275 }
5276 /* Check that we consumed all the message. */
5277 if( p != end )
5278 {
5279 MBEDTLS_SSL_DEBUG_MSG( 1,
5280 ( "Signature algorithms extension length misaligned" ) );
5281 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5282 MBEDTLS_ERR_SSL_DECODE_ERROR );
5283 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5284 }
5285
5286 if( common_idx == 0 )
5287 {
5288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5289 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5290 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5291 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5292 }
5293
Gabor Mezei15b95a62022-05-09 16:37:58 +02005294 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005295 return( 0 );
5296}
5297
5298#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5299
Jerry Yudc7bd172022-02-17 13:44:15 +08005300#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5301
5302#if defined(MBEDTLS_USE_PSA_CRYPTO)
5303
5304static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5305 mbedtls_svc_key_id_t key,
5306 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005307 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005308 const unsigned char* seed, size_t seed_length,
5309 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005310 const unsigned char* other_secret,
5311 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005312 size_t capacity )
5313{
5314 psa_status_t status;
5315
5316 status = psa_key_derivation_setup( derivation, alg );
5317 if( status != PSA_SUCCESS )
5318 return( status );
5319
5320 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5321 {
5322 status = psa_key_derivation_input_bytes( derivation,
5323 PSA_KEY_DERIVATION_INPUT_SEED,
5324 seed, seed_length );
5325 if( status != PSA_SUCCESS )
5326 return( status );
5327
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005328 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005329 {
5330 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005331 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5332 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005333 if( status != PSA_SUCCESS )
5334 return( status );
5335 }
5336
Jerry Yudc7bd172022-02-17 13:44:15 +08005337 if( mbedtls_svc_key_id_is_null( key ) )
5338 {
5339 status = psa_key_derivation_input_bytes(
5340 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005341 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005342 }
5343 else
5344 {
5345 status = psa_key_derivation_input_key(
5346 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5347 }
5348 if( status != PSA_SUCCESS )
5349 return( status );
5350
5351 status = psa_key_derivation_input_bytes( derivation,
5352 PSA_KEY_DERIVATION_INPUT_LABEL,
5353 label, label_length );
5354 if( status != PSA_SUCCESS )
5355 return( status );
5356 }
5357 else
5358 {
5359 return( PSA_ERROR_NOT_SUPPORTED );
5360 }
5361
5362 status = psa_key_derivation_set_capacity( derivation, capacity );
5363 if( status != PSA_SUCCESS )
5364 return( status );
5365
5366 return( PSA_SUCCESS );
5367}
5368
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005369MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005370static int tls_prf_generic( mbedtls_md_type_t md_type,
5371 const unsigned char *secret, size_t slen,
5372 const char *label,
5373 const unsigned char *random, size_t rlen,
5374 unsigned char *dstbuf, size_t dlen )
5375{
5376 psa_status_t status;
5377 psa_algorithm_t alg;
5378 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5379 psa_key_derivation_operation_t derivation =
5380 PSA_KEY_DERIVATION_OPERATION_INIT;
5381
5382 if( md_type == MBEDTLS_MD_SHA384 )
5383 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5384 else
5385 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5386
5387 /* Normally a "secret" should be long enough to be impossible to
5388 * find by brute force, and in particular should not be empty. But
5389 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5390 * and for this use case it makes sense to have a 0-length "secret".
5391 * Since the key API doesn't allow importing a key of length 0,
5392 * keep master_key=0, which setup_psa_key_derivation() understands
5393 * to mean a 0-length "secret" input. */
5394 if( slen != 0 )
5395 {
5396 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5397 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5398 psa_set_key_algorithm( &key_attributes, alg );
5399 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5400
5401 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5402 if( status != PSA_SUCCESS )
5403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5404 }
5405
5406 status = setup_psa_key_derivation( &derivation,
5407 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005408 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005409 random, rlen,
5410 (unsigned char const *) label,
5411 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005412 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005413 dlen );
5414 if( status != PSA_SUCCESS )
5415 {
5416 psa_key_derivation_abort( &derivation );
5417 psa_destroy_key( master_key );
5418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5419 }
5420
5421 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5422 if( status != PSA_SUCCESS )
5423 {
5424 psa_key_derivation_abort( &derivation );
5425 psa_destroy_key( master_key );
5426 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5427 }
5428
5429 status = psa_key_derivation_abort( &derivation );
5430 if( status != PSA_SUCCESS )
5431 {
5432 psa_destroy_key( master_key );
5433 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5434 }
5435
5436 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5437 status = psa_destroy_key( master_key );
5438 if( status != PSA_SUCCESS )
5439 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5440
5441 return( 0 );
5442}
5443
5444#else /* MBEDTLS_USE_PSA_CRYPTO */
5445
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005446MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005447static int tls_prf_generic( mbedtls_md_type_t md_type,
5448 const unsigned char *secret, size_t slen,
5449 const char *label,
5450 const unsigned char *random, size_t rlen,
5451 unsigned char *dstbuf, size_t dlen )
5452{
5453 size_t nb;
5454 size_t i, j, k, md_len;
5455 unsigned char *tmp;
5456 size_t tmp_len = 0;
5457 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5458 const mbedtls_md_info_t *md_info;
5459 mbedtls_md_context_t md_ctx;
5460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5461
5462 mbedtls_md_init( &md_ctx );
5463
5464 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5465 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5466
5467 md_len = mbedtls_md_get_size( md_info );
5468
5469 tmp_len = md_len + strlen( label ) + rlen;
5470 tmp = mbedtls_calloc( 1, tmp_len );
5471 if( tmp == NULL )
5472 {
5473 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5474 goto exit;
5475 }
5476
5477 nb = strlen( label );
5478 memcpy( tmp + md_len, label, nb );
5479 memcpy( tmp + md_len + nb, random, rlen );
5480 nb += rlen;
5481
5482 /*
5483 * Compute P_<hash>(secret, label + random)[0..dlen]
5484 */
5485 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5486 goto exit;
5487
5488 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5489 if( ret != 0 )
5490 goto exit;
5491 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5492 if( ret != 0 )
5493 goto exit;
5494 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5495 if( ret != 0 )
5496 goto exit;
5497
5498 for( i = 0; i < dlen; i += md_len )
5499 {
5500 ret = mbedtls_md_hmac_reset ( &md_ctx );
5501 if( ret != 0 )
5502 goto exit;
5503 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5504 if( ret != 0 )
5505 goto exit;
5506 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5507 if( ret != 0 )
5508 goto exit;
5509
5510 ret = mbedtls_md_hmac_reset ( &md_ctx );
5511 if( ret != 0 )
5512 goto exit;
5513 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5514 if( ret != 0 )
5515 goto exit;
5516 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5517 if( ret != 0 )
5518 goto exit;
5519
5520 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5521
5522 for( j = 0; j < k; j++ )
5523 dstbuf[i + j] = h_i[j];
5524 }
5525
5526exit:
5527 mbedtls_md_free( &md_ctx );
5528
5529 mbedtls_platform_zeroize( tmp, tmp_len );
5530 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5531
5532 mbedtls_free( tmp );
5533
5534 return( ret );
5535}
5536#endif /* MBEDTLS_USE_PSA_CRYPTO */
5537
Andrzej Kurek25f27152022-08-17 16:09:31 -04005538#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005539MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005540static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5541 const char *label,
5542 const unsigned char *random, size_t rlen,
5543 unsigned char *dstbuf, size_t dlen )
5544{
5545 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5546 label, random, rlen, dstbuf, dlen ) );
5547}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005548#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005549
Andrzej Kurek25f27152022-08-17 16:09:31 -04005550#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005551MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005552static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5553 const char *label,
5554 const unsigned char *random, size_t rlen,
5555 unsigned char *dstbuf, size_t dlen )
5556{
5557 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5558 label, random, rlen, dstbuf, dlen ) );
5559}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005560#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005561
Jerry Yuf009d862022-02-17 14:01:37 +08005562/*
5563 * Set appropriate PRF function and other SSL / TLS1.2 functions
5564 *
5565 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005566 * - hash associated with the ciphersuite (only used by TLS 1.2)
5567 *
5568 * Outputs:
5569 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5570 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005571MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005572static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005573 mbedtls_md_type_t hash )
5574{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005575#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005576 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005577 {
5578 handshake->tls_prf = tls_prf_sha384;
5579 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5580 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5581 }
5582 else
5583#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005584#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005585 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005586 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005587 handshake->tls_prf = tls_prf_sha256;
5588 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5589 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5590 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005591#else
Jerry Yuf009d862022-02-17 14:01:37 +08005592 {
Jerry Yuf009d862022-02-17 14:01:37 +08005593 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005594 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5596 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005597#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005598
5599 return( 0 );
5600}
Jerry Yud6ab2352022-02-17 14:03:43 +08005601
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005602/*
5603 * Compute master secret if needed
5604 *
5605 * Parameters:
5606 * [in/out] handshake
5607 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5608 * (PSA-PSK) ciphersuite_info, psk_opaque
5609 * [out] premaster (cleared)
5610 * [out] master
5611 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5612 * debug: conf->f_dbg, conf->p_dbg
5613 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005614 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005615 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005616MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005617static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5618 unsigned char *master,
5619 const mbedtls_ssl_context *ssl )
5620{
5621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5622
5623 /* cf. RFC 5246, Section 8.1:
5624 * "The master secret is always exactly 48 bytes in length." */
5625 size_t const master_secret_len = 48;
5626
5627#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5628 unsigned char session_hash[48];
5629#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5630
5631 /* The label for the KDF used for key expansion.
5632 * This is either "master secret" or "extended master secret"
5633 * depending on whether the Extended Master Secret extension
5634 * is used. */
5635 char const *lbl = "master secret";
5636
Przemek Stekielae4ed302022-04-05 17:15:55 +02005637 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005638 * - If the Extended Master Secret extension is not used,
5639 * this is ClientHello.Random + ServerHello.Random
5640 * (see Sect. 8.1 in RFC 5246).
5641 * - If the Extended Master Secret extension is used,
5642 * this is the transcript of the handshake so far.
5643 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005644 unsigned char const *seed = handshake->randbytes;
5645 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005646
5647#if !defined(MBEDTLS_DEBUG_C) && \
5648 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5649 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5650 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5651 ssl = NULL; /* make sure we don't use it except for those cases */
5652 (void) ssl;
5653#endif
5654
5655 if( handshake->resume != 0 )
5656 {
5657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5658 return( 0 );
5659 }
5660
5661#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5662 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5663 {
5664 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005665 seed = session_hash;
5666 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005667
5668 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005669 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005670 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005671#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005672
Przemek Stekiel99114f32022-04-22 11:20:09 +02005673#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005674 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005675 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005676 {
5677 /* Perform PSK-to-MS expansion in a single step. */
5678 psa_status_t status;
5679 psa_algorithm_t alg;
5680 mbedtls_svc_key_id_t psk;
5681 psa_key_derivation_operation_t derivation =
5682 PSA_KEY_DERIVATION_OPERATION_INIT;
5683 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5684
5685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5686
5687 psk = mbedtls_ssl_get_opaque_psk( ssl );
5688
5689 if( hash_alg == MBEDTLS_MD_SHA384 )
5690 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5691 else
5692 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5693
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005694 size_t other_secret_len = 0;
5695 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005696
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005697 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005698 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005699 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005700 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005701 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005702 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005703 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005704 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005705 other_secret_len = 48;
5706 other_secret = handshake->premaster + 2;
5707 break;
5708 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005709 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005710 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5711 other_secret = handshake->premaster + 2;
5712 break;
5713 default:
5714 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005715 }
5716
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005717 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005718 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005719 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005720 (unsigned char const *) lbl,
5721 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005722 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005723 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_output_bytes( &derivation,
5731 master,
5732 master_secret_len );
5733 if( status != PSA_SUCCESS )
5734 {
5735 psa_key_derivation_abort( &derivation );
5736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5737 }
5738
5739 status = psa_key_derivation_abort( &derivation );
5740 if( status != PSA_SUCCESS )
5741 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5742 }
5743 else
5744#endif
5745 {
5746 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005747 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005748 master,
5749 master_secret_len );
5750 if( ret != 0 )
5751 {
5752 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5753 return( ret );
5754 }
5755
5756 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5757 handshake->premaster,
5758 handshake->pmslen );
5759
5760 mbedtls_platform_zeroize( handshake->premaster,
5761 sizeof(handshake->premaster) );
5762 }
5763
5764 return( 0 );
5765}
5766
Jerry Yud62f87e2022-02-17 14:09:02 +08005767int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5768{
5769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5770 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5771 ssl->handshake->ciphersuite_info;
5772
5773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5774
5775 /* Set PRF, calc_verify and calc_finished function pointers */
5776 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005777 ciphersuite_info->mac );
5778 if( ret != 0 )
5779 {
5780 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5781 return( ret );
5782 }
5783
5784 /* Compute master secret if needed */
5785 ret = ssl_compute_master( ssl->handshake,
5786 ssl->session_negotiate->master,
5787 ssl );
5788 if( ret != 0 )
5789 {
5790 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5791 return( ret );
5792 }
5793
5794 /* Swap the client and server random values:
5795 * - MS derivation wanted client+server (RFC 5246 8.1)
5796 * - key derivation wants server+client (RFC 5246 6.3) */
5797 {
5798 unsigned char tmp[64];
5799 memcpy( tmp, ssl->handshake->randbytes, 64 );
5800 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5801 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5802 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5803 }
5804
5805 /* Populate transform structure */
5806 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5807 ssl->session_negotiate->ciphersuite,
5808 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005809#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005810 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005811#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005812 ssl->handshake->tls_prf,
5813 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005814 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005815 ssl->conf->endpoint,
5816 ssl );
5817 if( ret != 0 )
5818 {
5819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5820 return( ret );
5821 }
5822
5823 /* We no longer need Server/ClientHello.random values */
5824 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5825 sizeof( ssl->handshake->randbytes ) );
5826
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5828
5829 return( 0 );
5830}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005831
Ronald Cron4dcbca92022-03-07 10:21:40 +01005832int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5833{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005834 switch( md )
5835 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005836#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005837 case MBEDTLS_SSL_HASH_SHA384:
5838 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5839 break;
5840#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005841#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005842 case MBEDTLS_SSL_HASH_SHA256:
5843 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5844 break;
5845#endif
5846 default:
5847 return( -1 );
5848 }
5849
Ronald Cronc2f13a02022-03-07 10:25:24 +01005850 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005851}
5852
Andrzej Kurek25f27152022-08-17 16:09:31 -04005853#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005854void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5855 unsigned char *hash,
5856 size_t *hlen )
5857{
5858#if defined(MBEDTLS_USE_PSA_CRYPTO)
5859 size_t hash_size;
5860 psa_status_t status;
5861 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5862
5863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5864 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5865 if( status != PSA_SUCCESS )
5866 {
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5868 return;
5869 }
5870
5871 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5872 if( status != PSA_SUCCESS )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5875 return;
5876 }
5877
5878 *hlen = 32;
5879 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5881#else
5882 mbedtls_sha256_context sha256;
5883
5884 mbedtls_sha256_init( &sha256 );
5885
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5887
5888 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5889 mbedtls_sha256_finish( &sha256, hash );
5890
5891 *hlen = 32;
5892
5893 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5895
5896 mbedtls_sha256_free( &sha256 );
5897#endif /* MBEDTLS_USE_PSA_CRYPTO */
5898 return;
5899}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005900#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005901
Andrzej Kurek25f27152022-08-17 16:09:31 -04005902#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005903void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5904 unsigned char *hash,
5905 size_t *hlen )
5906{
5907#if defined(MBEDTLS_USE_PSA_CRYPTO)
5908 size_t hash_size;
5909 psa_status_t status;
5910 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5911
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5913 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5914 if( status != PSA_SUCCESS )
5915 {
5916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5917 return;
5918 }
5919
5920 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5921 if( status != PSA_SUCCESS )
5922 {
5923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5924 return;
5925 }
5926
5927 *hlen = 48;
5928 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5930#else
5931 mbedtls_sha512_context sha512;
5932
5933 mbedtls_sha512_init( &sha512 );
5934
5935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5936
Andrzej Kureka242e832022-08-11 10:03:14 -04005937 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005938 mbedtls_sha512_finish( &sha512, hash );
5939
5940 *hlen = 48;
5941
5942 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5944
5945 mbedtls_sha512_free( &sha512 );
5946#endif /* MBEDTLS_USE_PSA_CRYPTO */
5947 return;
5948}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005949#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005950
Neil Armstrong80f6f322022-05-03 17:56:38 +02005951#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5952 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005953int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5954{
5955 unsigned char *p = ssl->handshake->premaster;
5956 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5957 const unsigned char *psk = NULL;
5958 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005959 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005960
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005961 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005962 {
5963 /*
5964 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005965 * checked before calling this function.
5966 *
5967 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005968 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005969 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005970 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5971 {
5972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5974 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005975 }
5976
5977 /*
5978 * PMS = struct {
5979 * opaque other_secret<0..2^16-1>;
5980 * opaque psk<0..2^16-1>;
5981 * };
5982 * with "other_secret" depending on the particular key exchange
5983 */
5984#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5985 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5986 {
5987 if( end - p < 2 )
5988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5989
5990 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5991 p += 2;
5992
5993 if( end < p || (size_t)( end - p ) < psk_len )
5994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5995
5996 memset( p, 0, psk_len );
5997 p += psk_len;
5998 }
5999 else
6000#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6001#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6002 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6003 {
6004 /*
6005 * other_secret already set by the ClientKeyExchange message,
6006 * and is 48 bytes long
6007 */
6008 if( end - p < 2 )
6009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6010
6011 *p++ = 0;
6012 *p++ = 48;
6013 p += 48;
6014 }
6015 else
6016#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6017#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6018 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6019 {
6020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6021 size_t len;
6022
6023 /* Write length only when we know the actual value */
6024 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6025 p + 2, end - ( p + 2 ), &len,
6026 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6027 {
6028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6029 return( ret );
6030 }
6031 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6032 p += 2 + len;
6033
6034 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6035 }
6036 else
6037#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006038#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006039 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6040 {
6041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6042 size_t zlen;
6043
6044 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6045 p + 2, end - ( p + 2 ),
6046 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6047 {
6048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6049 return( ret );
6050 }
6051
6052 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6053 p += 2 + zlen;
6054
6055 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6056 MBEDTLS_DEBUG_ECDH_Z );
6057 }
6058 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006059#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006060 {
6061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6063 }
6064
6065 /* opaque psk<0..2^16-1>; */
6066 if( end - p < 2 )
6067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6068
6069 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6070 p += 2;
6071
6072 if( end < p || (size_t)( end - p ) < psk_len )
6073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6074
6075 memcpy( p, psk, psk_len );
6076 p += psk_len;
6077
6078 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6079
6080 return( 0 );
6081}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006082#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006083
6084#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006085MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006086static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6087
6088#if defined(MBEDTLS_SSL_PROTO_DTLS)
6089int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6090{
6091 /* If renegotiation is not enforced, retransmit until we would reach max
6092 * timeout if we were using the usual handshake doubling scheme */
6093 if( ssl->conf->renego_max_records < 0 )
6094 {
6095 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6096 unsigned char doublings = 1;
6097
6098 while( ratio != 0 )
6099 {
6100 ++doublings;
6101 ratio >>= 1;
6102 }
6103
6104 if( ++ssl->renego_records_seen > doublings )
6105 {
6106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6107 return( 0 );
6108 }
6109 }
6110
6111 return( ssl_write_hello_request( ssl ) );
6112}
6113#endif
6114#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006115
Jerry Yud9526692022-02-17 14:23:47 +08006116/*
6117 * Handshake functions
6118 */
6119#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6120/* No certificate support -> dummy functions */
6121int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6122{
6123 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6124 ssl->handshake->ciphersuite_info;
6125
6126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6127
6128 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6129 {
6130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6131 ssl->state++;
6132 return( 0 );
6133 }
6134
6135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6137}
6138
6139int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6140{
6141 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6142 ssl->handshake->ciphersuite_info;
6143
6144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6145
6146 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6147 {
6148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6149 ssl->state++;
6150 return( 0 );
6151 }
6152
6153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6155}
6156
6157#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6158/* Some certificate support -> implement write and parse */
6159
6160int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6161{
6162 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6163 size_t i, n;
6164 const mbedtls_x509_crt *crt;
6165 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6166 ssl->handshake->ciphersuite_info;
6167
6168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6169
6170 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6171 {
6172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6173 ssl->state++;
6174 return( 0 );
6175 }
6176
6177#if defined(MBEDTLS_SSL_CLI_C)
6178 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6179 {
6180 if( ssl->handshake->client_auth == 0 )
6181 {
6182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6183 ssl->state++;
6184 return( 0 );
6185 }
6186 }
6187#endif /* MBEDTLS_SSL_CLI_C */
6188#if defined(MBEDTLS_SSL_SRV_C)
6189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6190 {
6191 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6192 {
6193 /* Should never happen because we shouldn't have picked the
6194 * ciphersuite if we don't have a certificate. */
6195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6196 }
6197 }
6198#endif
6199
6200 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6201
6202 /*
6203 * 0 . 0 handshake type
6204 * 1 . 3 handshake length
6205 * 4 . 6 length of all certs
6206 * 7 . 9 length of cert. 1
6207 * 10 . n-1 peer certificate
6208 * n . n+2 length of cert. 2
6209 * n+3 . ... upper level cert, etc.
6210 */
6211 i = 7;
6212 crt = mbedtls_ssl_own_cert( ssl );
6213
6214 while( crt != NULL )
6215 {
6216 n = crt->raw.len;
6217 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6218 {
6219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6220 " > %" MBEDTLS_PRINTF_SIZET,
6221 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6222 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6223 }
6224
6225 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6226 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6227 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6228
6229 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6230 i += n; crt = crt->next;
6231 }
6232
6233 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6234 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6235 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6236
6237 ssl->out_msglen = i;
6238 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6239 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6240
6241 ssl->state++;
6242
6243 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6244 {
6245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6246 return( ret );
6247 }
6248
6249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6250
6251 return( ret );
6252}
6253
6254#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6255
6256#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006257MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006258static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6259 unsigned char *crt_buf,
6260 size_t crt_buf_len )
6261{
6262 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6263
6264 if( peer_crt == NULL )
6265 return( -1 );
6266
6267 if( peer_crt->raw.len != crt_buf_len )
6268 return( -1 );
6269
6270 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6271}
6272#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006274static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6275 unsigned char *crt_buf,
6276 size_t crt_buf_len )
6277{
6278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6279 unsigned char const * const peer_cert_digest =
6280 ssl->session->peer_cert_digest;
6281 mbedtls_md_type_t const peer_cert_digest_type =
6282 ssl->session->peer_cert_digest_type;
6283 mbedtls_md_info_t const * const digest_info =
6284 mbedtls_md_info_from_type( peer_cert_digest_type );
6285 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6286 size_t digest_len;
6287
6288 if( peer_cert_digest == NULL || digest_info == NULL )
6289 return( -1 );
6290
6291 digest_len = mbedtls_md_get_size( digest_info );
6292 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6293 return( -1 );
6294
6295 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6296 if( ret != 0 )
6297 return( -1 );
6298
6299 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6300}
6301#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6302#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6303
6304/*
6305 * Once the certificate message is read, parse it into a cert chain and
6306 * perform basic checks, but leave actual verification to the caller
6307 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006308MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006309static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6310 mbedtls_x509_crt *chain )
6311{
6312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6313#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6314 int crt_cnt=0;
6315#endif
6316 size_t i, n;
6317 uint8_t alert;
6318
6319 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6320 {
6321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6322 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6323 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6324 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6325 }
6326
6327 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6328 {
6329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6330 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6331 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6332 }
6333
6334 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6335 {
6336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6339 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6340 }
6341
6342 i = mbedtls_ssl_hs_hdr_len( ssl );
6343
6344 /*
6345 * Same message structure as in mbedtls_ssl_write_certificate()
6346 */
6347 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6348
6349 if( ssl->in_msg[i] != 0 ||
6350 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6354 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6355 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6356 }
6357
6358 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6359 i += 3;
6360
6361 /* Iterate through and parse the CRTs in the provided chain. */
6362 while( i < ssl->in_hslen )
6363 {
6364 /* Check that there's room for the next CRT's length fields. */
6365 if ( i + 3 > ssl->in_hslen ) {
6366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6367 mbedtls_ssl_send_alert_message( ssl,
6368 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6369 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6370 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6371 }
6372 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6373 * anything beyond 2**16 ~ 64K. */
6374 if( ssl->in_msg[i] != 0 )
6375 {
6376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6377 mbedtls_ssl_send_alert_message( ssl,
6378 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6379 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6380 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6381 }
6382
6383 /* Read length of the next CRT in the chain. */
6384 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6385 | (unsigned int) ssl->in_msg[i + 2];
6386 i += 3;
6387
6388 if( n < 128 || i + n > ssl->in_hslen )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6391 mbedtls_ssl_send_alert_message( ssl,
6392 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6393 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6394 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6395 }
6396
6397 /* Check if we're handling the first CRT in the chain. */
6398#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6399 if( crt_cnt++ == 0 &&
6400 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6401 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6402 {
6403 /* During client-side renegotiation, check that the server's
6404 * end-CRTs hasn't changed compared to the initial handshake,
6405 * mitigating the triple handshake attack. On success, reuse
6406 * the original end-CRT instead of parsing it again. */
6407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6408 if( ssl_check_peer_crt_unchanged( ssl,
6409 &ssl->in_msg[i],
6410 n ) != 0 )
6411 {
6412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6413 mbedtls_ssl_send_alert_message( ssl,
6414 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6415 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6416 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6417 }
6418
6419 /* Now we can safely free the original chain. */
6420 ssl_clear_peer_cert( ssl->session );
6421 }
6422#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6423
6424 /* Parse the next certificate in the chain. */
6425#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6426 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6427#else
6428 /* If we don't need to store the CRT chain permanently, parse
6429 * it in-place from the input buffer instead of making a copy. */
6430 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6431#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6432 switch( ret )
6433 {
6434 case 0: /*ok*/
6435 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6436 /* Ignore certificate with an unknown algorithm: maybe a
6437 prior certificate was already trusted. */
6438 break;
6439
6440 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6441 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6442 goto crt_parse_der_failed;
6443
6444 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6445 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6446 goto crt_parse_der_failed;
6447
6448 default:
6449 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6450 crt_parse_der_failed:
6451 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6452 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6453 return( ret );
6454 }
6455
6456 i += n;
6457 }
6458
6459 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6460 return( 0 );
6461}
6462
6463#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006464MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006465static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6466{
6467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6468 return( -1 );
6469
6470 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6471 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6472 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6473 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6474 {
Ronald Cron19385882022-06-15 16:26:13 +02006475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006476 return( 0 );
6477 }
6478 return( -1 );
6479}
6480#endif /* MBEDTLS_SSL_SRV_C */
6481
6482/* Check if a certificate message is expected.
6483 * Return either
6484 * - SSL_CERTIFICATE_EXPECTED, or
6485 * - SSL_CERTIFICATE_SKIP
6486 * indicating whether a Certificate message is expected or not.
6487 */
6488#define SSL_CERTIFICATE_EXPECTED 0
6489#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006490MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006491static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6492 int authmode )
6493{
6494 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6495 ssl->handshake->ciphersuite_info;
6496
6497 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6498 return( SSL_CERTIFICATE_SKIP );
6499
6500#if defined(MBEDTLS_SSL_SRV_C)
6501 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6502 {
6503 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6504 return( SSL_CERTIFICATE_SKIP );
6505
6506 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6507 {
6508 ssl->session_negotiate->verify_result =
6509 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6510 return( SSL_CERTIFICATE_SKIP );
6511 }
6512 }
6513#else
6514 ((void) authmode);
6515#endif /* MBEDTLS_SSL_SRV_C */
6516
6517 return( SSL_CERTIFICATE_EXPECTED );
6518}
6519
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006520MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006521static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6522 int authmode,
6523 mbedtls_x509_crt *chain,
6524 void *rs_ctx )
6525{
6526 int ret = 0;
6527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6528 ssl->handshake->ciphersuite_info;
6529 int have_ca_chain = 0;
6530
6531 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6532 void *p_vrfy;
6533
6534 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6535 return( 0 );
6536
6537 if( ssl->f_vrfy != NULL )
6538 {
6539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6540 f_vrfy = ssl->f_vrfy;
6541 p_vrfy = ssl->p_vrfy;
6542 }
6543 else
6544 {
6545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6546 f_vrfy = ssl->conf->f_vrfy;
6547 p_vrfy = ssl->conf->p_vrfy;
6548 }
6549
6550 /*
6551 * Main check: verify certificate
6552 */
6553#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6554 if( ssl->conf->f_ca_cb != NULL )
6555 {
6556 ((void) rs_ctx);
6557 have_ca_chain = 1;
6558
6559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6560 ret = mbedtls_x509_crt_verify_with_ca_cb(
6561 chain,
6562 ssl->conf->f_ca_cb,
6563 ssl->conf->p_ca_cb,
6564 ssl->conf->cert_profile,
6565 ssl->hostname,
6566 &ssl->session_negotiate->verify_result,
6567 f_vrfy, p_vrfy );
6568 }
6569 else
6570#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6571 {
6572 mbedtls_x509_crt *ca_chain;
6573 mbedtls_x509_crl *ca_crl;
6574
6575#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6576 if( ssl->handshake->sni_ca_chain != NULL )
6577 {
6578 ca_chain = ssl->handshake->sni_ca_chain;
6579 ca_crl = ssl->handshake->sni_ca_crl;
6580 }
6581 else
6582#endif
6583 {
6584 ca_chain = ssl->conf->ca_chain;
6585 ca_crl = ssl->conf->ca_crl;
6586 }
6587
6588 if( ca_chain != NULL )
6589 have_ca_chain = 1;
6590
6591 ret = mbedtls_x509_crt_verify_restartable(
6592 chain,
6593 ca_chain, ca_crl,
6594 ssl->conf->cert_profile,
6595 ssl->hostname,
6596 &ssl->session_negotiate->verify_result,
6597 f_vrfy, p_vrfy, rs_ctx );
6598 }
6599
6600 if( ret != 0 )
6601 {
6602 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6603 }
6604
6605#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6606 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6607 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6608#endif
6609
6610 /*
6611 * Secondary checks: always done, but change 'ret' only if it was 0
6612 */
6613
6614#if defined(MBEDTLS_ECP_C)
6615 {
6616 const mbedtls_pk_context *pk = &chain->pk;
6617
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006618 /* If certificate uses an EC key, make sure the curve is OK.
6619 * This is a public key, so it can't be opaque, so can_do() is a good
6620 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006621 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006622 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006623 /* and in the unlikely case the above assumption no longer holds
6624 * we are making sure that pk_ec() here does not return a NULL
6625 */
6626 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6627 if( ec == NULL )
6628 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6631 }
Jerry Yud9526692022-02-17 14:23:47 +08006632
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006633 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6634 {
6635 ssl->session_negotiate->verify_result |=
6636 MBEDTLS_X509_BADCERT_BAD_KEY;
6637
6638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6639 if( ret == 0 )
6640 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6641 }
Jerry Yud9526692022-02-17 14:23:47 +08006642 }
6643 }
6644#endif /* MBEDTLS_ECP_C */
6645
6646 if( mbedtls_ssl_check_cert_usage( chain,
6647 ciphersuite_info,
6648 ! ssl->conf->endpoint,
6649 &ssl->session_negotiate->verify_result ) != 0 )
6650 {
6651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6652 if( ret == 0 )
6653 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6654 }
6655
6656 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6657 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6658 * with details encoded in the verification flags. All other kinds
6659 * of error codes, including those from the user provided f_vrfy
6660 * functions, are treated as fatal and lead to a failure of
6661 * ssl_parse_certificate even if verification was optional. */
6662 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6663 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6664 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6665 {
6666 ret = 0;
6667 }
6668
6669 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6670 {
6671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6672 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6673 }
6674
6675 if( ret != 0 )
6676 {
6677 uint8_t alert;
6678
6679 /* The certificate may have been rejected for several reasons.
6680 Pick one and send the corresponding alert. Which alert to send
6681 may be a subject of debate in some cases. */
6682 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6683 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6684 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6685 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6686 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6687 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6688 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6689 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6690 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6691 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6692 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6693 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6694 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6695 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6696 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6697 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6698 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6699 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6700 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6701 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6702 else
6703 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6704 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6705 alert );
6706 }
6707
6708#if defined(MBEDTLS_DEBUG_C)
6709 if( ssl->session_negotiate->verify_result != 0 )
6710 {
6711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6712 (unsigned int) ssl->session_negotiate->verify_result ) );
6713 }
6714 else
6715 {
6716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6717 }
6718#endif /* MBEDTLS_DEBUG_C */
6719
6720 return( ret );
6721}
6722
6723#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006724MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006725static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6726 unsigned char *start, size_t len )
6727{
6728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6729 /* Remember digest of the peer's end-CRT. */
6730 ssl->session_negotiate->peer_cert_digest =
6731 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6732 if( ssl->session_negotiate->peer_cert_digest == NULL )
6733 {
6734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6735 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6736 mbedtls_ssl_send_alert_message( ssl,
6737 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6738 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6739
6740 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6741 }
6742
6743 ret = mbedtls_md( mbedtls_md_info_from_type(
6744 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6745 start, len,
6746 ssl->session_negotiate->peer_cert_digest );
6747
6748 ssl->session_negotiate->peer_cert_digest_type =
6749 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6750 ssl->session_negotiate->peer_cert_digest_len =
6751 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6752
6753 return( ret );
6754}
6755
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006756MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006757static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6758 unsigned char *start, size_t len )
6759{
6760 unsigned char *end = start + len;
6761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6762
6763 /* Make a copy of the peer's raw public key. */
6764 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6765 ret = mbedtls_pk_parse_subpubkey( &start, end,
6766 &ssl->handshake->peer_pubkey );
6767 if( ret != 0 )
6768 {
6769 /* We should have parsed the public key before. */
6770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6771 }
6772
6773 return( 0 );
6774}
6775#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6776
6777int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6778{
6779 int ret = 0;
6780 int crt_expected;
6781#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6782 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6783 ? ssl->handshake->sni_authmode
6784 : ssl->conf->authmode;
6785#else
6786 const int authmode = ssl->conf->authmode;
6787#endif
6788 void *rs_ctx = NULL;
6789 mbedtls_x509_crt *chain = NULL;
6790
6791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6792
6793 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6794 if( crt_expected == SSL_CERTIFICATE_SKIP )
6795 {
6796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6797 goto exit;
6798 }
6799
6800#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6801 if( ssl->handshake->ecrs_enabled &&
6802 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6803 {
6804 chain = ssl->handshake->ecrs_peer_cert;
6805 ssl->handshake->ecrs_peer_cert = NULL;
6806 goto crt_verify;
6807 }
6808#endif
6809
6810 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6811 {
6812 /* mbedtls_ssl_read_record may have sent an alert already. We
6813 let it decide whether to alert. */
6814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6815 goto exit;
6816 }
6817
6818#if defined(MBEDTLS_SSL_SRV_C)
6819 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6820 {
6821 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6822
6823 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6824 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6825
6826 goto exit;
6827 }
6828#endif /* MBEDTLS_SSL_SRV_C */
6829
6830 /* Clear existing peer CRT structure in case we tried to
6831 * reuse a session but it failed, and allocate a new one. */
6832 ssl_clear_peer_cert( ssl->session_negotiate );
6833
6834 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6835 if( chain == NULL )
6836 {
6837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6838 sizeof( mbedtls_x509_crt ) ) );
6839 mbedtls_ssl_send_alert_message( ssl,
6840 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6841 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6842
6843 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6844 goto exit;
6845 }
6846 mbedtls_x509_crt_init( chain );
6847
6848 ret = ssl_parse_certificate_chain( ssl, chain );
6849 if( ret != 0 )
6850 goto exit;
6851
6852#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6853 if( ssl->handshake->ecrs_enabled)
6854 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6855
6856crt_verify:
6857 if( ssl->handshake->ecrs_enabled)
6858 rs_ctx = &ssl->handshake->ecrs_ctx;
6859#endif
6860
6861 ret = ssl_parse_certificate_verify( ssl, authmode,
6862 chain, rs_ctx );
6863 if( ret != 0 )
6864 goto exit;
6865
6866#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6867 {
6868 unsigned char *crt_start, *pk_start;
6869 size_t crt_len, pk_len;
6870
6871 /* We parse the CRT chain without copying, so
6872 * these pointers point into the input buffer,
6873 * and are hence still valid after freeing the
6874 * CRT chain. */
6875
6876 crt_start = chain->raw.p;
6877 crt_len = chain->raw.len;
6878
6879 pk_start = chain->pk_raw.p;
6880 pk_len = chain->pk_raw.len;
6881
6882 /* Free the CRT structures before computing
6883 * digest and copying the peer's public key. */
6884 mbedtls_x509_crt_free( chain );
6885 mbedtls_free( chain );
6886 chain = NULL;
6887
6888 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6889 if( ret != 0 )
6890 goto exit;
6891
6892 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6893 if( ret != 0 )
6894 goto exit;
6895 }
6896#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6897 /* Pass ownership to session structure. */
6898 ssl->session_negotiate->peer_cert = chain;
6899 chain = NULL;
6900#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6901
6902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6903
6904exit:
6905
6906 if( ret == 0 )
6907 ssl->state++;
6908
6909#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6910 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6911 {
6912 ssl->handshake->ecrs_peer_cert = chain;
6913 chain = NULL;
6914 }
6915#endif
6916
6917 if( chain != NULL )
6918 {
6919 mbedtls_x509_crt_free( chain );
6920 mbedtls_free( chain );
6921 }
6922
6923 return( ret );
6924}
6925#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6926
Andrzej Kurek25f27152022-08-17 16:09:31 -04006927#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006928static void ssl_calc_finished_tls_sha256(
6929 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6930{
6931 int len = 12;
6932 const char *sender;
6933 unsigned char padbuf[32];
6934#if defined(MBEDTLS_USE_PSA_CRYPTO)
6935 size_t hash_size;
6936 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6937 psa_status_t status;
6938#else
6939 mbedtls_sha256_context sha256;
6940#endif
6941
6942 mbedtls_ssl_session *session = ssl->session_negotiate;
6943 if( !session )
6944 session = ssl->session;
6945
6946 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6947 ? "client finished"
6948 : "server finished";
6949
6950#if defined(MBEDTLS_USE_PSA_CRYPTO)
6951 sha256_psa = psa_hash_operation_init();
6952
6953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6954
6955 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6956 if( status != PSA_SUCCESS )
6957 {
6958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6959 return;
6960 }
6961
6962 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6963 if( status != PSA_SUCCESS )
6964 {
6965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6966 return;
6967 }
6968 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6969#else
6970
6971 mbedtls_sha256_init( &sha256 );
6972
6973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6974
6975 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6976
6977 /*
6978 * TLSv1.2:
6979 * hash = PRF( master, finished_label,
6980 * Hash( handshake ) )[0.11]
6981 */
6982
6983#if !defined(MBEDTLS_SHA256_ALT)
6984 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6985 sha256.state, sizeof( sha256.state ) );
6986#endif
6987
6988 mbedtls_sha256_finish( &sha256, padbuf );
6989 mbedtls_sha256_free( &sha256 );
6990#endif /* MBEDTLS_USE_PSA_CRYPTO */
6991
6992 ssl->handshake->tls_prf( session->master, 48, sender,
6993 padbuf, 32, buf, len );
6994
6995 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
6996
6997 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6998
6999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7000}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007001#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007002
Jerry Yub7ba49e2022-02-17 14:25:53 +08007003
Andrzej Kurek25f27152022-08-17 16:09:31 -04007004#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007005static void ssl_calc_finished_tls_sha384(
7006 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7007{
7008 int len = 12;
7009 const char *sender;
7010 unsigned char padbuf[48];
7011#if defined(MBEDTLS_USE_PSA_CRYPTO)
7012 size_t hash_size;
7013 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7014 psa_status_t status;
7015#else
7016 mbedtls_sha512_context sha512;
7017#endif
7018
7019 mbedtls_ssl_session *session = ssl->session_negotiate;
7020 if( !session )
7021 session = ssl->session;
7022
7023 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7024 ? "client finished"
7025 : "server finished";
7026
7027#if defined(MBEDTLS_USE_PSA_CRYPTO)
7028 sha384_psa = psa_hash_operation_init();
7029
7030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7031
7032 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7033 if( status != PSA_SUCCESS )
7034 {
7035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7036 return;
7037 }
7038
7039 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7040 if( status != PSA_SUCCESS )
7041 {
7042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7043 return;
7044 }
7045 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7046#else
7047 mbedtls_sha512_init( &sha512 );
7048
7049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7050
Andrzej Kureka242e832022-08-11 10:03:14 -04007051 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007052
7053 /*
7054 * TLSv1.2:
7055 * hash = PRF( master, finished_label,
7056 * Hash( handshake ) )[0.11]
7057 */
7058
7059#if !defined(MBEDTLS_SHA512_ALT)
7060 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7061 sha512.state, sizeof( sha512.state ) );
7062#endif
7063 mbedtls_sha512_finish( &sha512, padbuf );
7064
7065 mbedtls_sha512_free( &sha512 );
7066#endif
7067
7068 ssl->handshake->tls_prf( session->master, 48, sender,
7069 padbuf, 48, buf, len );
7070
7071 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7072
7073 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7074
7075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7076}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007077#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007078
Jerry Yuaef00152022-02-17 14:27:31 +08007079void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7080{
7081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7082
7083 /*
7084 * Free our handshake params
7085 */
7086 mbedtls_ssl_handshake_free( ssl );
7087 mbedtls_free( ssl->handshake );
7088 ssl->handshake = NULL;
7089
7090 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007091 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007092 */
7093 if( ssl->transform )
7094 {
7095 mbedtls_ssl_transform_free( ssl->transform );
7096 mbedtls_free( ssl->transform );
7097 }
7098 ssl->transform = ssl->transform_negotiate;
7099 ssl->transform_negotiate = NULL;
7100
7101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7102}
7103
Jerry Yu2a9fff52022-02-17 14:28:51 +08007104void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7105{
7106 int resume = ssl->handshake->resume;
7107
7108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7109
7110#if defined(MBEDTLS_SSL_RENEGOTIATION)
7111 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7112 {
7113 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7114 ssl->renego_records_seen = 0;
7115 }
7116#endif
7117
7118 /*
7119 * Free the previous session and switch in the current one
7120 */
7121 if( ssl->session )
7122 {
7123#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7124 /* RFC 7366 3.1: keep the EtM state */
7125 ssl->session_negotiate->encrypt_then_mac =
7126 ssl->session->encrypt_then_mac;
7127#endif
7128
7129 mbedtls_ssl_session_free( ssl->session );
7130 mbedtls_free( ssl->session );
7131 }
7132 ssl->session = ssl->session_negotiate;
7133 ssl->session_negotiate = NULL;
7134
7135 /*
7136 * Add cache entry
7137 */
7138 if( ssl->conf->f_set_cache != NULL &&
7139 ssl->session->id_len != 0 &&
7140 resume == 0 )
7141 {
7142 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7143 ssl->session->id,
7144 ssl->session->id_len,
7145 ssl->session ) != 0 )
7146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7147 }
7148
7149#if defined(MBEDTLS_SSL_PROTO_DTLS)
7150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7151 ssl->handshake->flight != NULL )
7152 {
7153 /* Cancel handshake timer */
7154 mbedtls_ssl_set_timer( ssl, 0 );
7155
7156 /* Keep last flight around in case we need to resend it:
7157 * we need the handshake and transform structures for that */
7158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7159 }
7160 else
7161#endif
7162 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7163
7164 ssl->state++;
7165
7166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7167}
7168
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007169int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7170{
7171 int ret, hash_len;
7172
7173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7174
7175 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7176
7177 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7178
7179 /*
7180 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7181 * may define some other value. Currently (early 2016), no defined
7182 * ciphersuite does this (and this is unlikely to change as activity has
7183 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7184 */
7185 hash_len = 12;
7186
7187#if defined(MBEDTLS_SSL_RENEGOTIATION)
7188 ssl->verify_data_len = hash_len;
7189 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7190#endif
7191
7192 ssl->out_msglen = 4 + hash_len;
7193 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7194 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7195
7196 /*
7197 * In case of session resuming, invert the client and server
7198 * ChangeCipherSpec messages order.
7199 */
7200 if( ssl->handshake->resume != 0 )
7201 {
7202#if defined(MBEDTLS_SSL_CLI_C)
7203 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7204 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7205#endif
7206#if defined(MBEDTLS_SSL_SRV_C)
7207 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7208 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7209#endif
7210 }
7211 else
7212 ssl->state++;
7213
7214 /*
7215 * Switch to our negotiated transform and session parameters for outbound
7216 * data.
7217 */
7218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7219
7220#if defined(MBEDTLS_SSL_PROTO_DTLS)
7221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7222 {
7223 unsigned char i;
7224
7225 /* Remember current epoch settings for resending */
7226 ssl->handshake->alt_transform_out = ssl->transform_out;
7227 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7228 sizeof( ssl->handshake->alt_out_ctr ) );
7229
7230 /* Set sequence_number to zero */
7231 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7232
7233
7234 /* Increment epoch */
7235 for( i = 2; i > 0; i-- )
7236 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7237 break;
7238
7239 /* The loop goes to its end iff the counter is wrapping */
7240 if( i == 0 )
7241 {
7242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7243 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7244 }
7245 }
7246 else
7247#endif /* MBEDTLS_SSL_PROTO_DTLS */
7248 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7249
7250 ssl->transform_out = ssl->transform_negotiate;
7251 ssl->session_out = ssl->session_negotiate;
7252
7253#if defined(MBEDTLS_SSL_PROTO_DTLS)
7254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7255 mbedtls_ssl_send_flight_completed( ssl );
7256#endif
7257
7258 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7259 {
7260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7261 return( ret );
7262 }
7263
7264#if defined(MBEDTLS_SSL_PROTO_DTLS)
7265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7266 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7267 {
7268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7269 return( ret );
7270 }
7271#endif
7272
7273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7274
7275 return( 0 );
7276}
7277
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007278#define SSL_MAX_HASH_LEN 12
7279
7280int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7281{
7282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7283 unsigned int hash_len = 12;
7284 unsigned char buf[SSL_MAX_HASH_LEN];
7285
7286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7287
7288 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7289
7290 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7291 {
7292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7293 goto exit;
7294 }
7295
7296 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7297 {
7298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7299 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7300 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7301 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7302 goto exit;
7303 }
7304
7305 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7306 {
7307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7308 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7309 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7310 goto exit;
7311 }
7312
7313 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7314 {
7315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7317 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7318 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7319 goto exit;
7320 }
7321
7322 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7323 buf, hash_len ) != 0 )
7324 {
7325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7326 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7327 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7328 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7329 goto exit;
7330 }
7331
7332#if defined(MBEDTLS_SSL_RENEGOTIATION)
7333 ssl->verify_data_len = hash_len;
7334 memcpy( ssl->peer_verify_data, buf, hash_len );
7335#endif
7336
7337 if( ssl->handshake->resume != 0 )
7338 {
7339#if defined(MBEDTLS_SSL_CLI_C)
7340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7341 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7342#endif
7343#if defined(MBEDTLS_SSL_SRV_C)
7344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7345 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7346#endif
7347 }
7348 else
7349 ssl->state++;
7350
7351#if defined(MBEDTLS_SSL_PROTO_DTLS)
7352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7353 mbedtls_ssl_recv_flight_completed( ssl );
7354#endif
7355
7356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7357
7358exit:
7359 mbedtls_platform_zeroize( buf, hash_len );
7360 return( ret );
7361}
7362
Jerry Yu392112c2022-02-17 14:34:10 +08007363#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7364/*
7365 * Helper to get TLS 1.2 PRF from ciphersuite
7366 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7367 */
7368static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7369{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007370#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007371 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7372 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7373
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007374 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007375 return( tls_prf_sha384 );
7376#else
7377 (void) ciphersuite_id;
7378#endif
7379 return( tls_prf_sha256 );
7380}
7381#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007382
7383static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7384{
7385 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007386#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007387 if( tls_prf == tls_prf_sha384 )
7388 {
7389 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7390 }
7391 else
7392#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007393#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007394 if( tls_prf == tls_prf_sha256 )
7395 {
7396 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7397 }
7398 else
7399#endif
7400 return( MBEDTLS_SSL_TLS_PRF_NONE );
7401}
7402
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007403/*
7404 * Populate a transform structure with session keys and all the other
7405 * necessary information.
7406 *
7407 * Parameters:
7408 * - [in/out]: transform: structure to populate
7409 * [in] must be just initialised with mbedtls_ssl_transform_init()
7410 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7411 * - [in] ciphersuite
7412 * - [in] master
7413 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007414 * - [in] tls_prf: pointer to PRF to use for key derivation
7415 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007416 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007417 * - [in] endpoint: client or server
7418 * - [in] ssl: used for:
7419 * - ssl->conf->{f,p}_export_keys
7420 * [in] optionally used for:
7421 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7422 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007423MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007424static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7425 int ciphersuite,
7426 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007428 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007429#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007430 ssl_tls_prf_t tls_prf,
7431 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007432 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007433 unsigned endpoint,
7434 const mbedtls_ssl_context *ssl )
7435{
7436 int ret = 0;
7437 unsigned char keyblk[256];
7438 unsigned char *key1;
7439 unsigned char *key2;
7440 unsigned char *mac_enc;
7441 unsigned char *mac_dec;
7442 size_t mac_key_len = 0;
7443 size_t iv_copy_len;
7444 size_t keylen;
7445 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007446 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007447#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007448 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007449 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007450#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007451
7452#if defined(MBEDTLS_USE_PSA_CRYPTO)
7453 psa_key_type_t key_type;
7454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7455 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007456 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007457 size_t key_bits;
7458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7459#endif
7460
7461#if !defined(MBEDTLS_DEBUG_C) && \
7462 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7463 if( ssl->f_export_keys == NULL )
7464 {
7465 ssl = NULL; /* make sure we don't use it except for these cases */
7466 (void) ssl;
7467 }
7468#endif
7469
7470 /*
7471 * Some data just needs copying into the structure
7472 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007473#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007474 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007475#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007476 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007477
7478#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7479 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7480#endif
7481
7482#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007483 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007484 {
7485 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7486 * generation separate. This should never happen. */
7487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7488 }
7489#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7490
7491 /*
7492 * Get various info structures
7493 */
7494 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7495 if( ciphersuite_info == NULL )
7496 {
7497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7498 ciphersuite ) );
7499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7500 }
7501
Neil Armstrongab555e02022-04-04 11:07:59 +02007502 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007503#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007504 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007505#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007506 ciphersuite_info );
7507
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007508 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7509 transform->taglen =
7510 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7511
7512#if defined(MBEDTLS_USE_PSA_CRYPTO)
7513 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7514 transform->taglen,
7515 &alg,
7516 &key_type,
7517 &key_bits ) ) != PSA_SUCCESS )
7518 {
7519 ret = psa_ssl_status_to_mbedtls( status );
7520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7521 goto end;
7522 }
7523#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007524 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7525 if( cipher_info == NULL )
7526 {
7527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7528 ciphersuite_info->cipher ) );
7529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7530 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007531#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532
Neil Armstronge4512952022-03-08 09:08:22 +01007533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007534 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007535 if( mac_alg == 0 )
7536 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007538 (unsigned) ciphersuite_info->mac ) );
7539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7540 }
7541#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007542 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7543 if( md_info == NULL )
7544 {
7545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7546 (unsigned) ciphersuite_info->mac ) );
7547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7548 }
Neil Armstronge4512952022-03-08 09:08:22 +01007549#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550
7551#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7552 /* Copy own and peer's CID if the use of the CID
7553 * extension has been negotiated. */
7554 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7555 {
7556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7557
7558 transform->in_cid_len = ssl->own_cid_len;
7559 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7560 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7561 transform->in_cid_len );
7562
7563 transform->out_cid_len = ssl->handshake->peer_cid_len;
7564 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7565 ssl->handshake->peer_cid_len );
7566 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7567 transform->out_cid_len );
7568 }
7569#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7570
7571 /*
7572 * Compute key block using the PRF
7573 */
7574 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7575 if( ret != 0 )
7576 {
7577 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7578 return( ret );
7579 }
7580
7581 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7582 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7583 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7584 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7585 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7586
7587 /*
7588 * Determine the appropriate key, IV and MAC length.
7589 */
7590
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007591#if defined(MBEDTLS_USE_PSA_CRYPTO)
7592 keylen = PSA_BITS_TO_BYTES(key_bits);
7593#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007594 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007595#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007596
7597#if defined(MBEDTLS_GCM_C) || \
7598 defined(MBEDTLS_CCM_C) || \
7599 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007600 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007601 {
7602 size_t explicit_ivlen;
7603
7604 transform->maclen = 0;
7605 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007606
7607 /* All modes haves 96-bit IVs, but the length of the static parts vary
7608 * with mode and version:
7609 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7610 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7611 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7612 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7613 * sequence number).
7614 */
7615 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007616#if defined(MBEDTLS_USE_PSA_CRYPTO)
7617 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7618#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007619 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007620#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007621 transform->fixed_ivlen = 12;
7622 else
7623 transform->fixed_ivlen = 4;
7624
7625 /* Minimum length of encrypted record */
7626 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7627 transform->minlen = explicit_ivlen + transform->taglen;
7628 }
7629 else
7630#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7631#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007632 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7633 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7634 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007635 {
Neil Armstronge4512952022-03-08 09:08:22 +01007636#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007637 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007638#else
7639 size_t block_size = cipher_info->block_size;
7640#endif /* MBEDTLS_USE_PSA_CRYPTO */
7641
7642#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007643 /* Get MAC length */
7644 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7645#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007646 /* Initialize HMAC contexts */
7647 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7648 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7649 {
7650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7651 goto end;
7652 }
7653
7654 /* Get MAC length */
7655 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007656#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007657 transform->maclen = mac_key_len;
7658
7659 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007660#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007661 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007662#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007663 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007664#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007665
7666 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007667 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007668 transform->minlen = transform->maclen;
7669 else
7670 {
7671 /*
7672 * GenericBlockCipher:
7673 * 1. if EtM is in use: one block plus MAC
7674 * otherwise: * first multiple of blocklen greater than maclen
7675 * 2. IV
7676 */
7677#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007678 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007679 {
7680 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007681 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007682 }
7683 else
7684#endif
7685 {
7686 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007687 + block_size
7688 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007689 }
7690
Glenn Strauss07c64162022-03-14 12:34:51 -04007691 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692 {
7693 transform->minlen += transform->ivlen;
7694 }
7695 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007696 {
7697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7698 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7699 goto end;
7700 }
7701 }
7702 }
7703 else
7704#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7705 {
7706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7708 }
7709
7710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7711 (unsigned) keylen,
7712 (unsigned) transform->minlen,
7713 (unsigned) transform->ivlen,
7714 (unsigned) transform->maclen ) );
7715
7716 /*
7717 * Finally setup the cipher contexts, IVs and MAC secrets.
7718 */
7719#if defined(MBEDTLS_SSL_CLI_C)
7720 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7721 {
7722 key1 = keyblk + mac_key_len * 2;
7723 key2 = keyblk + mac_key_len * 2 + keylen;
7724
7725 mac_enc = keyblk;
7726 mac_dec = keyblk + mac_key_len;
7727
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007728 iv_copy_len = ( transform->fixed_ivlen ) ?
7729 transform->fixed_ivlen : transform->ivlen;
7730 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7731 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7732 iv_copy_len );
7733 }
7734 else
7735#endif /* MBEDTLS_SSL_CLI_C */
7736#if defined(MBEDTLS_SSL_SRV_C)
7737 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7738 {
7739 key1 = keyblk + mac_key_len * 2 + keylen;
7740 key2 = keyblk + mac_key_len * 2;
7741
7742 mac_enc = keyblk + mac_key_len;
7743 mac_dec = keyblk;
7744
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007745 iv_copy_len = ( transform->fixed_ivlen ) ?
7746 transform->fixed_ivlen : transform->ivlen;
7747 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7748 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7749 iv_copy_len );
7750 }
7751 else
7752#endif /* MBEDTLS_SSL_SRV_C */
7753 {
7754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7755 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7756 goto end;
7757 }
7758
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759 if( ssl != NULL && ssl->f_export_keys != NULL )
7760 {
7761 ssl->f_export_keys( ssl->p_export_keys,
7762 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7763 master, 48,
7764 randbytes + 32,
7765 randbytes,
7766 tls_prf_get_type( tls_prf ) );
7767 }
7768
7769#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007770 transform->psa_alg = alg;
7771
7772 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7773 {
7774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7775 psa_set_key_algorithm( &attributes, alg );
7776 psa_set_key_type( &attributes, key_type );
7777
7778 if( ( status = psa_import_key( &attributes,
7779 key1,
7780 PSA_BITS_TO_BYTES( key_bits ),
7781 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7782 {
7783 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7784 ret = psa_ssl_status_to_mbedtls( status );
7785 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7786 goto end;
7787 }
7788
7789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7790
7791 if( ( status = psa_import_key( &attributes,
7792 key2,
7793 PSA_BITS_TO_BYTES( key_bits ),
7794 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7795 {
7796 ret = psa_ssl_status_to_mbedtls( status );
7797 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7798 goto end;
7799 }
7800 }
7801#else
7802 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7803 cipher_info ) ) != 0 )
7804 {
7805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7806 goto end;
7807 }
7808
7809 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7810 cipher_info ) ) != 0 )
7811 {
7812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7813 goto end;
7814 }
7815
7816 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7817 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7818 MBEDTLS_ENCRYPT ) ) != 0 )
7819 {
7820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7821 goto end;
7822 }
7823
7824 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7825 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7826 MBEDTLS_DECRYPT ) ) != 0 )
7827 {
7828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7829 goto end;
7830 }
7831
7832#if defined(MBEDTLS_CIPHER_MODE_CBC)
7833 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7834 {
7835 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7836 MBEDTLS_PADDING_NONE ) ) != 0 )
7837 {
7838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7839 goto end;
7840 }
7841
7842 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7843 MBEDTLS_PADDING_NONE ) ) != 0 )
7844 {
7845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7846 goto end;
7847 }
7848 }
7849#endif /* MBEDTLS_CIPHER_MODE_CBC */
7850#endif /* MBEDTLS_USE_PSA_CRYPTO */
7851
Neil Armstrong29c0c042022-03-17 17:47:28 +01007852#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7853 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7854 For AEAD-based ciphersuites, there is nothing to do here. */
7855 if( mac_key_len != 0 )
7856 {
7857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007858 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007859
7860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007861 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007862 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7863
7864 if( ( status = psa_import_key( &attributes,
7865 mac_enc, mac_key_len,
7866 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7867 {
7868 ret = psa_ssl_status_to_mbedtls( status );
7869 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7870 goto end;
7871 }
7872
Ronald Cronfb39f152022-03-25 14:36:28 +01007873 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007874 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7875#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7876 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7877#endif
7878 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007879 /* mbedtls_ct_hmac() requires the key to be exportable */
7880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7881 PSA_KEY_USAGE_VERIFY_HASH );
7882 else
7883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7884
7885 if( ( status = psa_import_key( &attributes,
7886 mac_dec, mac_key_len,
7887 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7888 {
7889 ret = psa_ssl_status_to_mbedtls( status );
7890 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7891 goto end;
7892 }
7893#else
7894 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7895 if( ret != 0 )
7896 goto end;
7897 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7898 if( ret != 0 )
7899 goto end;
7900#endif /* MBEDTLS_USE_PSA_CRYPTO */
7901 }
7902#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7903
7904 ((void) mac_dec);
7905 ((void) mac_enc);
7906
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007907end:
7908 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7909 return( ret );
7910}
7911
Jerry Yuee40f9d2022-02-17 14:55:16 +08007912#if defined(MBEDTLS_USE_PSA_CRYPTO)
7913int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7914 unsigned char *hash, size_t *hashlen,
7915 unsigned char *data, size_t data_len,
7916 mbedtls_md_type_t md_alg )
7917{
7918 psa_status_t status;
7919 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007920 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007921
7922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7923
7924 if( ( status = psa_hash_setup( &hash_operation,
7925 hash_alg ) ) != PSA_SUCCESS )
7926 {
7927 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7928 goto exit;
7929 }
7930
7931 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7932 64 ) ) != PSA_SUCCESS )
7933 {
7934 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7935 goto exit;
7936 }
7937
7938 if( ( status = psa_hash_update( &hash_operation,
7939 data, data_len ) ) != PSA_SUCCESS )
7940 {
7941 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7942 goto exit;
7943 }
7944
7945 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7946 hashlen ) ) != PSA_SUCCESS )
7947 {
7948 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7949 goto exit;
7950 }
7951
7952exit:
7953 if( status != PSA_SUCCESS )
7954 {
7955 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7956 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7957 switch( status )
7958 {
7959 case PSA_ERROR_NOT_SUPPORTED:
7960 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7961 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7962 case PSA_ERROR_BUFFER_TOO_SMALL:
7963 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7964 case PSA_ERROR_INSUFFICIENT_MEMORY:
7965 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7966 default:
7967 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7968 }
7969 }
7970 return( 0 );
7971}
7972
7973#else
7974
7975int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7976 unsigned char *hash, size_t *hashlen,
7977 unsigned char *data, size_t data_len,
7978 mbedtls_md_type_t md_alg )
7979{
7980 int ret = 0;
7981 mbedtls_md_context_t ctx;
7982 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7983 *hashlen = mbedtls_md_get_size( md_info );
7984
7985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7986
7987 mbedtls_md_init( &ctx );
7988
7989 /*
7990 * digitally-signed struct {
7991 * opaque client_random[32];
7992 * opaque server_random[32];
7993 * ServerDHParams params;
7994 * };
7995 */
7996 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
7997 {
7998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7999 goto exit;
8000 }
8001 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8002 {
8003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8004 goto exit;
8005 }
8006 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8007 {
8008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8009 goto exit;
8010 }
8011 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8012 {
8013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8014 goto exit;
8015 }
8016 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8017 {
8018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8019 goto exit;
8020 }
8021
8022exit:
8023 mbedtls_md_free( &ctx );
8024
8025 if( ret != 0 )
8026 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8027 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8028
8029 return( ret );
8030}
8031#endif /* MBEDTLS_USE_PSA_CRYPTO */
8032
Jerry Yud9d91da2022-02-17 14:57:06 +08008033#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8034
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008035/* Find the preferred hash for a given signature algorithm. */
8036unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8037 mbedtls_ssl_context *ssl,
8038 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008039{
Gabor Mezei078e8032022-04-27 21:17:56 +02008040 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008041 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008042
8043 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008044 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008045
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008046 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008047 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008048 unsigned int hash_alg_received =
8049 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8050 received_sig_algs[i] );
8051 unsigned int sig_alg_received =
8052 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8053 received_sig_algs[i] );
8054
8055 if( sig_alg == sig_alg_received )
8056 {
8057#if defined(MBEDTLS_USE_PSA_CRYPTO)
8058 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8059 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008060 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008061 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008062
Neil Armstrong96eceb82022-06-30 18:05:05 +02008063 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8064 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8065 PSA_ALG_ECDSA( psa_hash_alg ),
8066 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008067 continue;
8068
Neil Armstrong96eceb82022-06-30 18:05:05 +02008069 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008070 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8071 PSA_ALG_RSA_PKCS1V15_SIGN(
8072 psa_hash_alg ),
8073 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008074 continue;
8075 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008076#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008077
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008078 return( hash_alg_received );
8079 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008080 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008081
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008082 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008083}
8084
8085#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8086
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008087/* Serialization of TLS 1.2 sessions:
8088 *
8089 * struct {
8090 * uint64 start_time;
8091 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008092 * uint8 session_id_len; // at most 32
8093 * opaque session_id[32];
8094 * opaque master[48]; // fixed length in the standard
8095 * uint32 verify_result;
8096 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8097 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8098 * uint32 ticket_lifetime;
8099 * uint8 mfl_code; // up to 255 according to standard
8100 * uint8 encrypt_then_mac; // 0 or 1
8101 * } serialized_session_tls12;
8102 *
8103 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008104static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008105 unsigned char *buf,
8106 size_t buf_len )
8107{
8108 unsigned char *p = buf;
8109 size_t used = 0;
8110
8111#if defined(MBEDTLS_HAVE_TIME)
8112 uint64_t start;
8113#endif
8114#if defined(MBEDTLS_X509_CRT_PARSE_C)
8115#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8116 size_t cert_len;
8117#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8118#endif /* MBEDTLS_X509_CRT_PARSE_C */
8119
8120 /*
8121 * Time
8122 */
8123#if defined(MBEDTLS_HAVE_TIME)
8124 used += 8;
8125
8126 if( used <= buf_len )
8127 {
8128 start = (uint64_t) session->start;
8129
8130 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8131 p += 8;
8132 }
8133#endif /* MBEDTLS_HAVE_TIME */
8134
8135 /*
8136 * Basic mandatory fields
8137 */
8138 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008139 + 1 /* id_len */
8140 + sizeof( session->id )
8141 + sizeof( session->master )
8142 + 4; /* verify_result */
8143
8144 if( used <= buf_len )
8145 {
8146 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8147 p += 2;
8148
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008149 *p++ = MBEDTLS_BYTE_0( session->id_len );
8150 memcpy( p, session->id, 32 );
8151 p += 32;
8152
8153 memcpy( p, session->master, 48 );
8154 p += 48;
8155
8156 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8157 p += 4;
8158 }
8159
8160 /*
8161 * Peer's end-entity certificate
8162 */
8163#if defined(MBEDTLS_X509_CRT_PARSE_C)
8164#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8165 if( session->peer_cert == NULL )
8166 cert_len = 0;
8167 else
8168 cert_len = session->peer_cert->raw.len;
8169
8170 used += 3 + cert_len;
8171
8172 if( used <= buf_len )
8173 {
8174 *p++ = MBEDTLS_BYTE_2( cert_len );
8175 *p++ = MBEDTLS_BYTE_1( cert_len );
8176 *p++ = MBEDTLS_BYTE_0( cert_len );
8177
8178 if( session->peer_cert != NULL )
8179 {
8180 memcpy( p, session->peer_cert->raw.p, cert_len );
8181 p += cert_len;
8182 }
8183 }
8184#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8185 if( session->peer_cert_digest != NULL )
8186 {
8187 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8188 if( used <= buf_len )
8189 {
8190 *p++ = (unsigned char) session->peer_cert_digest_type;
8191 *p++ = (unsigned char) session->peer_cert_digest_len;
8192 memcpy( p, session->peer_cert_digest,
8193 session->peer_cert_digest_len );
8194 p += session->peer_cert_digest_len;
8195 }
8196 }
8197 else
8198 {
8199 used += 2;
8200 if( used <= buf_len )
8201 {
8202 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8203 *p++ = 0;
8204 }
8205 }
8206#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8207#endif /* MBEDTLS_X509_CRT_PARSE_C */
8208
8209 /*
8210 * Session ticket if any, plus associated data
8211 */
8212#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8213 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8214
8215 if( used <= buf_len )
8216 {
8217 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8218 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8219 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8220
8221 if( session->ticket != NULL )
8222 {
8223 memcpy( p, session->ticket, session->ticket_len );
8224 p += session->ticket_len;
8225 }
8226
8227 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8228 p += 4;
8229 }
8230#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8231
8232 /*
8233 * Misc extension-related info
8234 */
8235#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8236 used += 1;
8237
8238 if( used <= buf_len )
8239 *p++ = session->mfl_code;
8240#endif
8241
8242#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8243 used += 1;
8244
8245 if( used <= buf_len )
8246 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8247#endif
8248
8249 return( used );
8250}
8251
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008252MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008253static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008254 const unsigned char *buf,
8255 size_t len )
8256{
8257#if defined(MBEDTLS_HAVE_TIME)
8258 uint64_t start;
8259#endif
8260#if defined(MBEDTLS_X509_CRT_PARSE_C)
8261#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8262 size_t cert_len;
8263#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8264#endif /* MBEDTLS_X509_CRT_PARSE_C */
8265
8266 const unsigned char *p = buf;
8267 const unsigned char * const end = buf + len;
8268
8269 /*
8270 * Time
8271 */
8272#if defined(MBEDTLS_HAVE_TIME)
8273 if( 8 > (size_t)( end - p ) )
8274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8275
8276 start = ( (uint64_t) p[0] << 56 ) |
8277 ( (uint64_t) p[1] << 48 ) |
8278 ( (uint64_t) p[2] << 40 ) |
8279 ( (uint64_t) p[3] << 32 ) |
8280 ( (uint64_t) p[4] << 24 ) |
8281 ( (uint64_t) p[5] << 16 ) |
8282 ( (uint64_t) p[6] << 8 ) |
8283 ( (uint64_t) p[7] );
8284 p += 8;
8285
8286 session->start = (time_t) start;
8287#endif /* MBEDTLS_HAVE_TIME */
8288
8289 /*
8290 * Basic mandatory fields
8291 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008292 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8294
8295 session->ciphersuite = ( p[0] << 8 ) | p[1];
8296 p += 2;
8297
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008298 session->id_len = *p++;
8299 memcpy( session->id, p, 32 );
8300 p += 32;
8301
8302 memcpy( session->master, p, 48 );
8303 p += 48;
8304
8305 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8306 ( (uint32_t) p[1] << 16 ) |
8307 ( (uint32_t) p[2] << 8 ) |
8308 ( (uint32_t) p[3] );
8309 p += 4;
8310
8311 /* Immediately clear invalid pointer values that have been read, in case
8312 * we exit early before we replaced them with valid ones. */
8313#if defined(MBEDTLS_X509_CRT_PARSE_C)
8314#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8315 session->peer_cert = NULL;
8316#else
8317 session->peer_cert_digest = NULL;
8318#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8319#endif /* MBEDTLS_X509_CRT_PARSE_C */
8320#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8321 session->ticket = NULL;
8322#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8323
8324 /*
8325 * Peer certificate
8326 */
8327#if defined(MBEDTLS_X509_CRT_PARSE_C)
8328#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8329 /* Deserialize CRT from the end of the ticket. */
8330 if( 3 > (size_t)( end - p ) )
8331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8332
8333 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8334 p += 3;
8335
8336 if( cert_len != 0 )
8337 {
8338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8339
8340 if( cert_len > (size_t)( end - p ) )
8341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8342
8343 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8344
8345 if( session->peer_cert == NULL )
8346 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8347
8348 mbedtls_x509_crt_init( session->peer_cert );
8349
8350 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8351 p, cert_len ) ) != 0 )
8352 {
8353 mbedtls_x509_crt_free( session->peer_cert );
8354 mbedtls_free( session->peer_cert );
8355 session->peer_cert = NULL;
8356 return( ret );
8357 }
8358
8359 p += cert_len;
8360 }
8361#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8362 /* Deserialize CRT digest from the end of the ticket. */
8363 if( 2 > (size_t)( end - p ) )
8364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8365
8366 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8367 session->peer_cert_digest_len = (size_t) *p++;
8368
8369 if( session->peer_cert_digest_len != 0 )
8370 {
8371 const mbedtls_md_info_t *md_info =
8372 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8373 if( md_info == NULL )
8374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8375 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8377
8378 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8380
8381 session->peer_cert_digest =
8382 mbedtls_calloc( 1, session->peer_cert_digest_len );
8383 if( session->peer_cert_digest == NULL )
8384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8385
8386 memcpy( session->peer_cert_digest, p,
8387 session->peer_cert_digest_len );
8388 p += session->peer_cert_digest_len;
8389 }
8390#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8391#endif /* MBEDTLS_X509_CRT_PARSE_C */
8392
8393 /*
8394 * Session ticket and associated data
8395 */
8396#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8397 if( 3 > (size_t)( end - p ) )
8398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8399
8400 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8401 p += 3;
8402
8403 if( session->ticket_len != 0 )
8404 {
8405 if( session->ticket_len > (size_t)( end - p ) )
8406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8407
8408 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8409 if( session->ticket == NULL )
8410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8411
8412 memcpy( session->ticket, p, session->ticket_len );
8413 p += session->ticket_len;
8414 }
8415
8416 if( 4 > (size_t)( end - p ) )
8417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8418
8419 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8420 ( (uint32_t) p[1] << 16 ) |
8421 ( (uint32_t) p[2] << 8 ) |
8422 ( (uint32_t) p[3] );
8423 p += 4;
8424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8425
8426 /*
8427 * Misc extension-related info
8428 */
8429#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8430 if( 1 > (size_t)( end - p ) )
8431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8432
8433 session->mfl_code = *p++;
8434#endif
8435
8436#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8437 if( 1 > (size_t)( end - p ) )
8438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8439
8440 session->encrypt_then_mac = *p++;
8441#endif
8442
8443 /* Done, should have consumed entire buffer */
8444 if( p != end )
8445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8446
8447 return( 0 );
8448}
Jerry Yudc7bd172022-02-17 13:44:15 +08008449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8450
XiaokangQian75d40ef2022-04-20 11:05:24 +00008451int mbedtls_ssl_validate_ciphersuite(
8452 const mbedtls_ssl_context *ssl,
8453 const mbedtls_ssl_ciphersuite_t *suite_info,
8454 mbedtls_ssl_protocol_version min_tls_version,
8455 mbedtls_ssl_protocol_version max_tls_version )
8456{
8457 (void) ssl;
8458
8459 if( suite_info == NULL )
8460 return( -1 );
8461
8462 if( ( suite_info->min_tls_version > max_tls_version ) ||
8463 ( suite_info->max_tls_version < min_tls_version ) )
8464 {
8465 return( -1 );
8466 }
8467
XiaokangQian060d8672022-04-21 09:24:56 +00008468#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008469#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8470 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8471 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8472 {
8473 return( -1 );
8474 }
8475#endif
8476
8477 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8478#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8479 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8480 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8481 {
8482 return( -1 );
8483 }
8484#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8485#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8486
8487 return( 0 );
8488}
8489
XiaokangQianeaf36512022-04-24 09:07:44 +00008490#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8491/*
8492 * Function for writing a signature algorithm extension.
8493 *
8494 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8495 * value (TLS 1.3 RFC8446):
8496 * enum {
8497 * ....
8498 * ecdsa_secp256r1_sha256( 0x0403 ),
8499 * ecdsa_secp384r1_sha384( 0x0503 ),
8500 * ecdsa_secp521r1_sha512( 0x0603 ),
8501 * ....
8502 * } SignatureScheme;
8503 *
8504 * struct {
8505 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8506 * } SignatureSchemeList;
8507 *
8508 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8509 * value (TLS 1.2 RFC5246):
8510 * enum {
8511 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8512 * sha512(6), (255)
8513 * } HashAlgorithm;
8514 *
8515 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8516 * SignatureAlgorithm;
8517 *
8518 * struct {
8519 * HashAlgorithm hash;
8520 * SignatureAlgorithm signature;
8521 * } SignatureAndHashAlgorithm;
8522 *
8523 * SignatureAndHashAlgorithm
8524 * supported_signature_algorithms<2..2^16-2>;
8525 *
8526 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8527 * generalization of the TLS 1.2 signature algorithm extension.
8528 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8529 * `SignatureScheme` field of TLS 1.3
8530 *
8531 */
8532int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8533 const unsigned char *end, size_t *out_len )
8534{
8535 unsigned char *p = buf;
8536 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8537 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8538
8539 *out_len = 0;
8540
8541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8542
8543 /* Check if we have space for header and length field:
8544 * - extension_type (2 bytes)
8545 * - extension_data_length (2 bytes)
8546 * - supported_signature_algorithms_length (2 bytes)
8547 */
8548 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8549 p += 6;
8550
8551 /*
8552 * Write supported_signature_algorithms
8553 */
8554 supported_sig_alg = p;
8555 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8556 if( sig_alg == NULL )
8557 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8558
8559 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8560 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8562 *sig_alg,
8563 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008564 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8565 continue;
8566 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8567 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8568 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008570 *sig_alg,
8571 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008572 }
8573
8574 /* Length of supported_signature_algorithms */
8575 supported_sig_alg_len = p - supported_sig_alg;
8576 if( supported_sig_alg_len == 0 )
8577 {
8578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8580 }
8581
XiaokangQianeaf36512022-04-24 09:07:44 +00008582 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008583 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008584 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8585
XiaokangQianeaf36512022-04-24 09:07:44 +00008586 *out_len = p - buf;
8587
8588#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8589 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8590#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8591 return( 0 );
8592}
8593#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8594
XiaokangQian40a35232022-05-07 09:02:40 +00008595#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008596/*
8597 * mbedtls_ssl_parse_server_name_ext
8598 *
8599 * Structure of server_name extension:
8600 *
8601 * enum {
8602 * host_name(0), (255)
8603 * } NameType;
8604 * opaque HostName<1..2^16-1>;
8605 *
8606 * struct {
8607 * NameType name_type;
8608 * select (name_type) {
8609 * case host_name: HostName;
8610 * } name;
8611 * } ServerName;
8612 * struct {
8613 * ServerName server_name_list<1..2^16-1>
8614 * } ServerNameList;
8615 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008616MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008617int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8618 const unsigned char *buf,
8619 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008620{
8621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8622 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008623 size_t server_name_list_len, hostname_len;
8624 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008625
XiaokangQianf2a94202022-05-20 06:44:24 +00008626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008627
8628 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008629 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008630 p += 2;
8631
XiaokangQian9b2b7712022-05-17 02:57:00 +00008632 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8633 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008634 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008635 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008636 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008637 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008638 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8639 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008640
8641 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8642 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008643 /* sni_name is intended to be used only during the parsing of the
8644 * ClientHello message (it is reset to NULL before the end of
8645 * the message parsing). Thus it is ok to just point to the
8646 * reception buffer and not make a copy of it.
8647 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008648 ssl->handshake->sni_name = p + 3;
8649 ssl->handshake->sni_name_len = hostname_len;
8650 if( ssl->conf->f_sni == NULL )
8651 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008652 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008653 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008654 if( ret != 0 )
8655 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008656 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008657 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8658 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008659 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8660 }
8661 return( 0 );
8662 }
8663
8664 p += hostname_len + 3;
8665 }
8666
8667 return( 0 );
8668}
8669#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8670
XiaokangQianacb39922022-06-17 10:18:48 +00008671#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008672MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008673int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8674 const unsigned char *buf,
8675 const unsigned char *end )
8676{
8677 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008678 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008679 const unsigned char *protocol_name_list;
8680 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008681 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008682
8683 /* If ALPN not configured, just ignore the extension */
8684 if( ssl->conf->alpn_list == NULL )
8685 return( 0 );
8686
8687 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008688 * RFC7301, section 3.1
8689 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008690 *
XiaokangQianc7403452022-06-23 03:24:12 +00008691 * struct {
8692 * ProtocolName protocol_name_list<2..2^16-1>
8693 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008694 */
8695
XiaokangQianc7403452022-06-23 03:24:12 +00008696 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008697 * protocol_name_list_len 2 bytes
8698 * protocol_name_len 1 bytes
8699 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008700 */
XiaokangQianacb39922022-06-17 10:18:48 +00008701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8702
XiaokangQianc7403452022-06-23 03:24:12 +00008703 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008704 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008706 protocol_name_list = p;
8707 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008708
8709 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008710 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008711 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008712 protocol_name_len = *p++;
8713 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8714 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008715 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008716 {
8717 MBEDTLS_SSL_PEND_FATAL_ALERT(
8718 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8719 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008720 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008721 }
8722
8723 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008724 }
8725
8726 /* Use our order of preference */
8727 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8728 {
8729 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008730 p = protocol_name_list;
8731 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008732 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008733 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008734 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008735 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008736 {
8737 ssl->alpn_chosen = *alpn;
8738 return( 0 );
8739 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008740
8741 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008742 }
8743 }
8744
XiaokangQian95d5f542022-06-24 02:29:26 +00008745 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008746 MBEDTLS_SSL_PEND_FATAL_ALERT(
8747 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8748 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8749 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8750}
8751
8752int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8753 unsigned char *buf,
8754 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008755 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008756{
8757 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008758 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008759 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008760
8761 if( ssl->alpn_chosen == NULL )
8762 {
8763 return( 0 );
8764 }
8765
XiaokangQian95d5f542022-06-24 02:29:26 +00008766 protocol_name_len = strlen( ssl->alpn_chosen );
8767 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008768
8769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8770 /*
8771 * 0 . 1 ext identifier
8772 * 2 . 3 ext length
8773 * 4 . 5 protocol list length
8774 * 6 . 6 protocol name length
8775 * 7 . 7+n protocol name
8776 */
8777 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8778
XiaokangQian95d5f542022-06-24 02:29:26 +00008779 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008780
XiaokangQian95d5f542022-06-24 02:29:26 +00008781 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8782 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008783 /* Note: the length of the chosen protocol has been checked to be less
8784 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8785 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008786 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008787
XiaokangQian95d5f542022-06-24 02:29:26 +00008788 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008789 return ( 0 );
8790}
8791#endif /* MBEDTLS_SSL_ALPN */
8792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793#endif /* MBEDTLS_SSL_TLS_C */