blob: 9741a6ef5f64a6de0eb101215b0a098ae2d80606 [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 Yu21092062022-10-10 21:21:31 +08001376#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1377 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001378 {
Jerry Yu21092062022-10-10 21:21:31 +08001379 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1380 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1381
1382 if( mbedtls_ssl_validate_ciphersuite(
1383 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1384 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1385 {
1386 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1387 session->ciphersuite ) );
1388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1389 }
Jerry Yu40afab62022-10-08 10:42:13 +08001390 }
Jerry Yu21092062022-10-10 21:21:31 +08001391#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001392
Hanno Becker52055ae2019-02-06 14:30:46 +00001393 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1394 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001395 return( ret );
1396
Paul Bakker0a597072012-09-25 21:55:46 +00001397 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001398
1399 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001403void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1404 const int *ciphersuites )
1405{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001406 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407}
1408
Ronald Cron6f135e12021-12-08 16:57:54 +01001409#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001410void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001411 const int kex_modes )
1412{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001413 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001414}
Ronald Cron6f135e12021-12-08 16:57:54 +01001415#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001418void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001419 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001420{
1421 conf->cert_profile = profile;
1422}
1423
Glenn Strauss36872db2022-01-22 05:06:31 -05001424static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1425{
1426 mbedtls_ssl_key_cert *cur = key_cert, *next;
1427
1428 while( cur != NULL )
1429 {
1430 next = cur->next;
1431 mbedtls_free( cur );
1432 cur = next;
1433 }
1434}
1435
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001436/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001437MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001438static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1439 mbedtls_x509_crt *cert,
1440 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001441{
niisato8ee24222018-06-25 19:05:48 +09001442 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001443
Glenn Strauss36872db2022-01-22 05:06:31 -05001444 if( cert == NULL )
1445 {
1446 /* Free list if cert is null */
1447 ssl_key_cert_free( *head );
1448 *head = NULL;
1449 return( 0 );
1450 }
1451
niisato8ee24222018-06-25 19:05:48 +09001452 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1453 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001454 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001455
niisato8ee24222018-06-25 19:05:48 +09001456 new_cert->cert = cert;
1457 new_cert->key = key;
1458 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001459
Glenn Strauss36872db2022-01-22 05:06:31 -05001460 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001461 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001462 {
niisato8ee24222018-06-25 19:05:48 +09001463 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001464 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001465 else
1466 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001467 mbedtls_ssl_key_cert *cur = *head;
1468 while( cur->next != NULL )
1469 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001470 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001471 }
1472
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001473 return( 0 );
1474}
1475
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001476int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001477 mbedtls_x509_crt *own_cert,
1478 mbedtls_pk_context *pk_key )
1479{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001480 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001481}
1482
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001483void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001484 mbedtls_x509_crt *ca_chain,
1485 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001487 conf->ca_chain = ca_chain;
1488 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001489
1490#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1491 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1492 * cannot be used together. */
1493 conf->f_ca_cb = NULL;
1494 conf->p_ca_cb = NULL;
1495#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001496}
Hanno Becker5adaad92019-03-27 16:54:37 +00001497
1498#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1499void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001500 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001501 void *p_ca_cb )
1502{
1503 conf->f_ca_cb = f_ca_cb;
1504 conf->p_ca_cb = p_ca_cb;
1505
1506 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1507 * cannot be used together. */
1508 conf->ca_chain = NULL;
1509 conf->ca_crl = NULL;
1510}
1511#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001513
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001514#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001515const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1516 size_t *name_len )
1517{
1518 *name_len = ssl->handshake->sni_name_len;
1519 return( ssl->handshake->sni_name );
1520}
1521
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001522int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1523 mbedtls_x509_crt *own_cert,
1524 mbedtls_pk_context *pk_key )
1525{
1526 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1527 own_cert, pk_key ) );
1528}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001529
1530void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1531 mbedtls_x509_crt *ca_chain,
1532 mbedtls_x509_crl *ca_crl )
1533{
1534 ssl->handshake->sni_ca_chain = ca_chain;
1535 ssl->handshake->sni_ca_crl = ca_crl;
1536}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001537
Glenn Strauss999ef702022-03-11 01:37:23 -05001538#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1539void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1540 const mbedtls_x509_crt *crt)
1541{
1542 ssl->handshake->dn_hints = crt;
1543}
1544#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1545
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001546void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1547 int authmode )
1548{
1549 ssl->handshake->sni_authmode = authmode;
1550}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001551#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1552
Hanno Becker8927c832019-04-03 12:52:50 +01001553#if defined(MBEDTLS_X509_CRT_PARSE_C)
1554void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1555 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1556 void *p_vrfy )
1557{
1558 ssl->f_vrfy = f_vrfy;
1559 ssl->p_vrfy = p_vrfy;
1560}
1561#endif
1562
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001563#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001564/*
1565 * Set EC J-PAKE password for current handshake
1566 */
1567int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1568 const unsigned char *pw,
1569 size_t pw_len )
1570{
1571 mbedtls_ecjpake_role role;
1572
Janos Follath8eb64132016-06-03 15:40:57 +01001573 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1575
1576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1577 role = MBEDTLS_ECJPAKE_SERVER;
1578 else
1579 role = MBEDTLS_ECJPAKE_CLIENT;
1580
1581 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1582 role,
1583 MBEDTLS_MD_SHA256,
1584 MBEDTLS_ECP_DP_SECP256R1,
1585 pw, pw_len ) );
1586}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001587#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001588
Gilles Peskineeccd8882020-03-10 12:19:08 +01001589#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001590
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001591MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001592static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1593{
1594#if defined(MBEDTLS_USE_PSA_CRYPTO)
1595 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1596 return( 1 );
1597#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001598 if( conf->psk != NULL )
1599 return( 1 );
1600
1601 return( 0 );
1602}
1603
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001604static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1605{
1606 /* Remove reference to existing PSK, if any. */
1607#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001608 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001609 {
1610 /* The maintenance of the PSK key slot is the
1611 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001612 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001613 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001614#endif /* MBEDTLS_USE_PSA_CRYPTO */
1615 if( conf->psk != NULL )
1616 {
1617 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1618
1619 mbedtls_free( conf->psk );
1620 conf->psk = NULL;
1621 conf->psk_len = 0;
1622 }
1623
1624 /* Remove reference to PSK identity, if any. */
1625 if( conf->psk_identity != NULL )
1626 {
1627 mbedtls_free( conf->psk_identity );
1628 conf->psk_identity = NULL;
1629 conf->psk_identity_len = 0;
1630 }
1631}
1632
Hanno Becker7390c712018-11-15 13:33:04 +00001633/* This function assumes that PSK identity in the SSL config is unset.
1634 * It checks that the provided identity is well-formed and attempts
1635 * to make a copy of it in the SSL config.
1636 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001637MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001638static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1639 unsigned char const *psk_identity,
1640 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001641{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001642 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001643 if( psk_identity == NULL ||
1644 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001645 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001646 {
1647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1648 }
1649
Hanno Becker7390c712018-11-15 13:33:04 +00001650 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1651 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001652 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001653
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001654 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001655 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001656
1657 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001658}
1659
Hanno Becker7390c712018-11-15 13:33:04 +00001660int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1661 const unsigned char *psk, size_t psk_len,
1662 const unsigned char *psk_identity, size_t psk_identity_len )
1663{
Janos Follath865b3eb2019-12-16 11:46:15 +00001664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001665
1666 /* We currently only support one PSK, raw or opaque. */
1667 if( ssl_conf_psk_is_configured( conf ) )
1668 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001669
1670 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001671 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001673 if( psk_len == 0 )
1674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1675 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1677
Hanno Becker7390c712018-11-15 13:33:04 +00001678 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1679 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1680 conf->psk_len = psk_len;
1681 memcpy( conf->psk, psk, conf->psk_len );
1682
1683 /* Check and set PSK Identity */
1684 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1685 if( ret != 0 )
1686 ssl_conf_remove_psk( conf );
1687
1688 return( ret );
1689}
1690
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001691static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1692{
1693#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001694 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001695 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001696 /* The maintenance of the external PSK key slot is the
1697 * user's responsibility. */
1698 if( ssl->handshake->psk_opaque_is_internal )
1699 {
1700 psa_destroy_key( ssl->handshake->psk_opaque );
1701 ssl->handshake->psk_opaque_is_internal = 0;
1702 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001703 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001704 }
Neil Armstronge952a302022-05-03 10:22:14 +02001705#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001706 if( ssl->handshake->psk != NULL )
1707 {
1708 mbedtls_platform_zeroize( ssl->handshake->psk,
1709 ssl->handshake->psk_len );
1710 mbedtls_free( ssl->handshake->psk );
1711 ssl->handshake->psk_len = 0;
1712 }
Neil Armstronge952a302022-05-03 10:22:14 +02001713#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001714}
1715
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001716int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1717 const unsigned char *psk, size_t psk_len )
1718{
Neil Armstrong501c9322022-05-03 09:35:09 +02001719#if defined(MBEDTLS_USE_PSA_CRYPTO)
1720 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001722 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001723 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001724#endif /* MBEDTLS_USE_PSA_CRYPTO */
1725
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001726 if( psk == NULL || ssl->handshake == NULL )
1727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1728
1729 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1731
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001732 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001733
Neil Armstrong501c9322022-05-03 09:35:09 +02001734#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1736 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1737 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001738 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001739 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1740 else
1741 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1742 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1743 }
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001745
Jerry Yu568ec252022-07-22 21:27:34 +08001746#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001747 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1748 {
1749 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1750 psa_set_key_usage_flags( &key_attributes,
1751 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1752 }
1753#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1754
Neil Armstrong501c9322022-05-03 09:35:09 +02001755 psa_set_key_algorithm( &key_attributes, alg );
1756 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1757
1758 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1759 if( status != PSA_SUCCESS )
1760 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1761
1762 /* Allow calling psa_destroy_key() on psk remove */
1763 ssl->handshake->psk_opaque_is_internal = 1;
1764 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1765#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001766 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001767 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001768
1769 ssl->handshake->psk_len = psk_len;
1770 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1771
1772 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001773#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001774}
1775
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001776#if defined(MBEDTLS_USE_PSA_CRYPTO)
1777int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001778 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001779 const unsigned char *psk_identity,
1780 size_t psk_identity_len )
1781{
Janos Follath865b3eb2019-12-16 11:46:15 +00001782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001783
1784 /* We currently only support one PSK, raw or opaque. */
1785 if( ssl_conf_psk_is_configured( conf ) )
1786 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001787
Hanno Becker7390c712018-11-15 13:33:04 +00001788 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001789 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001791 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001792
1793 /* Check and set PSK Identity */
1794 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1795 psk_identity_len );
1796 if( ret != 0 )
1797 ssl_conf_remove_psk( conf );
1798
1799 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001800}
1801
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001802int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1803 mbedtls_svc_key_id_t psk )
1804{
1805 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1806 ( ssl->handshake == NULL ) )
1807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1808
1809 ssl_remove_psk( ssl );
1810 ssl->handshake->psk_opaque = psk;
1811 return( 0 );
1812}
1813#endif /* MBEDTLS_USE_PSA_CRYPTO */
1814
Jerry Yu8897c072022-08-12 13:56:53 +08001815#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001816void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1817 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1818 size_t),
1819 void *p_psk )
1820{
1821 conf->f_psk = f_psk;
1822 conf->p_psk = p_psk;
1823}
Jerry Yu8897c072022-08-12 13:56:53 +08001824#endif /* MBEDTLS_SSL_SRV_C */
1825
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001826#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1827
1828#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001829static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1830 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001831{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001832#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001833 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001834 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001835#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001836 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001837 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001838 return( MBEDTLS_SSL_MODE_STREAM );
1839}
1840
1841#else /* MBEDTLS_USE_PSA_CRYPTO */
1842
1843static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1844 mbedtls_cipher_mode_t mode )
1845{
1846#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1847 if( mode == MBEDTLS_MODE_CBC )
1848 return( MBEDTLS_SSL_MODE_CBC );
1849#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1850
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001851#if defined(MBEDTLS_GCM_C) || \
1852 defined(MBEDTLS_CCM_C) || \
1853 defined(MBEDTLS_CHACHAPOLY_C)
1854 if( mode == MBEDTLS_MODE_GCM ||
1855 mode == MBEDTLS_MODE_CCM ||
1856 mode == MBEDTLS_MODE_CHACHAPOLY )
1857 return( MBEDTLS_SSL_MODE_AEAD );
1858#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001859
1860 return( MBEDTLS_SSL_MODE_STREAM );
1861}
Gilles Peskine301711e2022-04-26 16:57:05 +02001862#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001863
Gilles Peskinee108d982022-04-26 16:50:40 +02001864static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1865 mbedtls_ssl_mode_t base_mode,
1866 int encrypt_then_mac )
1867{
1868#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1869 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1870 base_mode == MBEDTLS_SSL_MODE_CBC )
1871 {
1872 return( MBEDTLS_SSL_MODE_CBC_ETM );
1873 }
1874#else
1875 (void) encrypt_then_mac;
1876#endif
1877 return( base_mode );
1878}
1879
Neil Armstrongab555e02022-04-04 11:07:59 +02001880mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001881 const mbedtls_ssl_transform *transform )
1882{
Gilles Peskinee108d982022-04-26 16:50:40 +02001883 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001884#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001885 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001886#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001887 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1888#endif
1889 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001890
Gilles Peskinee108d982022-04-26 16:50:40 +02001891 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001892#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001893 encrypt_then_mac = transform->encrypt_then_mac;
1894#endif
1895 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001896}
1897
Neil Armstrongab555e02022-04-04 11:07:59 +02001898mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001899#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001901#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001902 const mbedtls_ssl_ciphersuite_t *suite )
1903{
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1905
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001906#if defined(MBEDTLS_USE_PSA_CRYPTO)
1907 psa_status_t status;
1908 psa_algorithm_t alg;
1909 psa_key_type_t type;
1910 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1912 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001913 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001914#else
1915 const mbedtls_cipher_info_t *cipher =
1916 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001918 {
1919 base_mode =
1920 mbedtls_ssl_get_base_mode(
1921 mbedtls_cipher_info_get_mode( cipher ) );
1922 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001923#endif /* MBEDTLS_USE_PSA_CRYPTO */
1924
Gilles Peskinee108d982022-04-26 16:50:40 +02001925#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1926 int encrypt_then_mac = 0;
1927#endif
1928 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001929}
1930
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001931#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001932
1933#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001934/* Serialization of TLS 1.3 sessions:
1935 *
1936 * struct {
1937 * uint64 ticket_received;
1938 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001939 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001940 * } ClientOnlyData;
1941 *
1942 * struct {
1943 * uint8 endpoint;
1944 * uint8 ciphersuite[2];
1945 * uint32 ticket_age_add;
1946 * uint8 ticket_flags;
1947 * opaque resumption_key<0..255>;
1948 * select ( endpoint ) {
1949 * case client: ClientOnlyData;
1950 * case server: uint64 start_time;
1951 * };
1952 * } serialized_session_tls13;
1953 *
1954 */
1955#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001956MBEDTLS_CHECK_RETURN_CRITICAL
1957static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1958 unsigned char *buf,
1959 size_t buf_len,
1960 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001961{
1962 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001963 size_t needed = 1 /* endpoint */
1964 + 2 /* ciphersuite */
1965 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001966 + 1 /* ticket_flags */
1967 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001968 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001969
1970 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1972 needed += session->resumption_key_len; /* resumption_key */
1973
Jerry Yu438ddd82022-07-07 06:55:50 +00001974#if defined(MBEDTLS_HAVE_TIME)
1975 needed += 8; /* start_time or ticket_received */
1976#endif
1977
1978#if defined(MBEDTLS_SSL_CLI_C)
1979 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1980 {
1981 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001982 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08001983
1984 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08001985 if( session->ticket_len > SIZE_MAX - needed )
1986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08001987
Jerry Yue28d9742022-08-18 15:44:03 +08001988 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00001989 }
1990#endif /* MBEDTLS_SSL_CLI_C */
1991
Jerry Yue36fdd62022-08-17 21:31:36 +08001992 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00001993 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08001994 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00001995
1996 p[0] = session->endpoint;
1997 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
1998 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
1999 p[7] = session->ticket_flags;
2000
2001 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002002 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002003 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002004 memcpy( p, session->resumption_key, session->resumption_key_len );
2005 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002006
2007#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2008 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2009 {
2010 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2011 p += 8;
2012 }
2013#endif /* MBEDTLS_HAVE_TIME */
2014
2015#if defined(MBEDTLS_SSL_CLI_C)
2016 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2017 {
2018#if defined(MBEDTLS_HAVE_TIME)
2019 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2020 p += 8;
2021#endif
2022 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2023 p += 4;
2024
2025 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2026 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002027
2028 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002029 {
2030 memcpy( p, session->ticket, session->ticket_len );
2031 p += session->ticket_len;
2032 }
2033 }
2034#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002035 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002036}
2037
2038MBEDTLS_CHECK_RETURN_CRITICAL
2039static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2040 const unsigned char *buf,
2041 size_t len )
2042{
2043 const unsigned char *p = buf;
2044 const unsigned char *end = buf + len;
2045
2046 if( end - p < 9 )
2047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2048 session->endpoint = p[0];
2049 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2050 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2051 session->ticket_flags = p[7];
2052
2053 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002054 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002055 p += 9;
2056
Jerry Yubc7c1a42022-07-21 22:57:37 +08002057 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2059
Jerry Yubc7c1a42022-07-21 22:57:37 +08002060 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002062 memcpy( session->resumption_key, p, session->resumption_key_len );
2063 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002064
2065#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2066 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2067 {
2068 if( end - p < 8 )
2069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2070 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2071 p += 8;
2072 }
2073#endif /* MBEDTLS_HAVE_TIME */
2074
2075#if defined(MBEDTLS_SSL_CLI_C)
2076 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2077 {
2078#if defined(MBEDTLS_HAVE_TIME)
2079 if( end - p < 8 )
2080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2081 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2082 p += 8;
2083#endif
2084 if( end - p < 4 )
2085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2086 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2087 p += 4;
2088
2089 if( end - p < 2 )
2090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2091 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2092 p += 2;
2093
2094 if( end - p < ( long int )session->ticket_len )
2095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2096 if( session->ticket_len > 0 )
2097 {
2098 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2099 if( session->ticket == NULL )
2100 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2101 memcpy( session->ticket, p, session->ticket_len );
2102 p += session->ticket_len;
2103 }
2104 }
2105#endif /* MBEDTLS_SSL_CLI_C */
2106
2107 return( 0 );
2108
2109}
2110#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002111MBEDTLS_CHECK_RETURN_CRITICAL
2112static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2113 unsigned char *buf,
2114 size_t buf_len,
2115 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002116{
2117 ((void) session);
2118 ((void) buf);
2119 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002120 *olen = 0;
2121 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002122}
Jerry Yu438ddd82022-07-07 06:55:50 +00002123
2124static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2125 unsigned char *buf,
2126 size_t buf_len )
2127{
2128 ((void) session);
2129 ((void) buf);
2130 ((void) buf_len);
2131 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2132}
2133#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002134#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2135
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002136psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002137 size_t taglen,
2138 psa_algorithm_t *alg,
2139 psa_key_type_t *key_type,
2140 size_t *key_size )
2141{
2142 switch ( mbedtls_cipher_type )
2143 {
2144 case MBEDTLS_CIPHER_AES_128_CBC:
2145 *alg = PSA_ALG_CBC_NO_PADDING;
2146 *key_type = PSA_KEY_TYPE_AES;
2147 *key_size = 128;
2148 break;
2149 case MBEDTLS_CIPHER_AES_128_CCM:
2150 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2151 *key_type = PSA_KEY_TYPE_AES;
2152 *key_size = 128;
2153 break;
2154 case MBEDTLS_CIPHER_AES_128_GCM:
2155 *alg = PSA_ALG_GCM;
2156 *key_type = PSA_KEY_TYPE_AES;
2157 *key_size = 128;
2158 break;
2159 case MBEDTLS_CIPHER_AES_192_CCM:
2160 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2161 *key_type = PSA_KEY_TYPE_AES;
2162 *key_size = 192;
2163 break;
2164 case MBEDTLS_CIPHER_AES_192_GCM:
2165 *alg = PSA_ALG_GCM;
2166 *key_type = PSA_KEY_TYPE_AES;
2167 *key_size = 192;
2168 break;
2169 case MBEDTLS_CIPHER_AES_256_CBC:
2170 *alg = PSA_ALG_CBC_NO_PADDING;
2171 *key_type = PSA_KEY_TYPE_AES;
2172 *key_size = 256;
2173 break;
2174 case MBEDTLS_CIPHER_AES_256_CCM:
2175 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2176 *key_type = PSA_KEY_TYPE_AES;
2177 *key_size = 256;
2178 break;
2179 case MBEDTLS_CIPHER_AES_256_GCM:
2180 *alg = PSA_ALG_GCM;
2181 *key_type = PSA_KEY_TYPE_AES;
2182 *key_size = 256;
2183 break;
2184 case MBEDTLS_CIPHER_ARIA_128_CBC:
2185 *alg = PSA_ALG_CBC_NO_PADDING;
2186 *key_type = PSA_KEY_TYPE_ARIA;
2187 *key_size = 128;
2188 break;
2189 case MBEDTLS_CIPHER_ARIA_128_CCM:
2190 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2191 *key_type = PSA_KEY_TYPE_ARIA;
2192 *key_size = 128;
2193 break;
2194 case MBEDTLS_CIPHER_ARIA_128_GCM:
2195 *alg = PSA_ALG_GCM;
2196 *key_type = PSA_KEY_TYPE_ARIA;
2197 *key_size = 128;
2198 break;
2199 case MBEDTLS_CIPHER_ARIA_192_CCM:
2200 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2201 *key_type = PSA_KEY_TYPE_ARIA;
2202 *key_size = 192;
2203 break;
2204 case MBEDTLS_CIPHER_ARIA_192_GCM:
2205 *alg = PSA_ALG_GCM;
2206 *key_type = PSA_KEY_TYPE_ARIA;
2207 *key_size = 192;
2208 break;
2209 case MBEDTLS_CIPHER_ARIA_256_CBC:
2210 *alg = PSA_ALG_CBC_NO_PADDING;
2211 *key_type = PSA_KEY_TYPE_ARIA;
2212 *key_size = 256;
2213 break;
2214 case MBEDTLS_CIPHER_ARIA_256_CCM:
2215 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2216 *key_type = PSA_KEY_TYPE_ARIA;
2217 *key_size = 256;
2218 break;
2219 case MBEDTLS_CIPHER_ARIA_256_GCM:
2220 *alg = PSA_ALG_GCM;
2221 *key_type = PSA_KEY_TYPE_ARIA;
2222 *key_size = 256;
2223 break;
2224 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2225 *alg = PSA_ALG_CBC_NO_PADDING;
2226 *key_type = PSA_KEY_TYPE_CAMELLIA;
2227 *key_size = 128;
2228 break;
2229 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2230 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2231 *key_type = PSA_KEY_TYPE_CAMELLIA;
2232 *key_size = 128;
2233 break;
2234 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2235 *alg = PSA_ALG_GCM;
2236 *key_type = PSA_KEY_TYPE_CAMELLIA;
2237 *key_size = 128;
2238 break;
2239 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2240 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2241 *key_type = PSA_KEY_TYPE_CAMELLIA;
2242 *key_size = 192;
2243 break;
2244 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2245 *alg = PSA_ALG_GCM;
2246 *key_type = PSA_KEY_TYPE_CAMELLIA;
2247 *key_size = 192;
2248 break;
2249 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2250 *alg = PSA_ALG_CBC_NO_PADDING;
2251 *key_type = PSA_KEY_TYPE_CAMELLIA;
2252 *key_size = 256;
2253 break;
2254 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2255 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2256 *key_type = PSA_KEY_TYPE_CAMELLIA;
2257 *key_size = 256;
2258 break;
2259 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2260 *alg = PSA_ALG_GCM;
2261 *key_type = PSA_KEY_TYPE_CAMELLIA;
2262 *key_size = 256;
2263 break;
2264 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2265 *alg = PSA_ALG_CHACHA20_POLY1305;
2266 *key_type = PSA_KEY_TYPE_CHACHA20;
2267 *key_size = 256;
2268 break;
2269 case MBEDTLS_CIPHER_NULL:
2270 *alg = MBEDTLS_SSL_NULL_CIPHER;
2271 *key_type = 0;
2272 *key_size = 0;
2273 break;
2274 default:
2275 return PSA_ERROR_NOT_SUPPORTED;
2276 }
2277
2278 return PSA_SUCCESS;
2279}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002280#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002281
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002282#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002283int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2284 const unsigned char *dhm_P, size_t P_len,
2285 const unsigned char *dhm_G, size_t G_len )
2286{
Janos Follath865b3eb2019-12-16 11:46:15 +00002287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002288
Glenn Strausscee11292021-12-20 01:43:17 -05002289 mbedtls_mpi_free( &conf->dhm_P );
2290 mbedtls_mpi_free( &conf->dhm_G );
2291
Hanno Beckera90658f2017-10-04 15:29:08 +01002292 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2293 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2294 {
2295 mbedtls_mpi_free( &conf->dhm_P );
2296 mbedtls_mpi_free( &conf->dhm_G );
2297 return( ret );
2298 }
2299
2300 return( 0 );
2301}
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002303int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002304{
Janos Follath865b3eb2019-12-16 11:46:15 +00002305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002306
Glenn Strausscee11292021-12-20 01:43:17 -05002307 mbedtls_mpi_free( &conf->dhm_P );
2308 mbedtls_mpi_free( &conf->dhm_G );
2309
Gilles Peskinee5702482021-06-11 21:59:08 +02002310 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2311 &conf->dhm_P ) ) != 0 ||
2312 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2313 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002314 {
2315 mbedtls_mpi_free( &conf->dhm_P );
2316 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002317 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002318 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002319
2320 return( 0 );
2321}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002322#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002323
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002324#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2325/*
2326 * Set the minimum length for Diffie-Hellman parameters
2327 */
2328void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2329 unsigned int bitlen )
2330{
2331 conf->dhm_min_bitlen = bitlen;
2332}
2333#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2334
Gilles Peskineeccd8882020-03-10 12:19:08 +01002335#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002336#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002337/*
2338 * Set allowed/preferred hashes for handshake signatures
2339 */
2340void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2341 const int *hashes )
2342{
2343 conf->sig_hashes = hashes;
2344}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002345#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002346
Jerry Yuf017ee42022-01-12 15:49:48 +08002347/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002348void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2349 const uint16_t* sig_algs )
2350{
Jerry Yuf017ee42022-01-12 15:49:48 +08002351#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2352 conf->sig_hashes = NULL;
2353#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2354 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002355}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002356#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002357
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002358#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002359#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002360/*
2361 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002362 *
2363 * mbedtls_ssl_setup() takes the provided list
2364 * and translates it to a list of IANA TLS group identifiers,
2365 * stored in ssl->handshake->group_list.
2366 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002367 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002368void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002369 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002371 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002372 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002373}
Brett Warrene0edc842021-08-17 09:53:13 +01002374#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002375#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002376
Brett Warrene0edc842021-08-17 09:53:13 +01002377/*
2378 * Set the allowed groups
2379 */
2380void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2381 const uint16_t *group_list )
2382{
2383#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2384 conf->curve_list = NULL;
2385#endif
2386 conf->group_list = group_list;
2387}
2388
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002389#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391{
Hanno Becker947194e2017-04-07 13:25:49 +01002392 /* Initialize to suppress unnecessary compiler warning */
2393 size_t hostname_len = 0;
2394
2395 /* Check if new hostname is valid before
2396 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002397 if( hostname != NULL )
2398 {
2399 hostname_len = strlen( hostname );
2400
2401 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2403 }
2404
2405 /* Now it's clear that we will overwrite the old hostname,
2406 * so we can free it safely */
2407
2408 if( ssl->hostname != NULL )
2409 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002410 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002411 mbedtls_free( ssl->hostname );
2412 }
2413
2414 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002415
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002417 {
2418 ssl->hostname = NULL;
2419 }
2420 else
2421 {
2422 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002423 if( ssl->hostname == NULL )
2424 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002425
Hanno Becker947194e2017-04-07 13:25:49 +01002426 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002427
Hanno Becker947194e2017-04-07 13:25:49 +01002428 ssl->hostname[hostname_len] = '\0';
2429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
2431 return( 0 );
2432}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002433#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002435#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002436void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002438 const unsigned char *, size_t),
2439 void *p_sni )
2440{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002441 conf->f_sni = f_sni;
2442 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002447int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002448{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002449 size_t cur_len, tot_len;
2450 const char **p;
2451
2452 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002453 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2454 * MUST NOT be truncated."
2455 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002456 */
2457 tot_len = 0;
2458 for( p = protos; *p != NULL; p++ )
2459 {
2460 cur_len = strlen( *p );
2461 tot_len += cur_len;
2462
Ronald Cron8216dd32020-04-23 16:41:44 +02002463 if( ( cur_len == 0 ) ||
2464 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2465 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002467 }
2468
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002469 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002470
2471 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002472}
2473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002475{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002476 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002479
Johan Pascalb62bb512015-12-03 21:56:45 +01002480#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002481void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2482 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002483{
2484 conf->dtls_srtp_mki_support = support_mki_value;
2485}
2486
Ron Eldoref72faf2018-07-12 11:54:20 +03002487int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2488 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002489 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002490{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002491 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002492 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002494 }
Ron Eldor591f1622018-01-22 12:30:04 +02002495
2496 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002497 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002498 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002499 }
Ron Eldor591f1622018-01-22 12:30:04 +02002500
2501 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2502 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002503 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002504}
2505
Ron Eldoref72faf2018-07-12 11:54:20 +03002506int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002507 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002508{
Johan Pascal253d0262020-09-22 13:04:45 +02002509 const mbedtls_ssl_srtp_profile *p;
2510 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002511
Johan Pascal253d0262020-09-22 13:04:45 +02002512 /* check the profiles list: all entry must be valid,
2513 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002514 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2515 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2516 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002517 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002518 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002519 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002520 list_size++;
2521 }
2522 else
2523 {
2524 /* unsupported value, stop parsing and set the size to an error value */
2525 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002526 }
2527 }
2528
Johan Pascal5ef72d22020-10-28 17:05:47 +01002529 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002530 {
Johan Pascal253d0262020-09-22 13:04:45 +02002531 conf->dtls_srtp_profile_list = NULL;
2532 conf->dtls_srtp_profile_list_len = 0;
2533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2534 }
2535
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002536 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002537 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002538
2539 return( 0 );
2540}
2541
Johan Pascal5ef72d22020-10-28 17:05:47 +01002542void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2543 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002544{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002545 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2546 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002547 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002548 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002549 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002550 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002551 else
2552 {
2553 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002554 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2555 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002556 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002557}
Johan Pascalb62bb512015-12-03 21:56:45 +01002558#endif /* MBEDTLS_SSL_DTLS_SRTP */
2559
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302560#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002561void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002562{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002563 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002564}
2565
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002566void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002567{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002568 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002569}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302570#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002571
Janos Follath088ce432017-04-10 12:42:31 +01002572#if defined(MBEDTLS_SSL_SRV_C)
2573void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2574 char cert_req_ca_list )
2575{
2576 conf->cert_req_ca_list = cert_req_ca_list;
2577}
2578#endif
2579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002581void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002583 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002584}
2585#endif
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002588void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002590 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002591}
2592#endif
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002595int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002598 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002601 }
2602
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002603 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002604
2605 return( 0 );
2606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002608
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002609void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002610{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002611 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002612}
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002615void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002617 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002618}
2619
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002620void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002622 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002623}
2624
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002625void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002626 const unsigned char period[8] )
2627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002628 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002633#if defined(MBEDTLS_SSL_CLI_C)
2634void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002635{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002636 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002637}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002638#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002639
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002640#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002641
Jerry Yud0766ec2022-09-22 10:46:57 +08002642#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002643void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2644 uint16_t num_tickets )
2645{
Jerry Yud0766ec2022-09-22 10:46:57 +08002646 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002647}
2648#endif
2649
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002650void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2651 mbedtls_ssl_ticket_write_t *f_ticket_write,
2652 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2653 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002654{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002655 conf->f_ticket_write = f_ticket_write;
2656 conf->f_ticket_parse = f_ticket_parse;
2657 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002658}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002659#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002661
Hanno Becker7e6c1782021-06-08 09:24:55 +01002662void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2663 mbedtls_ssl_export_keys_t *f_export_keys,
2664 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002665{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002666 ssl->f_export_keys = f_export_keys;
2667 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002668}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002669
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002670#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002671void mbedtls_ssl_conf_async_private_cb(
2672 mbedtls_ssl_config *conf,
2673 mbedtls_ssl_async_sign_t *f_async_sign,
2674 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2675 mbedtls_ssl_async_resume_t *f_async_resume,
2676 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002677 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002678{
2679 conf->f_async_sign_start = f_async_sign;
2680 conf->f_async_decrypt_start = f_async_decrypt;
2681 conf->f_async_resume = f_async_resume;
2682 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002683 conf->p_async_config_data = async_config_data;
2684}
2685
Gilles Peskine8f97af72018-04-26 11:46:10 +02002686void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2687{
2688 return( conf->p_async_config_data );
2689}
2690
Gilles Peskine1febfef2018-04-30 11:54:39 +02002691void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002692{
2693 if( ssl->handshake == NULL )
2694 return( NULL );
2695 else
2696 return( ssl->handshake->user_async_ctx );
2697}
2698
Gilles Peskine1febfef2018-04-30 11:54:39 +02002699void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002700 void *ctx )
2701{
2702 if( ssl->handshake != NULL )
2703 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002704}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002705#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002706
Paul Bakker5121ce52009-01-03 21:22:43 +00002707/*
2708 * SSL get accessors
2709 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002710uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002712 if( ssl->session != NULL )
2713 return( ssl->session->verify_result );
2714
2715 if( ssl->session_negotiate != NULL )
2716 return( ssl->session_negotiate->verify_result );
2717
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002718 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719}
2720
Glenn Strauss8f526902022-01-13 00:04:49 -05002721int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2722{
2723 if( ssl == NULL || ssl->session == NULL )
2724 return( 0 );
2725
2726 return( ssl->session->ciphersuite );
2727}
2728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002730{
Paul Bakker926c8e42013-03-06 10:23:34 +01002731 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002732 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002735}
2736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002740 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002741 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002742 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002743 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002744 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002745 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002746 default:
2747 return( "unknown (DTLS)" );
2748 }
2749 }
2750#endif
2751
Glenn Strauss60bfe602022-03-14 19:04:24 -04002752 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002753 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002754 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002755 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002756 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002757 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002758 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002759 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002760 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002761}
2762
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002763#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002764size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2765{
David Horstmann95d516f2021-05-04 18:36:56 +01002766 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002767 size_t read_mfl;
2768
2769 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2770 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2771 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2772 {
2773 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2774 }
2775
2776 /* Check if a smaller max length was negotiated */
2777 if( ssl->session_out != NULL )
2778 {
2779 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2780 if( read_mfl < max_len )
2781 {
2782 max_len = read_mfl;
2783 }
2784 }
2785
2786 // During a handshake, use the value being negotiated
2787 if( ssl->session_negotiate != NULL )
2788 {
2789 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2790 if( read_mfl < max_len )
2791 {
2792 max_len = read_mfl;
2793 }
2794 }
2795
2796 return( max_len );
2797}
2798
2799size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002800{
2801 size_t max_len;
2802
2803 /*
2804 * Assume mfl_code is correct since it was checked when set
2805 */
Angus Grattond8213d02016-05-25 20:56:48 +10002806 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002807
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002808 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002809 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002810 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002811 {
Angus Grattond8213d02016-05-25 20:56:48 +10002812 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002813 }
2814
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002815 /* During a handshake, use the value being negotiated */
2816 if( ssl->session_negotiate != NULL &&
2817 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2818 {
2819 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2820 }
2821
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002822 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002823}
2824#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2825
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002827size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002828{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002829 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2830 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2831 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2832 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2833 return ( 0 );
2834
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002835 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2836 return( ssl->mtu );
2837
2838 if( ssl->mtu == 0 )
2839 return( ssl->handshake->mtu );
2840
2841 return( ssl->mtu < ssl->handshake->mtu ?
2842 ssl->mtu : ssl->handshake->mtu );
2843}
2844#endif /* MBEDTLS_SSL_PROTO_DTLS */
2845
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002846int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2847{
2848 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2849
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002850#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2851 !defined(MBEDTLS_SSL_PROTO_DTLS)
2852 (void) ssl;
2853#endif
2854
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002855#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002856 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002857
2858 if( max_len > mfl )
2859 max_len = mfl;
2860#endif
2861
2862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002863 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002864 {
Hanno Becker89490712020-02-05 10:50:12 +00002865 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002866 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2867 const size_t overhead = (size_t) ret;
2868
2869 if( ret < 0 )
2870 return( ret );
2871
2872 if( mtu <= overhead )
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2875 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2876 }
2877
2878 if( max_len > mtu - overhead )
2879 max_len = mtu - overhead;
2880 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002881#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002882
Hanno Becker0defedb2018-08-10 12:35:02 +01002883#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2884 !defined(MBEDTLS_SSL_PROTO_DTLS)
2885 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002886#endif
2887
2888 return( (int) max_len );
2889}
2890
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002891int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2892{
2893 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2894
2895#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2896 (void) ssl;
2897#endif
2898
2899#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2900 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2901
2902 if( max_len > mfl )
2903 max_len = mfl;
2904#endif
2905
2906 return( (int) max_len );
2907}
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_X509_CRT_PARSE_C)
2910const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002911{
2912 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002913 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002914
Hanno Beckere6824572019-02-07 13:18:46 +00002915#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002916 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002917#else
2918 return( NULL );
2919#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002924int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2925 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002926{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002927 int ret;
2928
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002929 if( ssl == NULL ||
2930 dst == NULL ||
2931 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002932 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002935 }
2936
Hanno Beckere810bbc2021-05-14 16:01:05 +01002937 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2938 * idempotent: Each session can only be exported once.
2939 *
2940 * (This is in preparation for TLS 1.3 support where we will
2941 * need the ability to export multiple sessions (aka tickets),
2942 * which will be achieved by calling mbedtls_ssl_get_session()
2943 * multiple times until it fails.)
2944 *
2945 * Check whether we have already exported the current session,
2946 * and fail if so.
2947 */
2948 if( ssl->session->exported == 1 )
2949 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2950
2951 ret = mbedtls_ssl_session_copy( dst, ssl->session );
2952 if( ret != 0 )
2953 return( ret );
2954
2955 /* Remember that we've exported the session. */
2956 ssl->session->exported = 1;
2957 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002960
Paul Bakker5121ce52009-01-03 21:22:43 +00002961/*
Hanno Beckera835da52019-05-16 12:39:07 +01002962 * Define ticket header determining Mbed TLS version
2963 * and structure of the ticket.
2964 */
2965
Hanno Becker94ef3b32019-05-16 12:50:45 +01002966/*
Hanno Becker50b59662019-05-28 14:30:45 +01002967 * Define bitflag determining compile-time settings influencing
2968 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01002969 */
2970
Hanno Becker50b59662019-05-28 14:30:45 +01002971#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01002972#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01002973#else
Hanno Becker3e088662019-05-29 11:10:18 +01002974#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002975#endif /* MBEDTLS_HAVE_TIME */
2976
2977#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01002978#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002979#else
Hanno Becker3e088662019-05-29 11:10:18 +01002980#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002981#endif /* MBEDTLS_X509_CRT_PARSE_C */
2982
2983#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01002984#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002985#else
Hanno Becker3e088662019-05-29 11:10:18 +01002986#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002987#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
2988
2989#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01002990#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002991#else
Hanno Becker3e088662019-05-29 11:10:18 +01002992#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002993#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2994
Hanno Becker94ef3b32019-05-16 12:50:45 +01002995#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01002996#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01002997#else
Hanno Becker3e088662019-05-29 11:10:18 +01002998#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01002999#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3000
Hanno Becker94ef3b32019-05-16 12:50:45 +01003001#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3002#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3003#else
3004#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3005#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3006
Hanno Becker3e088662019-05-29 11:10:18 +01003007#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3008#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3009#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3010#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003011#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3012#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003013
Hanno Becker50b59662019-05-28 14:30:45 +01003014#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003015 ( (uint16_t) ( \
3016 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3017 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3018 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3019 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003020 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003021 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003022
Hanno Beckerf8787072019-05-16 12:41:07 +01003023static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003024 MBEDTLS_VERSION_MAJOR,
3025 MBEDTLS_VERSION_MINOR,
3026 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003027 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3028 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003029};
Hanno Beckera835da52019-05-16 12:39:07 +01003030
3031/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003032 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003033 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003034 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003035 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003036 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003037 * opaque mbedtls_version[3]; // library version: major, minor, patch
3038 * opaque session_format[2]; // library-version specific 16-bit field
3039 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003040 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003041 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003042 * Note: When updating the format, remember to keep
3043 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003044 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003045 * // In this version, `session_format` determines
3046 * // the setting of those compile-time
3047 * // configuration options which influence
3048 * // the structure of mbedtls_ssl_session.
3049 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003050 * uint8_t minor_ver; // Protocol minor version. Possible values:
3051 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003052 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003053 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003054 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003055 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003056 * serialized_session_tls12 data;
3057 *
3058 * };
3059 *
3060 * } serialized_session;
3061 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003062 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003063
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003064MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003065static int ssl_session_save( const mbedtls_ssl_session *session,
3066 unsigned char omit_header,
3067 unsigned char *buf,
3068 size_t buf_len,
3069 size_t *olen )
3070{
3071 unsigned char *p = buf;
3072 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003073 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003074#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3075 size_t out_len;
3076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3077#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003078 if( session == NULL )
3079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3080
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003081 if( !omit_header )
3082 {
3083 /*
3084 * Add Mbed TLS version identifier
3085 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003086 used += sizeof( ssl_serialized_session_header );
3087
3088 if( used <= buf_len )
3089 {
3090 memcpy( p, ssl_serialized_session_header,
3091 sizeof( ssl_serialized_session_header ) );
3092 p += sizeof( ssl_serialized_session_header );
3093 }
3094 }
3095
3096 /*
3097 * TLS version identifier
3098 */
3099 used += 1;
3100 if( used <= buf_len )
3101 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003102 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003103 }
3104
3105 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003106 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003107 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003108 {
3109#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003110 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003111 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003112 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003113#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3114
Jerry Yu251a12e2022-07-13 15:15:48 +08003115#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3116 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003117 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3118 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003119 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003120 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003121 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003122#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3123
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003124 default:
3125 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3126 }
3127
3128 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003129 if( used > buf_len )
3130 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003131
3132 return( 0 );
3133}
3134
3135/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003136 * Public wrapper for ssl_session_save()
3137 */
3138int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3139 unsigned char *buf,
3140 size_t buf_len,
3141 size_t *olen )
3142{
3143 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3144}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003145
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003146/*
3147 * Deserialize session, see mbedtls_ssl_session_save() for format.
3148 *
3149 * This internal version is wrapped by a public function that cleans up in
3150 * case of error, and has an extra option omit_header.
3151 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003152MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003153static int ssl_session_load( mbedtls_ssl_session *session,
3154 unsigned char omit_header,
3155 const unsigned char *buf,
3156 size_t len )
3157{
3158 const unsigned char *p = buf;
3159 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003160 size_t remaining_len;
3161
3162
3163 if( session == NULL )
3164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003165
3166 if( !omit_header )
3167 {
3168 /*
3169 * Check Mbed TLS version identifier
3170 */
3171
3172 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3174
3175 if( memcmp( p, ssl_serialized_session_header,
3176 sizeof( ssl_serialized_session_header ) ) != 0 )
3177 {
3178 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3179 }
3180 p += sizeof( ssl_serialized_session_header );
3181 }
3182
3183 /*
3184 * TLS version identifier
3185 */
3186 if( 1 > (size_t)( end - p ) )
3187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003188 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003189
3190 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003191 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003192 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003193 {
3194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003195 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003196 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003197#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3198
Jerry Yu438ddd82022-07-07 06:55:50 +00003199#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3200 case MBEDTLS_SSL_VERSION_TLS1_3:
3201 return( ssl_tls13_session_load( session, p, remaining_len ) );
3202#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3203
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003204 default:
3205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3206 }
3207}
3208
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003209/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003210 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003211 */
3212int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3213 const unsigned char *buf,
3214 size_t len )
3215{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003216 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003217
3218 if( ret != 0 )
3219 mbedtls_ssl_session_free( session );
3220
3221 return( ret );
3222}
3223
3224/*
Paul Bakker1961b702013-01-25 14:49:24 +01003225 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003227MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003228static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3229{
3230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3231
Ronald Cron66dbf912022-02-02 15:33:46 +01003232 /*
3233 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003234 * that were written into the output buffer by the previous handshake step,
3235 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003236 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3237 * We proceed to the next handshake step only when all data from the
3238 * previous one have been sent to the peer, thus we make sure that this is
3239 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3240 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3241 * we have to wait before to go ahead.
3242 * In the case of TLS 1.3, handshake step handlers do not send data to the
3243 * peer. Data are only sent here and through
3244 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003245 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003246 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003247 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3248 return( ret );
3249
3250#if defined(MBEDTLS_SSL_PROTO_DTLS)
3251 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3252 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3253 {
3254 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3255 return( ret );
3256 }
3257#endif /* MBEDTLS_SSL_PROTO_DTLS */
3258
3259 return( ret );
3260}
3261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003263{
Hanno Becker41934dd2021-08-07 19:13:43 +01003264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
Hanno Becker41934dd2021-08-07 19:13:43 +01003266 if( ssl == NULL ||
3267 ssl->conf == NULL ||
3268 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003269 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003270 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003272 }
3273
3274 ret = ssl_prepare_handshake_step( ssl );
3275 if( ret != 0 )
3276 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003277
Jerry Yue7047812021-09-13 19:26:39 +08003278 ret = mbedtls_ssl_handle_pending_alert( ssl );
3279 if( ret != 0 )
3280 goto cleanup;
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003283 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003284 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3286 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003287
Ronald Cron9f0fba32022-02-10 16:45:15 +01003288 switch( ssl->state )
3289 {
3290 case MBEDTLS_SSL_HELLO_REQUEST:
3291 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3292 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003293
Ronald Cron9f0fba32022-02-10 16:45:15 +01003294 case MBEDTLS_SSL_CLIENT_HELLO:
3295 ret = mbedtls_ssl_write_client_hello( ssl );
3296 break;
3297
3298 default:
3299#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003300 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003301 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3302 else
3303 ret = mbedtls_ssl_handshake_client_step( ssl );
3304#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3305 ret = mbedtls_ssl_handshake_client_step( ssl );
3306#else
3307 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3308#endif
3309 }
Jerry Yub9930e72021-08-06 17:11:51 +08003310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003314 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003315#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003316 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003317 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003318#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003319
3320#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3321 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3322 ret = mbedtls_ssl_handshake_server_step( ssl );
3323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003325#endif
3326
Jerry Yue7047812021-09-13 19:26:39 +08003327 if( ret != 0 )
3328 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003329 /* handshake_step return error. And it is same
3330 * with alert_reason.
3331 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003332 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003333 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003334 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003335 goto cleanup;
3336 }
3337 }
3338
3339cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003340 return( ret );
3341}
3342
3343/*
3344 * Perform the SSL handshake
3345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003347{
3348 int ret = 0;
3349
Hanno Beckera817ea42020-10-20 15:20:23 +01003350 /* Sanity checks */
3351
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003352 if( ssl == NULL || ssl->conf == NULL )
3353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3354
Hanno Beckera817ea42020-10-20 15:20:23 +01003355#if defined(MBEDTLS_SSL_PROTO_DTLS)
3356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3357 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3358 {
3359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3360 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3362 }
3363#endif /* MBEDTLS_SSL_PROTO_DTLS */
3364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003366
Hanno Beckera817ea42020-10-20 15:20:23 +01003367 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003368 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003371
3372 if( ret != 0 )
3373 break;
3374 }
3375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
3378 return( ret );
3379}
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381#if defined(MBEDTLS_SSL_RENEGOTIATION)
3382#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003383/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003384 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003385 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003386MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003388{
Janos Follath865b3eb2019-12-16 11:46:15 +00003389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003392
3393 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3395 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003396
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003397 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003398 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003400 return( ret );
3401 }
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003404
3405 return( 0 );
3406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003408
3409/*
3410 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 * - any side: calling mbedtls_ssl_renegotiate(),
3412 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3413 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003414 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003415 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003417 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003418int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003419{
Janos Follath865b3eb2019-12-16 11:46:15 +00003420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003423
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003424 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3425 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003426
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003427 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3428 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003432 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003434 ssl->handshake->out_msg_seq = 1;
3435 else
3436 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003437 }
3438#endif
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3441 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003446 return( ret );
3447 }
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003450
3451 return( 0 );
3452}
3453
3454/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003455 * Renegotiate current connection on client,
3456 * or request renegotiation on server
3457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003461
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003462 if( ssl == NULL || ssl->conf == NULL )
3463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003466 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003468 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003469 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003473
3474 /* Did we already try/start sending HelloRequest? */
3475 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003477
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003478 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003483 /*
3484 * On client, either start the renegotiation process or,
3485 * if already in progress, continue the handshake
3486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003488 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003489 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003491
Hanno Becker40cdaa12020-02-05 10:48:27 +00003492 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003493 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003495 return( ret );
3496 }
3497 }
3498 else
3499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003503 return( ret );
3504 }
3505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003507
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003508 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003511
Gilles Peskine9b562d52018-04-25 20:32:43 +02003512void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003513{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003514 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3515
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003516 if( handshake == NULL )
3517 return;
3518
Brett Warrene0edc842021-08-17 09:53:13 +01003519#if defined(MBEDTLS_ECP_C)
3520#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3521 if ( ssl->handshake->group_list_heap_allocated )
3522 mbedtls_free( (void*) handshake->group_list );
3523 handshake->group_list = NULL;
3524#endif /* MBEDTLS_DEPRECATED_REMOVED */
3525#endif /* MBEDTLS_ECP_C */
3526
Jerry Yuf017ee42022-01-12 15:49:48 +08003527#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3528#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3529 if ( ssl->handshake->sig_algs_heap_allocated )
3530 mbedtls_free( (void*) handshake->sig_algs );
3531 handshake->sig_algs = NULL;
3532#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003533#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3534 if( ssl->handshake->certificate_request_context )
3535 {
3536 mbedtls_free( (void*) handshake->certificate_request_context );
3537 }
3538#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003539#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3540
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003541#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3542 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3543 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003544 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003545 handshake->async_in_progress = 0;
3546 }
3547#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3548
Andrzej Kurek25f27152022-08-17 16:09:31 -04003549#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003550#if defined(MBEDTLS_USE_PSA_CRYPTO)
3551 psa_hash_abort( &handshake->fin_sha256_psa );
3552#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003553 mbedtls_sha256_free( &handshake->fin_sha256 );
3554#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003555#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003556#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003557#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003558 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003559#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003560 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003561#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003562#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#if defined(MBEDTLS_DHM_C)
3565 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003566#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003567#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003569#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003570#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003571 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003572#if defined(MBEDTLS_SSL_CLI_C)
3573 mbedtls_free( handshake->ecjpake_cache );
3574 handshake->ecjpake_cache = NULL;
3575 handshake->ecjpake_cache_len = 0;
3576#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003577#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003578
Janos Follath4ae5c292016-02-10 11:27:43 +00003579#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3580 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003581 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003583#endif
3584
Gilles Peskineeccd8882020-03-10 12:19:08 +01003585#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003586#if defined(MBEDTLS_USE_PSA_CRYPTO)
3587 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3588 {
3589 /* The maintenance of the external PSK key slot is the
3590 * user's responsibility. */
3591 if( ssl->handshake->psk_opaque_is_internal )
3592 {
3593 psa_destroy_key( ssl->handshake->psk_opaque );
3594 ssl->handshake->psk_opaque_is_internal = 0;
3595 }
3596 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3597 }
Neil Armstronge952a302022-05-03 10:22:14 +02003598#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003599 if( handshake->psk != NULL )
3600 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003601 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003602 mbedtls_free( handshake->psk );
3603 }
Neil Armstronge952a302022-05-03 10:22:14 +02003604#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003605#endif
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3608 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003609 /*
3610 * Free only the linked list wrapper, not the keys themselves
3611 * since the belong to the SNI callback
3612 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003613 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003615
Gilles Peskineeccd8882020-03-10 12:19:08 +01003616#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003617 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003618 if( handshake->ecrs_peer_cert != NULL )
3619 {
3620 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3621 mbedtls_free( handshake->ecrs_peer_cert );
3622 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003623#endif
3624
Hanno Becker75173122019-02-06 16:18:31 +00003625#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3626 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3627 mbedtls_pk_free( &handshake->peer_pubkey );
3628#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3629
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003630#if defined(MBEDTLS_SSL_CLI_C) && \
3631 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3632 mbedtls_free( handshake->cookie );
3633#endif /* MBEDTLS_SSL_CLI_C &&
3634 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003635
3636#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003637 mbedtls_ssl_flight_free( handshake->flight );
3638 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003639#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003640
Ronald Cronf12b81d2022-03-15 10:42:41 +01003641#if defined(MBEDTLS_ECDH_C) && \
3642 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003643 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003644 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003645#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3646
Ronald Cron6f135e12021-12-08 16:57:54 +01003647#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003648 mbedtls_ssl_transform_free( handshake->transform_handshake );
3649 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003650 mbedtls_free( handshake->transform_earlydata );
3651 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003652#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003653
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003654
3655#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3656 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3657 * processes datagrams and the fact that a datagram is allowed to have
3658 * several records in it, it is possible that the I/O buffers are not
3659 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003660 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3661 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003662#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003663
Jerry Yuba9c7272021-10-30 11:54:10 +08003664 /* mbedtls_platform_zeroize MUST be last one in this function */
3665 mbedtls_platform_zeroize( handshake,
3666 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003667}
3668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003670{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003671 if( session == NULL )
3672 return;
3673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003675 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003676#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003677
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003678#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003680#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003681
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003682 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003683}
3684
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003685#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003686
3687#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3688#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3689#else
3690#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3691#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3692
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003693#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003694
3695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3696#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3697#else
3698#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3699#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3700
3701#if defined(MBEDTLS_SSL_ALPN)
3702#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3703#else
3704#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3705#endif /* MBEDTLS_SSL_ALPN */
3706
3707#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3708#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3709#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3710#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3711
3712#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3713 ( (uint32_t) ( \
3714 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3715 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3716 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3717 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3718 0u ) )
3719
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003720static unsigned char ssl_serialized_context_header[] = {
3721 MBEDTLS_VERSION_MAJOR,
3722 MBEDTLS_VERSION_MINOR,
3723 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003724 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3725 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3726 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3727 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3728 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003729};
3730
Paul Bakker5121ce52009-01-03 21:22:43 +00003731/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003732 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003733 *
3734 * The format of the serialized data is:
3735 * (in the presentation language of TLS, RFC 8446 section 3)
3736 *
3737 * // header
3738 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003739 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003740 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003741 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003742 * Note: When updating the format, remember to keep these
3743 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003744 *
3745 * // session sub-structure
3746 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3747 * // transform sub-structure
3748 * uint8 random[64]; // ServerHello.random+ClientHello.random
3749 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3750 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3751 * // fields from ssl_context
3752 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3753 * uint64 in_window_top; // DTLS: last validated record seq_num
3754 * uint64 in_window; // DTLS: bitmask for replay protection
3755 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3756 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3757 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3758 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3759 *
3760 * Note that many fields of the ssl_context or sub-structures are not
3761 * serialized, as they fall in one of the following categories:
3762 *
3763 * 1. forced value (eg in_left must be 0)
3764 * 2. pointer to dynamically-allocated memory (eg session, transform)
3765 * 3. value can be re-derived from other data (eg session keys from MS)
3766 * 4. value was temporary (eg content of input buffer)
3767 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003768 */
3769int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3770 unsigned char *buf,
3771 size_t buf_len,
3772 size_t *olen )
3773{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003774 unsigned char *p = buf;
3775 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003776 size_t session_len;
3777 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003778
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003779 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003780 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3781 * this function's documentation.
3782 *
3783 * These are due to assumptions/limitations in the implementation. Some of
3784 * them are likely to stay (no handshake in progress) some might go away
3785 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003786 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003787 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003788 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003789 {
3790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003792 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003793 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003797 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003798 /* Double-check that sub-structures are indeed ready */
3799 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003800 {
3801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003803 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003804 /* There must be no pending incoming or outgoing data */
3805 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003806 {
3807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003809 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003810 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003811 {
3812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003814 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003815 /* Protocol must be DLTS, not TLS */
3816 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003820 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003821 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003822 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003826 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003827 /* We must be using an AEAD ciphersuite */
3828 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003829 {
3830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003832 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003833 /* Renegotiation must not be enabled */
3834#if defined(MBEDTLS_SSL_RENEGOTIATION)
3835 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003836 {
3837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003839 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003840#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003841
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003842 /*
3843 * Version and format identifier
3844 */
3845 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003846
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003847 if( used <= buf_len )
3848 {
3849 memcpy( p, ssl_serialized_context_header,
3850 sizeof( ssl_serialized_context_header ) );
3851 p += sizeof( ssl_serialized_context_header );
3852 }
3853
3854 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003855 * Session (length + data)
3856 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003857 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003858 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3859 return( ret );
3860
3861 used += 4 + session_len;
3862 if( used <= buf_len )
3863 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003864 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3865 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003866
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003867 ret = ssl_session_save( ssl->session, 1,
3868 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003869 if( ret != 0 )
3870 return( ret );
3871
3872 p += session_len;
3873 }
3874
3875 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003876 * Transform
3877 */
3878 used += sizeof( ssl->transform->randbytes );
3879 if( used <= buf_len )
3880 {
3881 memcpy( p, ssl->transform->randbytes,
3882 sizeof( ssl->transform->randbytes ) );
3883 p += sizeof( ssl->transform->randbytes );
3884 }
3885
3886#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3887 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3888 if( used <= buf_len )
3889 {
3890 *p++ = ssl->transform->in_cid_len;
3891 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3892 p += ssl->transform->in_cid_len;
3893
3894 *p++ = ssl->transform->out_cid_len;
3895 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3896 p += ssl->transform->out_cid_len;
3897 }
3898#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3899
3900 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003901 * Saved fields from top-level ssl_context structure
3902 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003903 used += 4;
3904 if( used <= buf_len )
3905 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003906 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3907 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003908 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003909
3910#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3911 used += 16;
3912 if( used <= buf_len )
3913 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003914 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3915 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003916
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003917 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3918 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003919 }
3920#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3921
3922#if defined(MBEDTLS_SSL_PROTO_DTLS)
3923 used += 1;
3924 if( used <= buf_len )
3925 {
3926 *p++ = ssl->disable_datagram_packing;
3927 }
3928#endif /* MBEDTLS_SSL_PROTO_DTLS */
3929
Jerry Yuae0b2e22021-10-08 15:21:19 +08003930 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003931 if( used <= buf_len )
3932 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003933 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3934 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003935 }
3936
3937#if defined(MBEDTLS_SSL_PROTO_DTLS)
3938 used += 2;
3939 if( used <= buf_len )
3940 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003941 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
3942 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003943 }
3944#endif /* MBEDTLS_SSL_PROTO_DTLS */
3945
3946#if defined(MBEDTLS_SSL_ALPN)
3947 {
3948 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02003949 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003950 : 0;
3951
3952 used += 1 + alpn_len;
3953 if( used <= buf_len )
3954 {
3955 *p++ = alpn_len;
3956
3957 if( ssl->alpn_chosen != NULL )
3958 {
3959 memcpy( p, ssl->alpn_chosen, alpn_len );
3960 p += alpn_len;
3961 }
3962 }
3963 }
3964#endif /* MBEDTLS_SSL_ALPN */
3965
3966 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003967 * Done
3968 */
3969 *olen = used;
3970
3971 if( used > buf_len )
3972 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003973
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003974 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
3975
Hanno Becker43aefe22020-02-05 10:44:56 +00003976 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003977}
3978
3979/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003980 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003981 *
3982 * This internal version is wrapped by a public function that cleans up in
3983 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003984 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003985MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003986static int ssl_context_load( mbedtls_ssl_context *ssl,
3987 const unsigned char *buf,
3988 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003989{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003990 const unsigned char *p = buf;
3991 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003992 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00003993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003994
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003995 /*
3996 * The context should have been freshly setup or reset.
3997 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02003998 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02003999 * renegotiating, or if the user mistakenly loaded a session first.)
4000 */
4001 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4002 ssl->session != NULL )
4003 {
4004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4005 }
4006
4007 /*
4008 * We can't check that the config matches the initial one, but we can at
4009 * least check it matches the requirements for serializing.
4010 */
4011 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004012 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4013 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004014#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004015 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004016#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004017 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004018 {
4019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4020 }
4021
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004022 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4023
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004024 /*
4025 * Check version identifier
4026 */
4027 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4029
4030 if( memcmp( p, ssl_serialized_context_header,
4031 sizeof( ssl_serialized_context_header ) ) != 0 )
4032 {
4033 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4034 }
4035 p += sizeof( ssl_serialized_context_header );
4036
4037 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004038 * Session
4039 */
4040 if( (size_t)( end - p ) < 4 )
4041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4042
4043 session_len = ( (size_t) p[0] << 24 ) |
4044 ( (size_t) p[1] << 16 ) |
4045 ( (size_t) p[2] << 8 ) |
4046 ( (size_t) p[3] );
4047 p += 4;
4048
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004049 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004050 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004051 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004052 ssl->session_in = ssl->session;
4053 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004054 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004055
4056 if( (size_t)( end - p ) < session_len )
4057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4058
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004059 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004060 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004061 {
4062 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004063 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004064 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004065
4066 p += session_len;
4067
4068 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004069 * Transform
4070 */
4071
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004072 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004073 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004074 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004075 ssl->transform_in = ssl->transform;
4076 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004077 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004078
4079 /* Read random bytes and populate structure */
4080 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004083 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004084 ssl->session->ciphersuite,
4085 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004086#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004087 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004088#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004089 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4090 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004091 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004092 ssl->conf->endpoint,
4093 ssl );
4094 if( ret != 0 )
4095 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004096#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004097 p += sizeof( ssl->transform->randbytes );
4098
4099#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4100 /* Read connection IDs and store them */
4101 if( (size_t)( end - p ) < 1 )
4102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4103
4104 ssl->transform->in_cid_len = *p++;
4105
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004106 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004107 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4108
4109 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4110 p += ssl->transform->in_cid_len;
4111
4112 ssl->transform->out_cid_len = *p++;
4113
4114 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4116
4117 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4118 p += ssl->transform->out_cid_len;
4119#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4120
4121 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004122 * Saved fields from top-level ssl_context structure
4123 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004124 if( (size_t)( end - p ) < 4 )
4125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4126
4127 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4128 ( (uint32_t) p[1] << 16 ) |
4129 ( (uint32_t) p[2] << 8 ) |
4130 ( (uint32_t) p[3] );
4131 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004132
4133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4134 if( (size_t)( end - p ) < 16 )
4135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4136
4137 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4138 ( (uint64_t) p[1] << 48 ) |
4139 ( (uint64_t) p[2] << 40 ) |
4140 ( (uint64_t) p[3] << 32 ) |
4141 ( (uint64_t) p[4] << 24 ) |
4142 ( (uint64_t) p[5] << 16 ) |
4143 ( (uint64_t) p[6] << 8 ) |
4144 ( (uint64_t) p[7] );
4145 p += 8;
4146
4147 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4148 ( (uint64_t) p[1] << 48 ) |
4149 ( (uint64_t) p[2] << 40 ) |
4150 ( (uint64_t) p[3] << 32 ) |
4151 ( (uint64_t) p[4] << 24 ) |
4152 ( (uint64_t) p[5] << 16 ) |
4153 ( (uint64_t) p[6] << 8 ) |
4154 ( (uint64_t) p[7] );
4155 p += 8;
4156#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4157
4158#if defined(MBEDTLS_SSL_PROTO_DTLS)
4159 if( (size_t)( end - p ) < 1 )
4160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4161
4162 ssl->disable_datagram_packing = *p++;
4163#endif /* MBEDTLS_SSL_PROTO_DTLS */
4164
Jerry Yud9a94fe2021-09-28 18:58:59 +08004165 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004167 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4168 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004169
4170#if defined(MBEDTLS_SSL_PROTO_DTLS)
4171 if( (size_t)( end - p ) < 2 )
4172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4173
4174 ssl->mtu = ( p[0] << 8 ) | p[1];
4175 p += 2;
4176#endif /* MBEDTLS_SSL_PROTO_DTLS */
4177
4178#if defined(MBEDTLS_SSL_ALPN)
4179 {
4180 uint8_t alpn_len;
4181 const char **cur;
4182
4183 if( (size_t)( end - p ) < 1 )
4184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4185
4186 alpn_len = *p++;
4187
4188 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4189 {
4190 /* alpn_chosen should point to an item in the configured list */
4191 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4192 {
4193 if( strlen( *cur ) == alpn_len &&
4194 memcmp( p, cur, alpn_len ) == 0 )
4195 {
4196 ssl->alpn_chosen = *cur;
4197 break;
4198 }
4199 }
4200 }
4201
4202 /* can only happen on conf mismatch */
4203 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4205
4206 p += alpn_len;
4207 }
4208#endif /* MBEDTLS_SSL_ALPN */
4209
4210 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004211 * Forced fields from top-level ssl_context structure
4212 *
4213 * Most of them already set to the correct value by mbedtls_ssl_init() and
4214 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4215 */
4216 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004217 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004218
Hanno Becker361b10d2019-08-30 10:42:49 +01004219 /* Adjust pointers for header fields of outgoing records to
4220 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004221 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004222
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004223#if defined(MBEDTLS_SSL_PROTO_DTLS)
4224 ssl->in_epoch = 1;
4225#endif
4226
4227 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4228 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004229 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4230 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004231 if( ssl->handshake != NULL )
4232 {
4233 mbedtls_ssl_handshake_free( ssl );
4234 mbedtls_free( ssl->handshake );
4235 ssl->handshake = NULL;
4236 }
4237
4238 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004239 * Done - should have consumed entire buffer
4240 */
4241 if( p != end )
4242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004243
4244 return( 0 );
4245}
4246
4247/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004248 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004249 */
4250int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4251 const unsigned char *buf,
4252 size_t len )
4253{
4254 int ret = ssl_context_load( context, buf, len );
4255
4256 if( ret != 0 )
4257 mbedtls_ssl_free( context );
4258
4259 return( ret );
4260}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004261#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004262
4263/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 * Free an SSL context
4265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004268 if( ssl == NULL )
4269 return;
4270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004273 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 {
sander-visserb8aa2072020-05-06 22:05:13 +02004275#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4276 size_t out_buf_len = ssl->out_buf_len;
4277#else
4278 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4279#endif
4280
Darryl Greenb33cc762019-11-28 14:29:44 +00004281 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004283 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 }
4285
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004286 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004287 {
sander-visserb8aa2072020-05-06 22:05:13 +02004288#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4289 size_t in_buf_len = ssl->in_buf_len;
4290#else
4291 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4292#endif
4293
Darryl Greenb33cc762019-11-28 14:29:44 +00004294 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004296 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 }
4298
Paul Bakker48916f92012-09-16 19:57:18 +00004299 if( ssl->transform )
4300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 mbedtls_ssl_transform_free( ssl->transform );
4302 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004303 }
4304
4305 if( ssl->handshake )
4306 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004307 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4309 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 mbedtls_free( ssl->handshake );
4312 mbedtls_free( ssl->transform_negotiate );
4313 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004314 }
4315
Ronald Cron6f135e12021-12-08 16:57:54 +01004316#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004317 mbedtls_ssl_transform_free( ssl->transform_application );
4318 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004319#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004320
Paul Bakkerc0463502013-02-14 11:19:38 +01004321 if( ssl->session )
4322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 mbedtls_ssl_session_free( ssl->session );
4324 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004325 }
4326
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004327#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004328 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004330 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004334
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004335#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004337#endif
4338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004340
Paul Bakker86f04f42013-02-14 11:20:09 +01004341 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004342 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343}
4344
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004345/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004346 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004347 */
4348void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4349{
4350 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4351}
4352
Gilles Peskineae270bf2021-06-02 00:05:29 +02004353/* The selection should be the same as mbedtls_x509_crt_profile_default in
4354 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004355 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004356 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004357 * about this list.
4358 */
Brett Warrene0edc842021-08-17 09:53:13 +01004359static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004360#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004361 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004362#endif
4363#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004364 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004365#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004366#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004367 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004368#endif
4369#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004370 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004371#endif
4372#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004373 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004374#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004375#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004376 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004377#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004378#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004379 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004380#endif
4381#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004382 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004383#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004384 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004385};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004386
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004387static int ssl_preset_suiteb_ciphersuites[] = {
4388 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4389 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4390 0
4391};
4392
Gilles Peskineeccd8882020-03-10 12:19:08 +01004393#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004394
Jerry Yu909df7b2022-01-22 11:56:27 +08004395/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004396 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004397 * rules SHOULD be upheld.
4398 * - No duplicate entries.
4399 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004400 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004401 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004402 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004403static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004404
Andrzej Kurek25f27152022-08-17 16:09:31 -04004405#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 +08004406 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4407 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004408#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004409 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004410
Andrzej Kurek25f27152022-08-17 16:09:31 -04004411#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 +08004412 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4413 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004414#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004415 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4416
Andrzej Kurek25f27152022-08-17 16:09:31 -04004417#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 +08004418 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4419 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004420#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004421 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004422
Andrzej Kurek25f27152022-08-17 16:09:31 -04004423#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 +08004424 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004425#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 +08004426
Andrzej Kurek25f27152022-08-17 16:09:31 -04004427#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 +08004428 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004429#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 +08004430
Andrzej Kurek25f27152022-08-17 16:09:31 -04004431#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 +08004432 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004433#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 +08004434
Andrzej Kurek25f27152022-08-17 16:09:31 -04004435#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 +08004436 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4437#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4438
Andrzej Kurek25f27152022-08-17 16:09:31 -04004439#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 +08004440 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4441#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4442
Andrzej Kurek25f27152022-08-17 16:09:31 -04004443#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 +08004444 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4445#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4446
Gabor Mezei15b95a62022-05-09 16:37:58 +02004447 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004448};
4449
4450/* NOTICE: see above */
4451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4452static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004453#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004454#if defined(MBEDTLS_ECDSA_C)
4455 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004456#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004457#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4458 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4459#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004460#if defined(MBEDTLS_RSA_C)
4461 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4462#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004463#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004464#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004465#if defined(MBEDTLS_ECDSA_C)
4466 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004467#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004468#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4469 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4470#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004471#if defined(MBEDTLS_RSA_C)
4472 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4473#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004474#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004475#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004476#if defined(MBEDTLS_ECDSA_C)
4477 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004478#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004479#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4480 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4481#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004482#if defined(MBEDTLS_RSA_C)
4483 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4484#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004486 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004487};
4488#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4489/* NOTICE: see above */
4490static uint16_t ssl_preset_suiteb_sig_algs[] = {
4491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4494 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004495#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004496 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4497
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4500 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004501#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004502 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004503
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004506#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 +08004507
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#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 +08004509 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004510#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004511
Gabor Mezei15b95a62022-05-09 16:37:58 +02004512 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004513};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004514
Jerry Yu909df7b2022-01-22 11:56:27 +08004515/* NOTICE: see above */
4516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4517static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#if defined(MBEDTLS_HAS_ALG_SHA_256_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_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +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_SHA256 ),
4524#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004525#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004526#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004527#if defined(MBEDTLS_ECDSA_C)
4528 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004529#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004530#if defined(MBEDTLS_RSA_C)
4531 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4532#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004533#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004534 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004535};
Jerry Yu909df7b2022-01-22 11:56:27 +08004536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4537
Jerry Yu1a8b4812022-01-20 17:56:50 +08004538#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004539
Brett Warrene0edc842021-08-17 09:53:13 +01004540static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004541#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004542 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004543#endif
4544#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004545 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004546#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004547 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004548};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004549
Jerry Yu1a8b4812022-01-20 17:56:50 +08004550#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004551/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004552 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004553MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004554static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004555{
4556 size_t i, j;
4557 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004558
Gabor Mezei15b95a62022-05-09 16:37:58 +02004559 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004560 {
Jerry Yuf377d642022-01-25 10:43:59 +08004561 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004562 {
Jerry Yuf377d642022-01-25 10:43:59 +08004563 if( sig_algs[i] != sig_algs[j] )
4564 continue;
4565 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4566 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4567 sig_algs[i], j, i );
4568 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004569 }
4570 }
4571 return( ret );
4572}
4573
4574#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4575
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004576/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004577 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004578 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004579int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004580 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004581{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004582#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004584#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004585
Jerry Yu1a8b4812022-01-20 17:56:50 +08004586#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004587 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004588 {
4589 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4590 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4591 }
4592
Jerry Yuf377d642022-01-25 10:43:59 +08004593 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004594 {
4595 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4596 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4597 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004598
4599#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004600 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004601 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004602 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004603 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4604 }
4605
Jerry Yuf377d642022-01-25 10:43:59 +08004606 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004607 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004608 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004609 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4610 }
4611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004612#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4613
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004614 /* Use the functions here so that they are covered in tests,
4615 * but otherwise access member directly for efficiency */
4616 mbedtls_ssl_conf_endpoint( conf, endpoint );
4617 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004618
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004619 /*
4620 * Things that are common to all presets
4621 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004622#if defined(MBEDTLS_SSL_CLI_C)
4623 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4624 {
4625 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4626#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4627 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4628#endif
4629 }
4630#endif
4631
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004632#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4633 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4634#endif
4635
4636#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4637 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4638#endif
4639
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004640#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004641 conf->f_cookie_write = ssl_cookie_write_dummy;
4642 conf->f_cookie_check = ssl_cookie_check_dummy;
4643#endif
4644
4645#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4646 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4647#endif
4648
Janos Follath088ce432017-04-10 12:42:31 +01004649#if defined(MBEDTLS_SSL_SRV_C)
4650 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004651 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004652#endif
4653
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004654#if defined(MBEDTLS_SSL_PROTO_DTLS)
4655 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4656 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4657#endif
4658
4659#if defined(MBEDTLS_SSL_RENEGOTIATION)
4660 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004661 memset( conf->renego_period, 0x00, 2 );
4662 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004663#endif
4664
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004665#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004666 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4667 {
4668 const unsigned char dhm_p[] =
4669 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4670 const unsigned char dhm_g[] =
4671 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004672
Hanno Beckere2defad2021-07-24 05:59:17 +01004673 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4674 dhm_p, sizeof( dhm_p ),
4675 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4676 {
4677 return( ret );
4678 }
4679 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004680#endif
4681
Ronald Cron6f135e12021-12-08 16:57:54 +01004682#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004683#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004684 mbedtls_ssl_conf_new_session_tickets(
4685 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4686#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004687 /*
4688 * Allow all TLS 1.3 key exchange modes by default.
4689 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004690 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004691#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004692
XiaokangQian4d3a6042022-04-21 13:46:17 +00004693 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4694 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004695#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004696 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004697 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004698#else
XiaokangQian060d8672022-04-21 09:24:56 +00004699 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004700#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004701 }
4702 else
4703 {
4704#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4705 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4706 {
4707 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4708 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4709 }
4710 else
4711 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4712 {
4713 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4714 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4715 }
4716#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4717 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4718 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4719#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4720 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4721 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4722#else
4723 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4724#endif
4725 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004726
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004727 /*
4728 * Preset-specific defaults
4729 */
4730 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004731 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004732 /*
4733 * NSA Suite B
4734 */
4735 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004736
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004737 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004738
4739#if defined(MBEDTLS_X509_CRT_PARSE_C)
4740 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004741#endif
4742
Gilles Peskineeccd8882020-03-10 12:19:08 +01004743#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4745 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4746 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4747 else
4748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4749 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004750#endif
4751
Brett Warrene0edc842021-08-17 09:53:13 +01004752#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4753 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004754#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004755 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004756 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004757
4758 /*
4759 * Default
4760 */
4761 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004762
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004763 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004764
4765#if defined(MBEDTLS_X509_CRT_PARSE_C)
4766 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4767#endif
4768
Gilles Peskineeccd8882020-03-10 12:19:08 +01004769#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004770#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4771 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4772 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4773 else
4774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4775 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004776#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004777
Brett Warrene0edc842021-08-17 09:53:13 +01004778#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4779 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004780#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004781 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004782
4783#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4784 conf->dhm_min_bitlen = 1024;
4785#endif
4786 }
4787
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004788 return( 0 );
4789}
4790
4791/*
4792 * Free mbedtls_ssl_config
4793 */
4794void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4795{
4796#if defined(MBEDTLS_DHM_C)
4797 mbedtls_mpi_free( &conf->dhm_P );
4798 mbedtls_mpi_free( &conf->dhm_G );
4799#endif
4800
Gilles Peskineeccd8882020-03-10 12:19:08 +01004801#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004802#if defined(MBEDTLS_USE_PSA_CRYPTO)
4803 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4804 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004805 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4806 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004807#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004808 if( conf->psk != NULL )
4809 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004810 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004811 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004812 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004813 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004814 }
4815
4816 if( conf->psk_identity != NULL )
4817 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004818 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004819 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004820 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004821 conf->psk_identity_len = 0;
4822 }
4823#endif
4824
4825#if defined(MBEDTLS_X509_CRT_PARSE_C)
4826 ssl_key_cert_free( conf->key_cert );
4827#endif
4828
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004829 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004830}
4831
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004832#if defined(MBEDTLS_PK_C) && \
4833 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004834/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#if defined(MBEDTLS_RSA_C)
4840 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4841 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843#if defined(MBEDTLS_ECDSA_C)
4844 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4845 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004848}
4849
Hanno Becker7e5437a2017-04-28 17:15:26 +01004850unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4851{
4852 switch( type ) {
4853 case MBEDTLS_PK_RSA:
4854 return( MBEDTLS_SSL_SIG_RSA );
4855 case MBEDTLS_PK_ECDSA:
4856 case MBEDTLS_PK_ECKEY:
4857 return( MBEDTLS_SSL_SIG_ECDSA );
4858 default:
4859 return( MBEDTLS_SSL_SIG_ANON );
4860 }
4861}
4862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004864{
4865 switch( sig )
4866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867#if defined(MBEDTLS_RSA_C)
4868 case MBEDTLS_SSL_SIG_RSA:
4869 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871#if defined(MBEDTLS_ECDSA_C)
4872 case MBEDTLS_SSL_SIG_ECDSA:
4873 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004874#endif
4875 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004877 }
4878}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004879#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004880
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004881/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004882 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004885{
4886 switch( hash )
4887 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 case MBEDTLS_SSL_HASH_MD5:
4890 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004891#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004892#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 case MBEDTLS_SSL_HASH_SHA1:
4894 return( MBEDTLS_MD_SHA1 );
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_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 case MBEDTLS_SSL_HASH_SHA224:
4898 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004899#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004900#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 case MBEDTLS_SSL_HASH_SHA256:
4902 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004903#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004904#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 case MBEDTLS_SSL_HASH_SHA384:
4906 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004907#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004908#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909 case MBEDTLS_SSL_HASH_SHA512:
4910 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004911#endif
4912 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004914 }
4915}
4916
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004917/*
4918 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4919 */
4920unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4921{
4922 switch( md )
4923 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004924#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004925 case MBEDTLS_MD_MD5:
4926 return( MBEDTLS_SSL_HASH_MD5 );
4927#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004928#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004929 case MBEDTLS_MD_SHA1:
4930 return( MBEDTLS_SSL_HASH_SHA1 );
4931#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004932#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004933 case MBEDTLS_MD_SHA224:
4934 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004935#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004936#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004937 case MBEDTLS_MD_SHA256:
4938 return( MBEDTLS_SSL_HASH_SHA256 );
4939#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004940#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004941 case MBEDTLS_MD_SHA384:
4942 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004943#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004944#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004945 case MBEDTLS_MD_SHA512:
4946 return( MBEDTLS_SSL_HASH_SHA512 );
4947#endif
4948 default:
4949 return( MBEDTLS_SSL_HASH_NONE );
4950 }
4951}
4952
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004953/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004954 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004955 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004956 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004957int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004958{
Brett Warrene0edc842021-08-17 09:53:13 +01004959 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004960
Brett Warrene0edc842021-08-17 09:53:13 +01004961 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004962 return( -1 );
4963
Brett Warrene0edc842021-08-17 09:53:13 +01004964 for( ; *group_list != 0; group_list++ )
4965 {
4966 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004967 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01004968 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004969
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004970 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004971}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004972
4973#if defined(MBEDTLS_ECP_C)
4974/*
4975 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
4976 */
4977int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
4978{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004979 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004980 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004981
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01004982 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07004983 return -1;
4984
4985 uint16_t tls_id = grp_info->tls_id;
4986
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01004987 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
4988}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004989#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991#if defined(MBEDTLS_X509_CRT_PARSE_C)
4992int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
4993 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004994 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02004995 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004996{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004997 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004998 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004999 const char *ext_oid;
5000 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005003 {
5004 /* Server part of the key exchange */
5005 switch( ciphersuite->key_exchange )
5006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 case MBEDTLS_KEY_EXCHANGE_RSA:
5008 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005009 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005010 break;
5011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5013 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5014 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5015 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016 break;
5017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5019 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005020 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005021 break;
5022
5023 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 case MBEDTLS_KEY_EXCHANGE_NONE:
5025 case MBEDTLS_KEY_EXCHANGE_PSK:
5026 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5027 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005028 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005029 usage = 0;
5030 }
5031 }
5032 else
5033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5035 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005036 }
5037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005039 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005040 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005041 ret = -1;
5042 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5047 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005048 }
5049 else
5050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5052 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005053 }
5054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005056 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005057 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005058 ret = -1;
5059 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005060
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005061 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005064
Jerry Yu148165c2021-09-24 23:20:59 +08005065#if defined(MBEDTLS_USE_PSA_CRYPTO)
5066int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5067 const mbedtls_md_type_t md,
5068 unsigned char *dst,
5069 size_t dst_len,
5070 size_t *olen )
5071{
Ronald Cronf6893e12022-01-07 22:09:01 +01005072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5073 psa_hash_operation_t *hash_operation_to_clone;
5074 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5075
Jerry Yu148165c2021-09-24 23:20:59 +08005076 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005077
5078 switch( md )
5079 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005080#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005081 case MBEDTLS_MD_SHA384:
5082 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5083 break;
5084#endif
5085
Andrzej Kurek25f27152022-08-17 16:09:31 -04005086#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005087 case MBEDTLS_MD_SHA256:
5088 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5089 break;
5090#endif
5091
5092 default:
5093 goto exit;
5094 }
5095
5096 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5097 if( status != PSA_SUCCESS )
5098 goto exit;
5099
5100 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5101 if( status != PSA_SUCCESS )
5102 goto exit;
5103
5104exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005105 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005106}
5107#else /* MBEDTLS_USE_PSA_CRYPTO */
5108
Andrzej Kurek25f27152022-08-17 16:09:31 -04005109#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005110MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005111static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5112 unsigned char *dst,
5113 size_t dst_len,
5114 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005115{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005116 int ret;
5117 mbedtls_sha512_context sha512;
5118
5119 if( dst_len < 48 )
5120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5121
5122 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005123 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005124
5125 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5126 {
5127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5128 goto exit;
5129 }
5130
5131 *olen = 48;
5132
5133exit:
5134
5135 mbedtls_sha512_free( &sha512 );
5136 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005137}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005138#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005139
Andrzej Kurek25f27152022-08-17 16:09:31 -04005140#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005141MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005142static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5143 unsigned char *dst,
5144 size_t dst_len,
5145 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005146{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005147 int ret;
5148 mbedtls_sha256_context sha256;
5149
5150 if( dst_len < 32 )
5151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5152
5153 mbedtls_sha256_init( &sha256 );
5154 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005155
Jerry Yu24c0ec32021-09-09 14:21:07 +08005156 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5157 {
5158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5159 goto exit;
5160 }
5161
5162 *olen = 32;
5163
5164exit:
5165
5166 mbedtls_sha256_free( &sha256 );
5167 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005168}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005169#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005170
Jerry Yu000f9762021-09-14 11:12:51 +08005171int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5172 const mbedtls_md_type_t md,
5173 unsigned char *dst,
5174 size_t dst_len,
5175 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005176{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005177 switch( md )
5178 {
5179
Andrzej Kurek25f27152022-08-17 16:09:31 -04005180#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005181 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005182 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005183#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005184
Andrzej Kurek25f27152022-08-17 16:09:31 -04005185#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005186 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005187 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005188#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005189
5190 default:
5191 break;
5192 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005194}
XiaokangQian647719a2021-12-07 09:16:29 +00005195
Jerry Yu148165c2021-09-24 23:20:59 +08005196#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005197
Gabor Mezei078e8032022-04-27 21:17:56 +02005198#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5199/* mbedtls_ssl_parse_sig_alg_ext()
5200 *
5201 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5202 * value (TLS 1.3 RFC8446):
5203 * enum {
5204 * ....
5205 * ecdsa_secp256r1_sha256( 0x0403 ),
5206 * ecdsa_secp384r1_sha384( 0x0503 ),
5207 * ecdsa_secp521r1_sha512( 0x0603 ),
5208 * ....
5209 * } SignatureScheme;
5210 *
5211 * struct {
5212 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5213 * } SignatureSchemeList;
5214 *
5215 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5216 * value (TLS 1.2 RFC5246):
5217 * enum {
5218 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5219 * sha512(6), (255)
5220 * } HashAlgorithm;
5221 *
5222 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5223 * SignatureAlgorithm;
5224 *
5225 * struct {
5226 * HashAlgorithm hash;
5227 * SignatureAlgorithm signature;
5228 * } SignatureAndHashAlgorithm;
5229 *
5230 * SignatureAndHashAlgorithm
5231 * supported_signature_algorithms<2..2^16-2>;
5232 *
5233 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5234 * generalization of the TLS 1.2 signature algorithm extension.
5235 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5236 * `SignatureScheme` field of TLS 1.3
5237 *
5238 */
5239int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5240 const unsigned char *buf,
5241 const unsigned char *end )
5242{
5243 const unsigned char *p = buf;
5244 size_t supported_sig_algs_len = 0;
5245 const unsigned char *supported_sig_algs_end;
5246 uint16_t sig_alg;
5247 uint32_t common_idx = 0;
5248
5249 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5250 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5251 p += 2;
5252
5253 memset( ssl->handshake->received_sig_algs, 0,
5254 sizeof(ssl->handshake->received_sig_algs) );
5255
5256 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5257 supported_sig_algs_end = p + supported_sig_algs_len;
5258 while( p < supported_sig_algs_end )
5259 {
5260 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5261 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5262 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005263 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5264 sig_alg,
5265 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005266#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005267 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005268 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5269 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5270 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005271 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005272 }
5273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005274
Jerry Yuf3b46b52022-06-19 16:52:27 +08005275 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5276 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005277
5278 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5279 {
5280 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5281 common_idx += 1;
5282 }
5283 }
5284 /* Check that we consumed all the message. */
5285 if( p != end )
5286 {
5287 MBEDTLS_SSL_DEBUG_MSG( 1,
5288 ( "Signature algorithms extension length misaligned" ) );
5289 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5290 MBEDTLS_ERR_SSL_DECODE_ERROR );
5291 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5292 }
5293
5294 if( common_idx == 0 )
5295 {
5296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5297 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5298 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5299 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5300 }
5301
Gabor Mezei15b95a62022-05-09 16:37:58 +02005302 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005303 return( 0 );
5304}
5305
5306#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5307
Jerry Yudc7bd172022-02-17 13:44:15 +08005308#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5309
5310#if defined(MBEDTLS_USE_PSA_CRYPTO)
5311
5312static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5313 mbedtls_svc_key_id_t key,
5314 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005315 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005316 const unsigned char* seed, size_t seed_length,
5317 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005318 const unsigned char* other_secret,
5319 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005320 size_t capacity )
5321{
5322 psa_status_t status;
5323
5324 status = psa_key_derivation_setup( derivation, alg );
5325 if( status != PSA_SUCCESS )
5326 return( status );
5327
5328 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5329 {
5330 status = psa_key_derivation_input_bytes( derivation,
5331 PSA_KEY_DERIVATION_INPUT_SEED,
5332 seed, seed_length );
5333 if( status != PSA_SUCCESS )
5334 return( status );
5335
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005336 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005337 {
5338 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005339 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5340 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005341 if( status != PSA_SUCCESS )
5342 return( status );
5343 }
5344
Jerry Yudc7bd172022-02-17 13:44:15 +08005345 if( mbedtls_svc_key_id_is_null( key ) )
5346 {
5347 status = psa_key_derivation_input_bytes(
5348 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005349 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005350 }
5351 else
5352 {
5353 status = psa_key_derivation_input_key(
5354 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5355 }
5356 if( status != PSA_SUCCESS )
5357 return( status );
5358
5359 status = psa_key_derivation_input_bytes( derivation,
5360 PSA_KEY_DERIVATION_INPUT_LABEL,
5361 label, label_length );
5362 if( status != PSA_SUCCESS )
5363 return( status );
5364 }
5365 else
5366 {
5367 return( PSA_ERROR_NOT_SUPPORTED );
5368 }
5369
5370 status = psa_key_derivation_set_capacity( derivation, capacity );
5371 if( status != PSA_SUCCESS )
5372 return( status );
5373
5374 return( PSA_SUCCESS );
5375}
5376
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005377MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005378static int tls_prf_generic( mbedtls_md_type_t md_type,
5379 const unsigned char *secret, size_t slen,
5380 const char *label,
5381 const unsigned char *random, size_t rlen,
5382 unsigned char *dstbuf, size_t dlen )
5383{
5384 psa_status_t status;
5385 psa_algorithm_t alg;
5386 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5387 psa_key_derivation_operation_t derivation =
5388 PSA_KEY_DERIVATION_OPERATION_INIT;
5389
5390 if( md_type == MBEDTLS_MD_SHA384 )
5391 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5392 else
5393 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5394
5395 /* Normally a "secret" should be long enough to be impossible to
5396 * find by brute force, and in particular should not be empty. But
5397 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5398 * and for this use case it makes sense to have a 0-length "secret".
5399 * Since the key API doesn't allow importing a key of length 0,
5400 * keep master_key=0, which setup_psa_key_derivation() understands
5401 * to mean a 0-length "secret" input. */
5402 if( slen != 0 )
5403 {
5404 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5405 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5406 psa_set_key_algorithm( &key_attributes, alg );
5407 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5408
5409 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5410 if( status != PSA_SUCCESS )
5411 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5412 }
5413
5414 status = setup_psa_key_derivation( &derivation,
5415 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005416 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005417 random, rlen,
5418 (unsigned char const *) label,
5419 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005420 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005421 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_output_bytes( &derivation, dstbuf, dlen );
5430 if( status != PSA_SUCCESS )
5431 {
5432 psa_key_derivation_abort( &derivation );
5433 psa_destroy_key( master_key );
5434 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5435 }
5436
5437 status = psa_key_derivation_abort( &derivation );
5438 if( status != PSA_SUCCESS )
5439 {
5440 psa_destroy_key( master_key );
5441 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5442 }
5443
5444 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5445 status = psa_destroy_key( master_key );
5446 if( status != PSA_SUCCESS )
5447 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5448
5449 return( 0 );
5450}
5451
5452#else /* MBEDTLS_USE_PSA_CRYPTO */
5453
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005454MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005455static int tls_prf_generic( mbedtls_md_type_t md_type,
5456 const unsigned char *secret, size_t slen,
5457 const char *label,
5458 const unsigned char *random, size_t rlen,
5459 unsigned char *dstbuf, size_t dlen )
5460{
5461 size_t nb;
5462 size_t i, j, k, md_len;
5463 unsigned char *tmp;
5464 size_t tmp_len = 0;
5465 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5466 const mbedtls_md_info_t *md_info;
5467 mbedtls_md_context_t md_ctx;
5468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5469
5470 mbedtls_md_init( &md_ctx );
5471
5472 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5474
5475 md_len = mbedtls_md_get_size( md_info );
5476
5477 tmp_len = md_len + strlen( label ) + rlen;
5478 tmp = mbedtls_calloc( 1, tmp_len );
5479 if( tmp == NULL )
5480 {
5481 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5482 goto exit;
5483 }
5484
5485 nb = strlen( label );
5486 memcpy( tmp + md_len, label, nb );
5487 memcpy( tmp + md_len + nb, random, rlen );
5488 nb += rlen;
5489
5490 /*
5491 * Compute P_<hash>(secret, label + random)[0..dlen]
5492 */
5493 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5494 goto exit;
5495
5496 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5497 if( ret != 0 )
5498 goto exit;
5499 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5500 if( ret != 0 )
5501 goto exit;
5502 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5503 if( ret != 0 )
5504 goto exit;
5505
5506 for( i = 0; i < dlen; i += md_len )
5507 {
5508 ret = mbedtls_md_hmac_reset ( &md_ctx );
5509 if( ret != 0 )
5510 goto exit;
5511 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5512 if( ret != 0 )
5513 goto exit;
5514 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5515 if( ret != 0 )
5516 goto exit;
5517
5518 ret = mbedtls_md_hmac_reset ( &md_ctx );
5519 if( ret != 0 )
5520 goto exit;
5521 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5522 if( ret != 0 )
5523 goto exit;
5524 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5525 if( ret != 0 )
5526 goto exit;
5527
5528 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5529
5530 for( j = 0; j < k; j++ )
5531 dstbuf[i + j] = h_i[j];
5532 }
5533
5534exit:
5535 mbedtls_md_free( &md_ctx );
5536
5537 mbedtls_platform_zeroize( tmp, tmp_len );
5538 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5539
5540 mbedtls_free( tmp );
5541
5542 return( ret );
5543}
5544#endif /* MBEDTLS_USE_PSA_CRYPTO */
5545
Andrzej Kurek25f27152022-08-17 16:09:31 -04005546#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005547MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005548static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5549 const char *label,
5550 const unsigned char *random, size_t rlen,
5551 unsigned char *dstbuf, size_t dlen )
5552{
5553 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5554 label, random, rlen, dstbuf, dlen ) );
5555}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005556#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005557
Andrzej Kurek25f27152022-08-17 16:09:31 -04005558#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005559MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005560static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5561 const char *label,
5562 const unsigned char *random, size_t rlen,
5563 unsigned char *dstbuf, size_t dlen )
5564{
5565 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5566 label, random, rlen, dstbuf, dlen ) );
5567}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005568#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005569
Jerry Yuf009d862022-02-17 14:01:37 +08005570/*
5571 * Set appropriate PRF function and other SSL / TLS1.2 functions
5572 *
5573 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005574 * - hash associated with the ciphersuite (only used by TLS 1.2)
5575 *
5576 * Outputs:
5577 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5578 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005579MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005580static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005581 mbedtls_md_type_t hash )
5582{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005583#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005584 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005585 {
5586 handshake->tls_prf = tls_prf_sha384;
5587 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5588 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5589 }
5590 else
5591#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005592#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005593 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005594 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005595 handshake->tls_prf = tls_prf_sha256;
5596 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5597 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5598 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005599#else
Jerry Yuf009d862022-02-17 14:01:37 +08005600 {
Jerry Yuf009d862022-02-17 14:01:37 +08005601 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005602 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5604 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005605#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005606
5607 return( 0 );
5608}
Jerry Yud6ab2352022-02-17 14:03:43 +08005609
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005610/*
5611 * Compute master secret if needed
5612 *
5613 * Parameters:
5614 * [in/out] handshake
5615 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5616 * (PSA-PSK) ciphersuite_info, psk_opaque
5617 * [out] premaster (cleared)
5618 * [out] master
5619 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5620 * debug: conf->f_dbg, conf->p_dbg
5621 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005622 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005623 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005624MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005625static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5626 unsigned char *master,
5627 const mbedtls_ssl_context *ssl )
5628{
5629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5630
5631 /* cf. RFC 5246, Section 8.1:
5632 * "The master secret is always exactly 48 bytes in length." */
5633 size_t const master_secret_len = 48;
5634
5635#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5636 unsigned char session_hash[48];
5637#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5638
5639 /* The label for the KDF used for key expansion.
5640 * This is either "master secret" or "extended master secret"
5641 * depending on whether the Extended Master Secret extension
5642 * is used. */
5643 char const *lbl = "master secret";
5644
Przemek Stekielae4ed302022-04-05 17:15:55 +02005645 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005646 * - If the Extended Master Secret extension is not used,
5647 * this is ClientHello.Random + ServerHello.Random
5648 * (see Sect. 8.1 in RFC 5246).
5649 * - If the Extended Master Secret extension is used,
5650 * this is the transcript of the handshake so far.
5651 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005652 unsigned char const *seed = handshake->randbytes;
5653 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005654
5655#if !defined(MBEDTLS_DEBUG_C) && \
5656 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5657 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5658 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5659 ssl = NULL; /* make sure we don't use it except for those cases */
5660 (void) ssl;
5661#endif
5662
5663 if( handshake->resume != 0 )
5664 {
5665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5666 return( 0 );
5667 }
5668
5669#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5670 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5671 {
5672 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005673 seed = session_hash;
5674 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005675
5676 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005677 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005678 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005679#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005680
Przemek Stekiel99114f32022-04-22 11:20:09 +02005681#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005682 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005683 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005684 {
5685 /* Perform PSK-to-MS expansion in a single step. */
5686 psa_status_t status;
5687 psa_algorithm_t alg;
5688 mbedtls_svc_key_id_t psk;
5689 psa_key_derivation_operation_t derivation =
5690 PSA_KEY_DERIVATION_OPERATION_INIT;
5691 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5692
5693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5694
5695 psk = mbedtls_ssl_get_opaque_psk( ssl );
5696
5697 if( hash_alg == MBEDTLS_MD_SHA384 )
5698 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5699 else
5700 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5701
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005702 size_t other_secret_len = 0;
5703 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005704
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005705 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005706 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005707 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005708 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005709 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005710 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005711 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005712 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005713 other_secret_len = 48;
5714 other_secret = handshake->premaster + 2;
5715 break;
5716 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005717 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005718 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5719 other_secret = handshake->premaster + 2;
5720 break;
5721 default:
5722 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005723 }
5724
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005725 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005726 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005727 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005728 (unsigned char const *) lbl,
5729 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005730 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005731 master_secret_len );
5732 if( status != PSA_SUCCESS )
5733 {
5734 psa_key_derivation_abort( &derivation );
5735 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5736 }
5737
5738 status = psa_key_derivation_output_bytes( &derivation,
5739 master,
5740 master_secret_len );
5741 if( status != PSA_SUCCESS )
5742 {
5743 psa_key_derivation_abort( &derivation );
5744 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5745 }
5746
5747 status = psa_key_derivation_abort( &derivation );
5748 if( status != PSA_SUCCESS )
5749 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5750 }
5751 else
5752#endif
5753 {
5754 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005755 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005756 master,
5757 master_secret_len );
5758 if( ret != 0 )
5759 {
5760 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5761 return( ret );
5762 }
5763
5764 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5765 handshake->premaster,
5766 handshake->pmslen );
5767
5768 mbedtls_platform_zeroize( handshake->premaster,
5769 sizeof(handshake->premaster) );
5770 }
5771
5772 return( 0 );
5773}
5774
Jerry Yud62f87e2022-02-17 14:09:02 +08005775int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5776{
5777 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5778 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5779 ssl->handshake->ciphersuite_info;
5780
5781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5782
5783 /* Set PRF, calc_verify and calc_finished function pointers */
5784 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005785 ciphersuite_info->mac );
5786 if( ret != 0 )
5787 {
5788 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5789 return( ret );
5790 }
5791
5792 /* Compute master secret if needed */
5793 ret = ssl_compute_master( ssl->handshake,
5794 ssl->session_negotiate->master,
5795 ssl );
5796 if( ret != 0 )
5797 {
5798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5799 return( ret );
5800 }
5801
5802 /* Swap the client and server random values:
5803 * - MS derivation wanted client+server (RFC 5246 8.1)
5804 * - key derivation wants server+client (RFC 5246 6.3) */
5805 {
5806 unsigned char tmp[64];
5807 memcpy( tmp, ssl->handshake->randbytes, 64 );
5808 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5809 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5810 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5811 }
5812
5813 /* Populate transform structure */
5814 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5815 ssl->session_negotiate->ciphersuite,
5816 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005817#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005818 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005819#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005820 ssl->handshake->tls_prf,
5821 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005822 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005823 ssl->conf->endpoint,
5824 ssl );
5825 if( ret != 0 )
5826 {
5827 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5828 return( ret );
5829 }
5830
5831 /* We no longer need Server/ClientHello.random values */
5832 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5833 sizeof( ssl->handshake->randbytes ) );
5834
5835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5836
5837 return( 0 );
5838}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005839
Ronald Cron4dcbca92022-03-07 10:21:40 +01005840int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5841{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005842 switch( md )
5843 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005844#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005845 case MBEDTLS_SSL_HASH_SHA384:
5846 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5847 break;
5848#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005849#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005850 case MBEDTLS_SSL_HASH_SHA256:
5851 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5852 break;
5853#endif
5854 default:
5855 return( -1 );
5856 }
5857
Ronald Cronc2f13a02022-03-07 10:25:24 +01005858 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005859}
5860
Andrzej Kurek25f27152022-08-17 16:09:31 -04005861#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005862void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5863 unsigned char *hash,
5864 size_t *hlen )
5865{
5866#if defined(MBEDTLS_USE_PSA_CRYPTO)
5867 size_t hash_size;
5868 psa_status_t status;
5869 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5870
5871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5872 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5873 if( status != PSA_SUCCESS )
5874 {
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5876 return;
5877 }
5878
5879 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5880 if( status != PSA_SUCCESS )
5881 {
5882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5883 return;
5884 }
5885
5886 *hlen = 32;
5887 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5889#else
5890 mbedtls_sha256_context sha256;
5891
5892 mbedtls_sha256_init( &sha256 );
5893
5894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5895
5896 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5897 mbedtls_sha256_finish( &sha256, hash );
5898
5899 *hlen = 32;
5900
5901 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5903
5904 mbedtls_sha256_free( &sha256 );
5905#endif /* MBEDTLS_USE_PSA_CRYPTO */
5906 return;
5907}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005908#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005909
Andrzej Kurek25f27152022-08-17 16:09:31 -04005910#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005911void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5912 unsigned char *hash,
5913 size_t *hlen )
5914{
5915#if defined(MBEDTLS_USE_PSA_CRYPTO)
5916 size_t hash_size;
5917 psa_status_t status;
5918 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5919
5920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5921 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5922 if( status != PSA_SUCCESS )
5923 {
5924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5925 return;
5926 }
5927
5928 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5929 if( status != PSA_SUCCESS )
5930 {
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5932 return;
5933 }
5934
5935 *hlen = 48;
5936 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5938#else
5939 mbedtls_sha512_context sha512;
5940
5941 mbedtls_sha512_init( &sha512 );
5942
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
5944
Andrzej Kureka242e832022-08-11 10:03:14 -04005945 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08005946 mbedtls_sha512_finish( &sha512, hash );
5947
5948 *hlen = 48;
5949
5950 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5952
5953 mbedtls_sha512_free( &sha512 );
5954#endif /* MBEDTLS_USE_PSA_CRYPTO */
5955 return;
5956}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005957#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08005958
Neil Armstrong80f6f322022-05-03 17:56:38 +02005959#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
5960 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08005961int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
5962{
5963 unsigned char *p = ssl->handshake->premaster;
5964 unsigned char *end = p + sizeof( ssl->handshake->premaster );
5965 const unsigned char *psk = NULL;
5966 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005967 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08005968
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005969 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08005970 {
5971 /*
5972 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005973 * checked before calling this function.
5974 *
5975 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005976 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08005977 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005978 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
5979 {
5980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5982 }
Jerry Yuce3dca42022-02-17 14:16:37 +08005983 }
5984
5985 /*
5986 * PMS = struct {
5987 * opaque other_secret<0..2^16-1>;
5988 * opaque psk<0..2^16-1>;
5989 * };
5990 * with "other_secret" depending on the particular key exchange
5991 */
5992#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
5993 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
5994 {
5995 if( end - p < 2 )
5996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5997
5998 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
5999 p += 2;
6000
6001 if( end < p || (size_t)( end - p ) < psk_len )
6002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6003
6004 memset( p, 0, psk_len );
6005 p += psk_len;
6006 }
6007 else
6008#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6009#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6010 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6011 {
6012 /*
6013 * other_secret already set by the ClientKeyExchange message,
6014 * and is 48 bytes long
6015 */
6016 if( end - p < 2 )
6017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6018
6019 *p++ = 0;
6020 *p++ = 48;
6021 p += 48;
6022 }
6023 else
6024#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6025#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6026 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6027 {
6028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6029 size_t len;
6030
6031 /* Write length only when we know the actual value */
6032 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6033 p + 2, end - ( p + 2 ), &len,
6034 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6035 {
6036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6037 return( ret );
6038 }
6039 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6040 p += 2 + len;
6041
6042 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6043 }
6044 else
6045#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006046#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006047 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6048 {
6049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6050 size_t zlen;
6051
6052 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6053 p + 2, end - ( p + 2 ),
6054 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6055 {
6056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6057 return( ret );
6058 }
6059
6060 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6061 p += 2 + zlen;
6062
6063 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6064 MBEDTLS_DEBUG_ECDH_Z );
6065 }
6066 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006067#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006068 {
6069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6071 }
6072
6073 /* opaque psk<0..2^16-1>; */
6074 if( end - p < 2 )
6075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6076
6077 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6078 p += 2;
6079
6080 if( end < p || (size_t)( end - p ) < psk_len )
6081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6082
6083 memcpy( p, psk, psk_len );
6084 p += psk_len;
6085
6086 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6087
6088 return( 0 );
6089}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006090#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006091
6092#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006093MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006094static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6095
6096#if defined(MBEDTLS_SSL_PROTO_DTLS)
6097int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6098{
6099 /* If renegotiation is not enforced, retransmit until we would reach max
6100 * timeout if we were using the usual handshake doubling scheme */
6101 if( ssl->conf->renego_max_records < 0 )
6102 {
6103 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6104 unsigned char doublings = 1;
6105
6106 while( ratio != 0 )
6107 {
6108 ++doublings;
6109 ratio >>= 1;
6110 }
6111
6112 if( ++ssl->renego_records_seen > doublings )
6113 {
6114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6115 return( 0 );
6116 }
6117 }
6118
6119 return( ssl_write_hello_request( ssl ) );
6120}
6121#endif
6122#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006123
Jerry Yud9526692022-02-17 14:23:47 +08006124/*
6125 * Handshake functions
6126 */
6127#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6128/* No certificate support -> dummy functions */
6129int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6130{
6131 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6132 ssl->handshake->ciphersuite_info;
6133
6134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6135
6136 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6137 {
6138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6139 ssl->state++;
6140 return( 0 );
6141 }
6142
6143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6145}
6146
6147int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6148{
6149 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6150 ssl->handshake->ciphersuite_info;
6151
6152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6153
6154 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6155 {
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6157 ssl->state++;
6158 return( 0 );
6159 }
6160
6161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6163}
6164
6165#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6166/* Some certificate support -> implement write and parse */
6167
6168int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6169{
6170 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6171 size_t i, n;
6172 const mbedtls_x509_crt *crt;
6173 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6174 ssl->handshake->ciphersuite_info;
6175
6176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6177
6178 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6179 {
6180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6181 ssl->state++;
6182 return( 0 );
6183 }
6184
6185#if defined(MBEDTLS_SSL_CLI_C)
6186 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6187 {
6188 if( ssl->handshake->client_auth == 0 )
6189 {
6190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6191 ssl->state++;
6192 return( 0 );
6193 }
6194 }
6195#endif /* MBEDTLS_SSL_CLI_C */
6196#if defined(MBEDTLS_SSL_SRV_C)
6197 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6198 {
6199 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6200 {
6201 /* Should never happen because we shouldn't have picked the
6202 * ciphersuite if we don't have a certificate. */
6203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6204 }
6205 }
6206#endif
6207
6208 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6209
6210 /*
6211 * 0 . 0 handshake type
6212 * 1 . 3 handshake length
6213 * 4 . 6 length of all certs
6214 * 7 . 9 length of cert. 1
6215 * 10 . n-1 peer certificate
6216 * n . n+2 length of cert. 2
6217 * n+3 . ... upper level cert, etc.
6218 */
6219 i = 7;
6220 crt = mbedtls_ssl_own_cert( ssl );
6221
6222 while( crt != NULL )
6223 {
6224 n = crt->raw.len;
6225 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6226 {
6227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6228 " > %" MBEDTLS_PRINTF_SIZET,
6229 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6230 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6231 }
6232
6233 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6234 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6235 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6236
6237 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6238 i += n; crt = crt->next;
6239 }
6240
6241 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6242 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6243 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6244
6245 ssl->out_msglen = i;
6246 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6247 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6248
6249 ssl->state++;
6250
6251 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6252 {
6253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6254 return( ret );
6255 }
6256
6257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6258
6259 return( ret );
6260}
6261
6262#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6263
6264#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006266static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6267 unsigned char *crt_buf,
6268 size_t crt_buf_len )
6269{
6270 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6271
6272 if( peer_crt == NULL )
6273 return( -1 );
6274
6275 if( peer_crt->raw.len != crt_buf_len )
6276 return( -1 );
6277
6278 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6279}
6280#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006281MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006282static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6283 unsigned char *crt_buf,
6284 size_t crt_buf_len )
6285{
6286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6287 unsigned char const * const peer_cert_digest =
6288 ssl->session->peer_cert_digest;
6289 mbedtls_md_type_t const peer_cert_digest_type =
6290 ssl->session->peer_cert_digest_type;
6291 mbedtls_md_info_t const * const digest_info =
6292 mbedtls_md_info_from_type( peer_cert_digest_type );
6293 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6294 size_t digest_len;
6295
6296 if( peer_cert_digest == NULL || digest_info == NULL )
6297 return( -1 );
6298
6299 digest_len = mbedtls_md_get_size( digest_info );
6300 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6301 return( -1 );
6302
6303 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6304 if( ret != 0 )
6305 return( -1 );
6306
6307 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6308}
6309#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6310#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6311
6312/*
6313 * Once the certificate message is read, parse it into a cert chain and
6314 * perform basic checks, but leave actual verification to the caller
6315 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006316MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006317static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6318 mbedtls_x509_crt *chain )
6319{
6320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6321#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6322 int crt_cnt=0;
6323#endif
6324 size_t i, n;
6325 uint8_t alert;
6326
6327 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6328 {
6329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6330 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6331 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6332 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6333 }
6334
6335 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6336 {
6337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6338 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6339 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6340 }
6341
6342 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6343 {
6344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6346 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6347 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6348 }
6349
6350 i = mbedtls_ssl_hs_hdr_len( ssl );
6351
6352 /*
6353 * Same message structure as in mbedtls_ssl_write_certificate()
6354 */
6355 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6356
6357 if( ssl->in_msg[i] != 0 ||
6358 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6359 {
6360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6362 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6363 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6364 }
6365
6366 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6367 i += 3;
6368
6369 /* Iterate through and parse the CRTs in the provided chain. */
6370 while( i < ssl->in_hslen )
6371 {
6372 /* Check that there's room for the next CRT's length fields. */
6373 if ( i + 3 > ssl->in_hslen ) {
6374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6375 mbedtls_ssl_send_alert_message( ssl,
6376 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6377 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6378 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6379 }
6380 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6381 * anything beyond 2**16 ~ 64K. */
6382 if( ssl->in_msg[i] != 0 )
6383 {
6384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6385 mbedtls_ssl_send_alert_message( ssl,
6386 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6387 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6388 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6389 }
6390
6391 /* Read length of the next CRT in the chain. */
6392 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6393 | (unsigned int) ssl->in_msg[i + 2];
6394 i += 3;
6395
6396 if( n < 128 || i + n > ssl->in_hslen )
6397 {
6398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6399 mbedtls_ssl_send_alert_message( ssl,
6400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6401 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6402 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6403 }
6404
6405 /* Check if we're handling the first CRT in the chain. */
6406#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6407 if( crt_cnt++ == 0 &&
6408 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6409 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6410 {
6411 /* During client-side renegotiation, check that the server's
6412 * end-CRTs hasn't changed compared to the initial handshake,
6413 * mitigating the triple handshake attack. On success, reuse
6414 * the original end-CRT instead of parsing it again. */
6415 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6416 if( ssl_check_peer_crt_unchanged( ssl,
6417 &ssl->in_msg[i],
6418 n ) != 0 )
6419 {
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6421 mbedtls_ssl_send_alert_message( ssl,
6422 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6423 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6424 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6425 }
6426
6427 /* Now we can safely free the original chain. */
6428 ssl_clear_peer_cert( ssl->session );
6429 }
6430#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6431
6432 /* Parse the next certificate in the chain. */
6433#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6434 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6435#else
6436 /* If we don't need to store the CRT chain permanently, parse
6437 * it in-place from the input buffer instead of making a copy. */
6438 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6439#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6440 switch( ret )
6441 {
6442 case 0: /*ok*/
6443 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6444 /* Ignore certificate with an unknown algorithm: maybe a
6445 prior certificate was already trusted. */
6446 break;
6447
6448 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6449 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6450 goto crt_parse_der_failed;
6451
6452 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6453 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6454 goto crt_parse_der_failed;
6455
6456 default:
6457 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6458 crt_parse_der_failed:
6459 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6460 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6461 return( ret );
6462 }
6463
6464 i += n;
6465 }
6466
6467 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6468 return( 0 );
6469}
6470
6471#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006472MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006473static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6474{
6475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6476 return( -1 );
6477
6478 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6479 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6480 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6481 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6482 {
Ronald Cron19385882022-06-15 16:26:13 +02006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006484 return( 0 );
6485 }
6486 return( -1 );
6487}
6488#endif /* MBEDTLS_SSL_SRV_C */
6489
6490/* Check if a certificate message is expected.
6491 * Return either
6492 * - SSL_CERTIFICATE_EXPECTED, or
6493 * - SSL_CERTIFICATE_SKIP
6494 * indicating whether a Certificate message is expected or not.
6495 */
6496#define SSL_CERTIFICATE_EXPECTED 0
6497#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006498MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006499static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6500 int authmode )
6501{
6502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6503 ssl->handshake->ciphersuite_info;
6504
6505 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6506 return( SSL_CERTIFICATE_SKIP );
6507
6508#if defined(MBEDTLS_SSL_SRV_C)
6509 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6510 {
6511 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6512 return( SSL_CERTIFICATE_SKIP );
6513
6514 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6515 {
6516 ssl->session_negotiate->verify_result =
6517 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6518 return( SSL_CERTIFICATE_SKIP );
6519 }
6520 }
6521#else
6522 ((void) authmode);
6523#endif /* MBEDTLS_SSL_SRV_C */
6524
6525 return( SSL_CERTIFICATE_EXPECTED );
6526}
6527
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006528MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006529static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6530 int authmode,
6531 mbedtls_x509_crt *chain,
6532 void *rs_ctx )
6533{
6534 int ret = 0;
6535 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6536 ssl->handshake->ciphersuite_info;
6537 int have_ca_chain = 0;
6538
6539 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6540 void *p_vrfy;
6541
6542 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6543 return( 0 );
6544
6545 if( ssl->f_vrfy != NULL )
6546 {
6547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6548 f_vrfy = ssl->f_vrfy;
6549 p_vrfy = ssl->p_vrfy;
6550 }
6551 else
6552 {
6553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6554 f_vrfy = ssl->conf->f_vrfy;
6555 p_vrfy = ssl->conf->p_vrfy;
6556 }
6557
6558 /*
6559 * Main check: verify certificate
6560 */
6561#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6562 if( ssl->conf->f_ca_cb != NULL )
6563 {
6564 ((void) rs_ctx);
6565 have_ca_chain = 1;
6566
6567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6568 ret = mbedtls_x509_crt_verify_with_ca_cb(
6569 chain,
6570 ssl->conf->f_ca_cb,
6571 ssl->conf->p_ca_cb,
6572 ssl->conf->cert_profile,
6573 ssl->hostname,
6574 &ssl->session_negotiate->verify_result,
6575 f_vrfy, p_vrfy );
6576 }
6577 else
6578#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6579 {
6580 mbedtls_x509_crt *ca_chain;
6581 mbedtls_x509_crl *ca_crl;
6582
6583#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6584 if( ssl->handshake->sni_ca_chain != NULL )
6585 {
6586 ca_chain = ssl->handshake->sni_ca_chain;
6587 ca_crl = ssl->handshake->sni_ca_crl;
6588 }
6589 else
6590#endif
6591 {
6592 ca_chain = ssl->conf->ca_chain;
6593 ca_crl = ssl->conf->ca_crl;
6594 }
6595
6596 if( ca_chain != NULL )
6597 have_ca_chain = 1;
6598
6599 ret = mbedtls_x509_crt_verify_restartable(
6600 chain,
6601 ca_chain, ca_crl,
6602 ssl->conf->cert_profile,
6603 ssl->hostname,
6604 &ssl->session_negotiate->verify_result,
6605 f_vrfy, p_vrfy, rs_ctx );
6606 }
6607
6608 if( ret != 0 )
6609 {
6610 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6611 }
6612
6613#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6614 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6615 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6616#endif
6617
6618 /*
6619 * Secondary checks: always done, but change 'ret' only if it was 0
6620 */
6621
6622#if defined(MBEDTLS_ECP_C)
6623 {
6624 const mbedtls_pk_context *pk = &chain->pk;
6625
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006626 /* If certificate uses an EC key, make sure the curve is OK.
6627 * This is a public key, so it can't be opaque, so can_do() is a good
6628 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006629 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006630 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006631 /* and in the unlikely case the above assumption no longer holds
6632 * we are making sure that pk_ec() here does not return a NULL
6633 */
6634 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6635 if( ec == NULL )
6636 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6639 }
Jerry Yud9526692022-02-17 14:23:47 +08006640
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006641 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6642 {
6643 ssl->session_negotiate->verify_result |=
6644 MBEDTLS_X509_BADCERT_BAD_KEY;
6645
6646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6647 if( ret == 0 )
6648 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6649 }
Jerry Yud9526692022-02-17 14:23:47 +08006650 }
6651 }
6652#endif /* MBEDTLS_ECP_C */
6653
6654 if( mbedtls_ssl_check_cert_usage( chain,
6655 ciphersuite_info,
6656 ! ssl->conf->endpoint,
6657 &ssl->session_negotiate->verify_result ) != 0 )
6658 {
6659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6660 if( ret == 0 )
6661 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6662 }
6663
6664 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6665 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6666 * with details encoded in the verification flags. All other kinds
6667 * of error codes, including those from the user provided f_vrfy
6668 * functions, are treated as fatal and lead to a failure of
6669 * ssl_parse_certificate even if verification was optional. */
6670 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6671 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6672 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6673 {
6674 ret = 0;
6675 }
6676
6677 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6678 {
6679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6680 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6681 }
6682
6683 if( ret != 0 )
6684 {
6685 uint8_t alert;
6686
6687 /* The certificate may have been rejected for several reasons.
6688 Pick one and send the corresponding alert. Which alert to send
6689 may be a subject of debate in some cases. */
6690 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6691 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6692 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6693 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6694 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6695 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6696 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6697 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6698 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6699 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6700 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6701 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6702 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6703 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6704 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6705 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6706 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6707 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6708 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6709 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6710 else
6711 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6713 alert );
6714 }
6715
6716#if defined(MBEDTLS_DEBUG_C)
6717 if( ssl->session_negotiate->verify_result != 0 )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6720 (unsigned int) ssl->session_negotiate->verify_result ) );
6721 }
6722 else
6723 {
6724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6725 }
6726#endif /* MBEDTLS_DEBUG_C */
6727
6728 return( ret );
6729}
6730
6731#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006732MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006733static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6734 unsigned char *start, size_t len )
6735{
6736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6737 /* Remember digest of the peer's end-CRT. */
6738 ssl->session_negotiate->peer_cert_digest =
6739 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6740 if( ssl->session_negotiate->peer_cert_digest == NULL )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6743 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6744 mbedtls_ssl_send_alert_message( ssl,
6745 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6746 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6747
6748 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6749 }
6750
6751 ret = mbedtls_md( mbedtls_md_info_from_type(
6752 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6753 start, len,
6754 ssl->session_negotiate->peer_cert_digest );
6755
6756 ssl->session_negotiate->peer_cert_digest_type =
6757 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6758 ssl->session_negotiate->peer_cert_digest_len =
6759 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6760
6761 return( ret );
6762}
6763
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006764MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006765static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6766 unsigned char *start, size_t len )
6767{
6768 unsigned char *end = start + len;
6769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6770
6771 /* Make a copy of the peer's raw public key. */
6772 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6773 ret = mbedtls_pk_parse_subpubkey( &start, end,
6774 &ssl->handshake->peer_pubkey );
6775 if( ret != 0 )
6776 {
6777 /* We should have parsed the public key before. */
6778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6779 }
6780
6781 return( 0 );
6782}
6783#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6784
6785int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6786{
6787 int ret = 0;
6788 int crt_expected;
6789#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6790 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6791 ? ssl->handshake->sni_authmode
6792 : ssl->conf->authmode;
6793#else
6794 const int authmode = ssl->conf->authmode;
6795#endif
6796 void *rs_ctx = NULL;
6797 mbedtls_x509_crt *chain = NULL;
6798
6799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6800
6801 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6802 if( crt_expected == SSL_CERTIFICATE_SKIP )
6803 {
6804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6805 goto exit;
6806 }
6807
6808#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6809 if( ssl->handshake->ecrs_enabled &&
6810 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6811 {
6812 chain = ssl->handshake->ecrs_peer_cert;
6813 ssl->handshake->ecrs_peer_cert = NULL;
6814 goto crt_verify;
6815 }
6816#endif
6817
6818 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6819 {
6820 /* mbedtls_ssl_read_record may have sent an alert already. We
6821 let it decide whether to alert. */
6822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6823 goto exit;
6824 }
6825
6826#if defined(MBEDTLS_SSL_SRV_C)
6827 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6828 {
6829 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6830
6831 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6832 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6833
6834 goto exit;
6835 }
6836#endif /* MBEDTLS_SSL_SRV_C */
6837
6838 /* Clear existing peer CRT structure in case we tried to
6839 * reuse a session but it failed, and allocate a new one. */
6840 ssl_clear_peer_cert( ssl->session_negotiate );
6841
6842 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6843 if( chain == NULL )
6844 {
6845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6846 sizeof( mbedtls_x509_crt ) ) );
6847 mbedtls_ssl_send_alert_message( ssl,
6848 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6849 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6850
6851 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6852 goto exit;
6853 }
6854 mbedtls_x509_crt_init( chain );
6855
6856 ret = ssl_parse_certificate_chain( ssl, chain );
6857 if( ret != 0 )
6858 goto exit;
6859
6860#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6861 if( ssl->handshake->ecrs_enabled)
6862 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6863
6864crt_verify:
6865 if( ssl->handshake->ecrs_enabled)
6866 rs_ctx = &ssl->handshake->ecrs_ctx;
6867#endif
6868
6869 ret = ssl_parse_certificate_verify( ssl, authmode,
6870 chain, rs_ctx );
6871 if( ret != 0 )
6872 goto exit;
6873
6874#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6875 {
6876 unsigned char *crt_start, *pk_start;
6877 size_t crt_len, pk_len;
6878
6879 /* We parse the CRT chain without copying, so
6880 * these pointers point into the input buffer,
6881 * and are hence still valid after freeing the
6882 * CRT chain. */
6883
6884 crt_start = chain->raw.p;
6885 crt_len = chain->raw.len;
6886
6887 pk_start = chain->pk_raw.p;
6888 pk_len = chain->pk_raw.len;
6889
6890 /* Free the CRT structures before computing
6891 * digest and copying the peer's public key. */
6892 mbedtls_x509_crt_free( chain );
6893 mbedtls_free( chain );
6894 chain = NULL;
6895
6896 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6897 if( ret != 0 )
6898 goto exit;
6899
6900 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6901 if( ret != 0 )
6902 goto exit;
6903 }
6904#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6905 /* Pass ownership to session structure. */
6906 ssl->session_negotiate->peer_cert = chain;
6907 chain = NULL;
6908#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6909
6910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6911
6912exit:
6913
6914 if( ret == 0 )
6915 ssl->state++;
6916
6917#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6918 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6919 {
6920 ssl->handshake->ecrs_peer_cert = chain;
6921 chain = NULL;
6922 }
6923#endif
6924
6925 if( chain != NULL )
6926 {
6927 mbedtls_x509_crt_free( chain );
6928 mbedtls_free( chain );
6929 }
6930
6931 return( ret );
6932}
6933#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6934
Andrzej Kurek25f27152022-08-17 16:09:31 -04006935#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08006936static void ssl_calc_finished_tls_sha256(
6937 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
6938{
6939 int len = 12;
6940 const char *sender;
6941 unsigned char padbuf[32];
6942#if defined(MBEDTLS_USE_PSA_CRYPTO)
6943 size_t hash_size;
6944 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
6945 psa_status_t status;
6946#else
6947 mbedtls_sha256_context sha256;
6948#endif
6949
6950 mbedtls_ssl_session *session = ssl->session_negotiate;
6951 if( !session )
6952 session = ssl->session;
6953
6954 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6955 ? "client finished"
6956 : "server finished";
6957
6958#if defined(MBEDTLS_USE_PSA_CRYPTO)
6959 sha256_psa = psa_hash_operation_init();
6960
6961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6962
6963 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6964 if( status != PSA_SUCCESS )
6965 {
6966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6967 return;
6968 }
6969
6970 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6971 if( status != PSA_SUCCESS )
6972 {
6973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6974 return;
6975 }
6976 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6977#else
6978
6979 mbedtls_sha256_init( &sha256 );
6980
6981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
6982
6983 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6984
6985 /*
6986 * TLSv1.2:
6987 * hash = PRF( master, finished_label,
6988 * Hash( handshake ) )[0.11]
6989 */
6990
6991#if !defined(MBEDTLS_SHA256_ALT)
6992 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
6993 sha256.state, sizeof( sha256.state ) );
6994#endif
6995
6996 mbedtls_sha256_finish( &sha256, padbuf );
6997 mbedtls_sha256_free( &sha256 );
6998#endif /* MBEDTLS_USE_PSA_CRYPTO */
6999
7000 ssl->handshake->tls_prf( session->master, 48, sender,
7001 padbuf, 32, buf, len );
7002
7003 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7004
7005 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7006
7007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7008}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007009#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007010
Jerry Yub7ba49e2022-02-17 14:25:53 +08007011
Andrzej Kurek25f27152022-08-17 16:09:31 -04007012#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007013static void ssl_calc_finished_tls_sha384(
7014 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7015{
7016 int len = 12;
7017 const char *sender;
7018 unsigned char padbuf[48];
7019#if defined(MBEDTLS_USE_PSA_CRYPTO)
7020 size_t hash_size;
7021 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7022 psa_status_t status;
7023#else
7024 mbedtls_sha512_context sha512;
7025#endif
7026
7027 mbedtls_ssl_session *session = ssl->session_negotiate;
7028 if( !session )
7029 session = ssl->session;
7030
7031 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7032 ? "client finished"
7033 : "server finished";
7034
7035#if defined(MBEDTLS_USE_PSA_CRYPTO)
7036 sha384_psa = psa_hash_operation_init();
7037
7038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7039
7040 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7041 if( status != PSA_SUCCESS )
7042 {
7043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7044 return;
7045 }
7046
7047 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7048 if( status != PSA_SUCCESS )
7049 {
7050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7051 return;
7052 }
7053 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7054#else
7055 mbedtls_sha512_init( &sha512 );
7056
7057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7058
Andrzej Kureka242e832022-08-11 10:03:14 -04007059 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007060
7061 /*
7062 * TLSv1.2:
7063 * hash = PRF( master, finished_label,
7064 * Hash( handshake ) )[0.11]
7065 */
7066
7067#if !defined(MBEDTLS_SHA512_ALT)
7068 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7069 sha512.state, sizeof( sha512.state ) );
7070#endif
7071 mbedtls_sha512_finish( &sha512, padbuf );
7072
7073 mbedtls_sha512_free( &sha512 );
7074#endif
7075
7076 ssl->handshake->tls_prf( session->master, 48, sender,
7077 padbuf, 48, buf, len );
7078
7079 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7080
7081 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7082
7083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7084}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007085#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007086
Jerry Yuaef00152022-02-17 14:27:31 +08007087void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7088{
7089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7090
7091 /*
7092 * Free our handshake params
7093 */
7094 mbedtls_ssl_handshake_free( ssl );
7095 mbedtls_free( ssl->handshake );
7096 ssl->handshake = NULL;
7097
7098 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007099 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007100 */
7101 if( ssl->transform )
7102 {
7103 mbedtls_ssl_transform_free( ssl->transform );
7104 mbedtls_free( ssl->transform );
7105 }
7106 ssl->transform = ssl->transform_negotiate;
7107 ssl->transform_negotiate = NULL;
7108
7109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7110}
7111
Jerry Yu2a9fff52022-02-17 14:28:51 +08007112void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7113{
7114 int resume = ssl->handshake->resume;
7115
7116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7117
7118#if defined(MBEDTLS_SSL_RENEGOTIATION)
7119 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7120 {
7121 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7122 ssl->renego_records_seen = 0;
7123 }
7124#endif
7125
7126 /*
7127 * Free the previous session and switch in the current one
7128 */
7129 if( ssl->session )
7130 {
7131#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7132 /* RFC 7366 3.1: keep the EtM state */
7133 ssl->session_negotiate->encrypt_then_mac =
7134 ssl->session->encrypt_then_mac;
7135#endif
7136
7137 mbedtls_ssl_session_free( ssl->session );
7138 mbedtls_free( ssl->session );
7139 }
7140 ssl->session = ssl->session_negotiate;
7141 ssl->session_negotiate = NULL;
7142
7143 /*
7144 * Add cache entry
7145 */
7146 if( ssl->conf->f_set_cache != NULL &&
7147 ssl->session->id_len != 0 &&
7148 resume == 0 )
7149 {
7150 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7151 ssl->session->id,
7152 ssl->session->id_len,
7153 ssl->session ) != 0 )
7154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7155 }
7156
7157#if defined(MBEDTLS_SSL_PROTO_DTLS)
7158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7159 ssl->handshake->flight != NULL )
7160 {
7161 /* Cancel handshake timer */
7162 mbedtls_ssl_set_timer( ssl, 0 );
7163
7164 /* Keep last flight around in case we need to resend it:
7165 * we need the handshake and transform structures for that */
7166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7167 }
7168 else
7169#endif
7170 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7171
7172 ssl->state++;
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7175}
7176
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007177int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7178{
7179 int ret, hash_len;
7180
7181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7182
7183 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7184
7185 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7186
7187 /*
7188 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7189 * may define some other value. Currently (early 2016), no defined
7190 * ciphersuite does this (and this is unlikely to change as activity has
7191 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7192 */
7193 hash_len = 12;
7194
7195#if defined(MBEDTLS_SSL_RENEGOTIATION)
7196 ssl->verify_data_len = hash_len;
7197 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7198#endif
7199
7200 ssl->out_msglen = 4 + hash_len;
7201 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7202 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7203
7204 /*
7205 * In case of session resuming, invert the client and server
7206 * ChangeCipherSpec messages order.
7207 */
7208 if( ssl->handshake->resume != 0 )
7209 {
7210#if defined(MBEDTLS_SSL_CLI_C)
7211 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7212 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7213#endif
7214#if defined(MBEDTLS_SSL_SRV_C)
7215 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7216 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7217#endif
7218 }
7219 else
7220 ssl->state++;
7221
7222 /*
7223 * Switch to our negotiated transform and session parameters for outbound
7224 * data.
7225 */
7226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7227
7228#if defined(MBEDTLS_SSL_PROTO_DTLS)
7229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7230 {
7231 unsigned char i;
7232
7233 /* Remember current epoch settings for resending */
7234 ssl->handshake->alt_transform_out = ssl->transform_out;
7235 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7236 sizeof( ssl->handshake->alt_out_ctr ) );
7237
7238 /* Set sequence_number to zero */
7239 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7240
7241
7242 /* Increment epoch */
7243 for( i = 2; i > 0; i-- )
7244 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7245 break;
7246
7247 /* The loop goes to its end iff the counter is wrapping */
7248 if( i == 0 )
7249 {
7250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7251 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7252 }
7253 }
7254 else
7255#endif /* MBEDTLS_SSL_PROTO_DTLS */
7256 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7257
7258 ssl->transform_out = ssl->transform_negotiate;
7259 ssl->session_out = ssl->session_negotiate;
7260
7261#if defined(MBEDTLS_SSL_PROTO_DTLS)
7262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7263 mbedtls_ssl_send_flight_completed( ssl );
7264#endif
7265
7266 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7267 {
7268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7269 return( ret );
7270 }
7271
7272#if defined(MBEDTLS_SSL_PROTO_DTLS)
7273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7274 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7275 {
7276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7277 return( ret );
7278 }
7279#endif
7280
7281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7282
7283 return( 0 );
7284}
7285
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007286#define SSL_MAX_HASH_LEN 12
7287
7288int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7289{
7290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7291 unsigned int hash_len = 12;
7292 unsigned char buf[SSL_MAX_HASH_LEN];
7293
7294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7295
7296 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7297
7298 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7299 {
7300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7301 goto exit;
7302 }
7303
7304 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7305 {
7306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7308 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7309 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7310 goto exit;
7311 }
7312
7313 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7314 {
7315 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7316 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7317 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7318 goto exit;
7319 }
7320
7321 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7322 {
7323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7325 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7326 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7327 goto exit;
7328 }
7329
7330 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7331 buf, hash_len ) != 0 )
7332 {
7333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7335 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7336 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7337 goto exit;
7338 }
7339
7340#if defined(MBEDTLS_SSL_RENEGOTIATION)
7341 ssl->verify_data_len = hash_len;
7342 memcpy( ssl->peer_verify_data, buf, hash_len );
7343#endif
7344
7345 if( ssl->handshake->resume != 0 )
7346 {
7347#if defined(MBEDTLS_SSL_CLI_C)
7348 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7349 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7350#endif
7351#if defined(MBEDTLS_SSL_SRV_C)
7352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7353 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7354#endif
7355 }
7356 else
7357 ssl->state++;
7358
7359#if defined(MBEDTLS_SSL_PROTO_DTLS)
7360 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7361 mbedtls_ssl_recv_flight_completed( ssl );
7362#endif
7363
7364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7365
7366exit:
7367 mbedtls_platform_zeroize( buf, hash_len );
7368 return( ret );
7369}
7370
Jerry Yu392112c2022-02-17 14:34:10 +08007371#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7372/*
7373 * Helper to get TLS 1.2 PRF from ciphersuite
7374 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7375 */
7376static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7377{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007378#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007379 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7380 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7381
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007382 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007383 return( tls_prf_sha384 );
7384#else
7385 (void) ciphersuite_id;
7386#endif
7387 return( tls_prf_sha256 );
7388}
7389#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007390
7391static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7392{
7393 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007394#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007395 if( tls_prf == tls_prf_sha384 )
7396 {
7397 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7398 }
7399 else
7400#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007401#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007402 if( tls_prf == tls_prf_sha256 )
7403 {
7404 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7405 }
7406 else
7407#endif
7408 return( MBEDTLS_SSL_TLS_PRF_NONE );
7409}
7410
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007411/*
7412 * Populate a transform structure with session keys and all the other
7413 * necessary information.
7414 *
7415 * Parameters:
7416 * - [in/out]: transform: structure to populate
7417 * [in] must be just initialised with mbedtls_ssl_transform_init()
7418 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7419 * - [in] ciphersuite
7420 * - [in] master
7421 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007422 * - [in] tls_prf: pointer to PRF to use for key derivation
7423 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007424 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007425 * - [in] endpoint: client or server
7426 * - [in] ssl: used for:
7427 * - ssl->conf->{f,p}_export_keys
7428 * [in] optionally used for:
7429 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7430 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007432static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7433 int ciphersuite,
7434 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007435#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007436 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007437#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007438 ssl_tls_prf_t tls_prf,
7439 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007440 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007441 unsigned endpoint,
7442 const mbedtls_ssl_context *ssl )
7443{
7444 int ret = 0;
7445 unsigned char keyblk[256];
7446 unsigned char *key1;
7447 unsigned char *key2;
7448 unsigned char *mac_enc;
7449 unsigned char *mac_dec;
7450 size_t mac_key_len = 0;
7451 size_t iv_copy_len;
7452 size_t keylen;
7453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007454 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007455#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007456 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007457 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007458#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007459
7460#if defined(MBEDTLS_USE_PSA_CRYPTO)
7461 psa_key_type_t key_type;
7462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7463 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007464 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007465 size_t key_bits;
7466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7467#endif
7468
7469#if !defined(MBEDTLS_DEBUG_C) && \
7470 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7471 if( ssl->f_export_keys == NULL )
7472 {
7473 ssl = NULL; /* make sure we don't use it except for these cases */
7474 (void) ssl;
7475 }
7476#endif
7477
7478 /*
7479 * Some data just needs copying into the structure
7480 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007481#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007482 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007483#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007484 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007485
7486#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7487 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7488#endif
7489
7490#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007491 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007492 {
7493 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7494 * generation separate. This should never happen. */
7495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7496 }
7497#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7498
7499 /*
7500 * Get various info structures
7501 */
7502 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7503 if( ciphersuite_info == NULL )
7504 {
7505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7506 ciphersuite ) );
7507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7508 }
7509
Neil Armstrongab555e02022-04-04 11:07:59 +02007510 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007511#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007512 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007513#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007514 ciphersuite_info );
7515
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007516 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7517 transform->taglen =
7518 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7519
7520#if defined(MBEDTLS_USE_PSA_CRYPTO)
7521 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7522 transform->taglen,
7523 &alg,
7524 &key_type,
7525 &key_bits ) ) != PSA_SUCCESS )
7526 {
7527 ret = psa_ssl_status_to_mbedtls( status );
7528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7529 goto end;
7530 }
7531#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007532 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7533 if( cipher_info == NULL )
7534 {
7535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7536 ciphersuite_info->cipher ) );
7537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7538 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007540
Neil Armstronge4512952022-03-08 09:08:22 +01007541#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007542 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007543 if( mac_alg == 0 )
7544 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007546 (unsigned) ciphersuite_info->mac ) );
7547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7548 }
7549#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7551 if( md_info == NULL )
7552 {
7553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7554 (unsigned) ciphersuite_info->mac ) );
7555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7556 }
Neil Armstronge4512952022-03-08 09:08:22 +01007557#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007558
7559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7560 /* Copy own and peer's CID if the use of the CID
7561 * extension has been negotiated. */
7562 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7563 {
7564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7565
7566 transform->in_cid_len = ssl->own_cid_len;
7567 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7568 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7569 transform->in_cid_len );
7570
7571 transform->out_cid_len = ssl->handshake->peer_cid_len;
7572 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7573 ssl->handshake->peer_cid_len );
7574 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7575 transform->out_cid_len );
7576 }
7577#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7578
7579 /*
7580 * Compute key block using the PRF
7581 */
7582 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7583 if( ret != 0 )
7584 {
7585 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7586 return( ret );
7587 }
7588
7589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7590 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7591 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7592 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7593 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7594
7595 /*
7596 * Determine the appropriate key, IV and MAC length.
7597 */
7598
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007599#if defined(MBEDTLS_USE_PSA_CRYPTO)
7600 keylen = PSA_BITS_TO_BYTES(key_bits);
7601#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007602 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007603#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007604
7605#if defined(MBEDTLS_GCM_C) || \
7606 defined(MBEDTLS_CCM_C) || \
7607 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007608 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007609 {
7610 size_t explicit_ivlen;
7611
7612 transform->maclen = 0;
7613 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007614
7615 /* All modes haves 96-bit IVs, but the length of the static parts vary
7616 * with mode and version:
7617 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7618 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7619 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7620 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7621 * sequence number).
7622 */
7623 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007624#if defined(MBEDTLS_USE_PSA_CRYPTO)
7625 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7626#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007627 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007628#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007629 transform->fixed_ivlen = 12;
7630 else
7631 transform->fixed_ivlen = 4;
7632
7633 /* Minimum length of encrypted record */
7634 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7635 transform->minlen = explicit_ivlen + transform->taglen;
7636 }
7637 else
7638#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7639#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007640 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7641 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7642 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007643 {
Neil Armstronge4512952022-03-08 09:08:22 +01007644#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007645 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007646#else
7647 size_t block_size = cipher_info->block_size;
7648#endif /* MBEDTLS_USE_PSA_CRYPTO */
7649
7650#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007651 /* Get MAC length */
7652 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7653#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007654 /* Initialize HMAC contexts */
7655 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7656 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7657 {
7658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7659 goto end;
7660 }
7661
7662 /* Get MAC length */
7663 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007664#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007665 transform->maclen = mac_key_len;
7666
7667 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007668#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007669 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007670#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007671 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007672#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007673
7674 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007675 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007676 transform->minlen = transform->maclen;
7677 else
7678 {
7679 /*
7680 * GenericBlockCipher:
7681 * 1. if EtM is in use: one block plus MAC
7682 * otherwise: * first multiple of blocklen greater than maclen
7683 * 2. IV
7684 */
7685#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007686 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007687 {
7688 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007689 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007690 }
7691 else
7692#endif
7693 {
7694 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007695 + block_size
7696 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007697 }
7698
Glenn Strauss07c64162022-03-14 12:34:51 -04007699 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700 {
7701 transform->minlen += transform->ivlen;
7702 }
7703 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007704 {
7705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7706 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7707 goto end;
7708 }
7709 }
7710 }
7711 else
7712#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7713 {
7714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7716 }
7717
7718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7719 (unsigned) keylen,
7720 (unsigned) transform->minlen,
7721 (unsigned) transform->ivlen,
7722 (unsigned) transform->maclen ) );
7723
7724 /*
7725 * Finally setup the cipher contexts, IVs and MAC secrets.
7726 */
7727#if defined(MBEDTLS_SSL_CLI_C)
7728 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7729 {
7730 key1 = keyblk + mac_key_len * 2;
7731 key2 = keyblk + mac_key_len * 2 + keylen;
7732
7733 mac_enc = keyblk;
7734 mac_dec = keyblk + mac_key_len;
7735
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007736 iv_copy_len = ( transform->fixed_ivlen ) ?
7737 transform->fixed_ivlen : transform->ivlen;
7738 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7739 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7740 iv_copy_len );
7741 }
7742 else
7743#endif /* MBEDTLS_SSL_CLI_C */
7744#if defined(MBEDTLS_SSL_SRV_C)
7745 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7746 {
7747 key1 = keyblk + mac_key_len * 2 + keylen;
7748 key2 = keyblk + mac_key_len * 2;
7749
7750 mac_enc = keyblk + mac_key_len;
7751 mac_dec = keyblk;
7752
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007753 iv_copy_len = ( transform->fixed_ivlen ) ?
7754 transform->fixed_ivlen : transform->ivlen;
7755 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7756 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7757 iv_copy_len );
7758 }
7759 else
7760#endif /* MBEDTLS_SSL_SRV_C */
7761 {
7762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7763 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7764 goto end;
7765 }
7766
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007767 if( ssl != NULL && ssl->f_export_keys != NULL )
7768 {
7769 ssl->f_export_keys( ssl->p_export_keys,
7770 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7771 master, 48,
7772 randbytes + 32,
7773 randbytes,
7774 tls_prf_get_type( tls_prf ) );
7775 }
7776
7777#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007778 transform->psa_alg = alg;
7779
7780 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7781 {
7782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7783 psa_set_key_algorithm( &attributes, alg );
7784 psa_set_key_type( &attributes, key_type );
7785
7786 if( ( status = psa_import_key( &attributes,
7787 key1,
7788 PSA_BITS_TO_BYTES( key_bits ),
7789 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7790 {
7791 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7792 ret = psa_ssl_status_to_mbedtls( status );
7793 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7794 goto end;
7795 }
7796
7797 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7798
7799 if( ( status = psa_import_key( &attributes,
7800 key2,
7801 PSA_BITS_TO_BYTES( key_bits ),
7802 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7803 {
7804 ret = psa_ssl_status_to_mbedtls( status );
7805 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7806 goto end;
7807 }
7808 }
7809#else
7810 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7811 cipher_info ) ) != 0 )
7812 {
7813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7814 goto end;
7815 }
7816
7817 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7818 cipher_info ) ) != 0 )
7819 {
7820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7821 goto end;
7822 }
7823
7824 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7825 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7826 MBEDTLS_ENCRYPT ) ) != 0 )
7827 {
7828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7829 goto end;
7830 }
7831
7832 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7833 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7834 MBEDTLS_DECRYPT ) ) != 0 )
7835 {
7836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7837 goto end;
7838 }
7839
7840#if defined(MBEDTLS_CIPHER_MODE_CBC)
7841 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7842 {
7843 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7844 MBEDTLS_PADDING_NONE ) ) != 0 )
7845 {
7846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7847 goto end;
7848 }
7849
7850 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7851 MBEDTLS_PADDING_NONE ) ) != 0 )
7852 {
7853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7854 goto end;
7855 }
7856 }
7857#endif /* MBEDTLS_CIPHER_MODE_CBC */
7858#endif /* MBEDTLS_USE_PSA_CRYPTO */
7859
Neil Armstrong29c0c042022-03-17 17:47:28 +01007860#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7861 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7862 For AEAD-based ciphersuites, there is nothing to do here. */
7863 if( mac_key_len != 0 )
7864 {
7865#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007866 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007867
7868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007869 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007870 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7871
7872 if( ( status = psa_import_key( &attributes,
7873 mac_enc, mac_key_len,
7874 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7875 {
7876 ret = psa_ssl_status_to_mbedtls( status );
7877 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7878 goto end;
7879 }
7880
Ronald Cronfb39f152022-03-25 14:36:28 +01007881 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007882 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7883#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7884 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7885#endif
7886 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007887 /* mbedtls_ct_hmac() requires the key to be exportable */
7888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7889 PSA_KEY_USAGE_VERIFY_HASH );
7890 else
7891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7892
7893 if( ( status = psa_import_key( &attributes,
7894 mac_dec, mac_key_len,
7895 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7896 {
7897 ret = psa_ssl_status_to_mbedtls( status );
7898 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7899 goto end;
7900 }
7901#else
7902 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7903 if( ret != 0 )
7904 goto end;
7905 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7906 if( ret != 0 )
7907 goto end;
7908#endif /* MBEDTLS_USE_PSA_CRYPTO */
7909 }
7910#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7911
7912 ((void) mac_dec);
7913 ((void) mac_enc);
7914
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007915end:
7916 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7917 return( ret );
7918}
7919
Jerry Yuee40f9d2022-02-17 14:55:16 +08007920#if defined(MBEDTLS_USE_PSA_CRYPTO)
7921int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7922 unsigned char *hash, size_t *hashlen,
7923 unsigned char *data, size_t data_len,
7924 mbedtls_md_type_t md_alg )
7925{
7926 psa_status_t status;
7927 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007928 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007929
7930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7931
7932 if( ( status = psa_hash_setup( &hash_operation,
7933 hash_alg ) ) != PSA_SUCCESS )
7934 {
7935 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
7936 goto exit;
7937 }
7938
7939 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
7940 64 ) ) != PSA_SUCCESS )
7941 {
7942 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7943 goto exit;
7944 }
7945
7946 if( ( status = psa_hash_update( &hash_operation,
7947 data, data_len ) ) != PSA_SUCCESS )
7948 {
7949 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
7950 goto exit;
7951 }
7952
7953 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
7954 hashlen ) ) != PSA_SUCCESS )
7955 {
7956 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
7957 goto exit;
7958 }
7959
7960exit:
7961 if( status != PSA_SUCCESS )
7962 {
7963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7964 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7965 switch( status )
7966 {
7967 case PSA_ERROR_NOT_SUPPORTED:
7968 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
7969 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
7970 case PSA_ERROR_BUFFER_TOO_SMALL:
7971 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
7972 case PSA_ERROR_INSUFFICIENT_MEMORY:
7973 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
7974 default:
7975 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
7976 }
7977 }
7978 return( 0 );
7979}
7980
7981#else
7982
7983int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7984 unsigned char *hash, size_t *hashlen,
7985 unsigned char *data, size_t data_len,
7986 mbedtls_md_type_t md_alg )
7987{
7988 int ret = 0;
7989 mbedtls_md_context_t ctx;
7990 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
7991 *hashlen = mbedtls_md_get_size( md_info );
7992
7993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
7994
7995 mbedtls_md_init( &ctx );
7996
7997 /*
7998 * digitally-signed struct {
7999 * opaque client_random[32];
8000 * opaque server_random[32];
8001 * ServerDHParams params;
8002 * };
8003 */
8004 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8005 {
8006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8007 goto exit;
8008 }
8009 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8010 {
8011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8012 goto exit;
8013 }
8014 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8015 {
8016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8017 goto exit;
8018 }
8019 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8020 {
8021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8022 goto exit;
8023 }
8024 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8025 {
8026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8027 goto exit;
8028 }
8029
8030exit:
8031 mbedtls_md_free( &ctx );
8032
8033 if( ret != 0 )
8034 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8035 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8036
8037 return( ret );
8038}
8039#endif /* MBEDTLS_USE_PSA_CRYPTO */
8040
Jerry Yud9d91da2022-02-17 14:57:06 +08008041#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8042
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008043/* Find the preferred hash for a given signature algorithm. */
8044unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8045 mbedtls_ssl_context *ssl,
8046 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008047{
Gabor Mezei078e8032022-04-27 21:17:56 +02008048 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008049 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008050
8051 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008052 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008053
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008054 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008055 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008056 unsigned int hash_alg_received =
8057 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8058 received_sig_algs[i] );
8059 unsigned int sig_alg_received =
8060 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8061 received_sig_algs[i] );
8062
8063 if( sig_alg == sig_alg_received )
8064 {
8065#if defined(MBEDTLS_USE_PSA_CRYPTO)
8066 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8067 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008068 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008069 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008070
Neil Armstrong96eceb82022-06-30 18:05:05 +02008071 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8072 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8073 PSA_ALG_ECDSA( psa_hash_alg ),
8074 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008075 continue;
8076
Neil Armstrong96eceb82022-06-30 18:05:05 +02008077 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008078 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8079 PSA_ALG_RSA_PKCS1V15_SIGN(
8080 psa_hash_alg ),
8081 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008082 continue;
8083 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008084#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008085
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008086 return( hash_alg_received );
8087 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008088 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008089
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008090 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008091}
8092
8093#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8094
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008095/* Serialization of TLS 1.2 sessions:
8096 *
8097 * struct {
8098 * uint64 start_time;
8099 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008100 * uint8 session_id_len; // at most 32
8101 * opaque session_id[32];
8102 * opaque master[48]; // fixed length in the standard
8103 * uint32 verify_result;
8104 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8105 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8106 * uint32 ticket_lifetime;
8107 * uint8 mfl_code; // up to 255 according to standard
8108 * uint8 encrypt_then_mac; // 0 or 1
8109 * } serialized_session_tls12;
8110 *
8111 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008112static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008113 unsigned char *buf,
8114 size_t buf_len )
8115{
8116 unsigned char *p = buf;
8117 size_t used = 0;
8118
8119#if defined(MBEDTLS_HAVE_TIME)
8120 uint64_t start;
8121#endif
8122#if defined(MBEDTLS_X509_CRT_PARSE_C)
8123#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8124 size_t cert_len;
8125#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8126#endif /* MBEDTLS_X509_CRT_PARSE_C */
8127
8128 /*
8129 * Time
8130 */
8131#if defined(MBEDTLS_HAVE_TIME)
8132 used += 8;
8133
8134 if( used <= buf_len )
8135 {
8136 start = (uint64_t) session->start;
8137
8138 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8139 p += 8;
8140 }
8141#endif /* MBEDTLS_HAVE_TIME */
8142
8143 /*
8144 * Basic mandatory fields
8145 */
8146 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008147 + 1 /* id_len */
8148 + sizeof( session->id )
8149 + sizeof( session->master )
8150 + 4; /* verify_result */
8151
8152 if( used <= buf_len )
8153 {
8154 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8155 p += 2;
8156
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008157 *p++ = MBEDTLS_BYTE_0( session->id_len );
8158 memcpy( p, session->id, 32 );
8159 p += 32;
8160
8161 memcpy( p, session->master, 48 );
8162 p += 48;
8163
8164 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8165 p += 4;
8166 }
8167
8168 /*
8169 * Peer's end-entity certificate
8170 */
8171#if defined(MBEDTLS_X509_CRT_PARSE_C)
8172#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8173 if( session->peer_cert == NULL )
8174 cert_len = 0;
8175 else
8176 cert_len = session->peer_cert->raw.len;
8177
8178 used += 3 + cert_len;
8179
8180 if( used <= buf_len )
8181 {
8182 *p++ = MBEDTLS_BYTE_2( cert_len );
8183 *p++ = MBEDTLS_BYTE_1( cert_len );
8184 *p++ = MBEDTLS_BYTE_0( cert_len );
8185
8186 if( session->peer_cert != NULL )
8187 {
8188 memcpy( p, session->peer_cert->raw.p, cert_len );
8189 p += cert_len;
8190 }
8191 }
8192#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8193 if( session->peer_cert_digest != NULL )
8194 {
8195 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8196 if( used <= buf_len )
8197 {
8198 *p++ = (unsigned char) session->peer_cert_digest_type;
8199 *p++ = (unsigned char) session->peer_cert_digest_len;
8200 memcpy( p, session->peer_cert_digest,
8201 session->peer_cert_digest_len );
8202 p += session->peer_cert_digest_len;
8203 }
8204 }
8205 else
8206 {
8207 used += 2;
8208 if( used <= buf_len )
8209 {
8210 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8211 *p++ = 0;
8212 }
8213 }
8214#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8215#endif /* MBEDTLS_X509_CRT_PARSE_C */
8216
8217 /*
8218 * Session ticket if any, plus associated data
8219 */
8220#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8221 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8222
8223 if( used <= buf_len )
8224 {
8225 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8226 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8227 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8228
8229 if( session->ticket != NULL )
8230 {
8231 memcpy( p, session->ticket, session->ticket_len );
8232 p += session->ticket_len;
8233 }
8234
8235 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8236 p += 4;
8237 }
8238#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8239
8240 /*
8241 * Misc extension-related info
8242 */
8243#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8244 used += 1;
8245
8246 if( used <= buf_len )
8247 *p++ = session->mfl_code;
8248#endif
8249
8250#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8251 used += 1;
8252
8253 if( used <= buf_len )
8254 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8255#endif
8256
8257 return( used );
8258}
8259
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008260MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008261static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008262 const unsigned char *buf,
8263 size_t len )
8264{
8265#if defined(MBEDTLS_HAVE_TIME)
8266 uint64_t start;
8267#endif
8268#if defined(MBEDTLS_X509_CRT_PARSE_C)
8269#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8270 size_t cert_len;
8271#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8272#endif /* MBEDTLS_X509_CRT_PARSE_C */
8273
8274 const unsigned char *p = buf;
8275 const unsigned char * const end = buf + len;
8276
8277 /*
8278 * Time
8279 */
8280#if defined(MBEDTLS_HAVE_TIME)
8281 if( 8 > (size_t)( end - p ) )
8282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8283
8284 start = ( (uint64_t) p[0] << 56 ) |
8285 ( (uint64_t) p[1] << 48 ) |
8286 ( (uint64_t) p[2] << 40 ) |
8287 ( (uint64_t) p[3] << 32 ) |
8288 ( (uint64_t) p[4] << 24 ) |
8289 ( (uint64_t) p[5] << 16 ) |
8290 ( (uint64_t) p[6] << 8 ) |
8291 ( (uint64_t) p[7] );
8292 p += 8;
8293
8294 session->start = (time_t) start;
8295#endif /* MBEDTLS_HAVE_TIME */
8296
8297 /*
8298 * Basic mandatory fields
8299 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008300 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302
8303 session->ciphersuite = ( p[0] << 8 ) | p[1];
8304 p += 2;
8305
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008306 session->id_len = *p++;
8307 memcpy( session->id, p, 32 );
8308 p += 32;
8309
8310 memcpy( session->master, p, 48 );
8311 p += 48;
8312
8313 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8314 ( (uint32_t) p[1] << 16 ) |
8315 ( (uint32_t) p[2] << 8 ) |
8316 ( (uint32_t) p[3] );
8317 p += 4;
8318
8319 /* Immediately clear invalid pointer values that have been read, in case
8320 * we exit early before we replaced them with valid ones. */
8321#if defined(MBEDTLS_X509_CRT_PARSE_C)
8322#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8323 session->peer_cert = NULL;
8324#else
8325 session->peer_cert_digest = NULL;
8326#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8327#endif /* MBEDTLS_X509_CRT_PARSE_C */
8328#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8329 session->ticket = NULL;
8330#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8331
8332 /*
8333 * Peer certificate
8334 */
8335#if defined(MBEDTLS_X509_CRT_PARSE_C)
8336#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8337 /* Deserialize CRT from the end of the ticket. */
8338 if( 3 > (size_t)( end - p ) )
8339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8340
8341 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8342 p += 3;
8343
8344 if( cert_len != 0 )
8345 {
8346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8347
8348 if( cert_len > (size_t)( end - p ) )
8349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8350
8351 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8352
8353 if( session->peer_cert == NULL )
8354 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8355
8356 mbedtls_x509_crt_init( session->peer_cert );
8357
8358 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8359 p, cert_len ) ) != 0 )
8360 {
8361 mbedtls_x509_crt_free( session->peer_cert );
8362 mbedtls_free( session->peer_cert );
8363 session->peer_cert = NULL;
8364 return( ret );
8365 }
8366
8367 p += cert_len;
8368 }
8369#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8370 /* Deserialize CRT digest from the end of the ticket. */
8371 if( 2 > (size_t)( end - p ) )
8372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8373
8374 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8375 session->peer_cert_digest_len = (size_t) *p++;
8376
8377 if( session->peer_cert_digest_len != 0 )
8378 {
8379 const mbedtls_md_info_t *md_info =
8380 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8381 if( md_info == NULL )
8382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8383 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8385
8386 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8388
8389 session->peer_cert_digest =
8390 mbedtls_calloc( 1, session->peer_cert_digest_len );
8391 if( session->peer_cert_digest == NULL )
8392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8393
8394 memcpy( session->peer_cert_digest, p,
8395 session->peer_cert_digest_len );
8396 p += session->peer_cert_digest_len;
8397 }
8398#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8399#endif /* MBEDTLS_X509_CRT_PARSE_C */
8400
8401 /*
8402 * Session ticket and associated data
8403 */
8404#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8405 if( 3 > (size_t)( end - p ) )
8406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8407
8408 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8409 p += 3;
8410
8411 if( session->ticket_len != 0 )
8412 {
8413 if( session->ticket_len > (size_t)( end - p ) )
8414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8415
8416 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8417 if( session->ticket == NULL )
8418 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8419
8420 memcpy( session->ticket, p, session->ticket_len );
8421 p += session->ticket_len;
8422 }
8423
8424 if( 4 > (size_t)( end - p ) )
8425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8426
8427 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8428 ( (uint32_t) p[1] << 16 ) |
8429 ( (uint32_t) p[2] << 8 ) |
8430 ( (uint32_t) p[3] );
8431 p += 4;
8432#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8433
8434 /*
8435 * Misc extension-related info
8436 */
8437#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8438 if( 1 > (size_t)( end - p ) )
8439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8440
8441 session->mfl_code = *p++;
8442#endif
8443
8444#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8445 if( 1 > (size_t)( end - p ) )
8446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8447
8448 session->encrypt_then_mac = *p++;
8449#endif
8450
8451 /* Done, should have consumed entire buffer */
8452 if( p != end )
8453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8454
8455 return( 0 );
8456}
Jerry Yudc7bd172022-02-17 13:44:15 +08008457#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8458
XiaokangQian75d40ef2022-04-20 11:05:24 +00008459int mbedtls_ssl_validate_ciphersuite(
8460 const mbedtls_ssl_context *ssl,
8461 const mbedtls_ssl_ciphersuite_t *suite_info,
8462 mbedtls_ssl_protocol_version min_tls_version,
8463 mbedtls_ssl_protocol_version max_tls_version )
8464{
8465 (void) ssl;
8466
8467 if( suite_info == NULL )
8468 return( -1 );
8469
8470 if( ( suite_info->min_tls_version > max_tls_version ) ||
8471 ( suite_info->max_tls_version < min_tls_version ) )
8472 {
8473 return( -1 );
8474 }
8475
XiaokangQian060d8672022-04-21 09:24:56 +00008476#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008477#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8478 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8479 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8480 {
8481 return( -1 );
8482 }
8483#endif
8484
8485 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8486#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8487 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8488 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8489 {
8490 return( -1 );
8491 }
8492#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8494
8495 return( 0 );
8496}
8497
XiaokangQianeaf36512022-04-24 09:07:44 +00008498#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8499/*
8500 * Function for writing a signature algorithm extension.
8501 *
8502 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8503 * value (TLS 1.3 RFC8446):
8504 * enum {
8505 * ....
8506 * ecdsa_secp256r1_sha256( 0x0403 ),
8507 * ecdsa_secp384r1_sha384( 0x0503 ),
8508 * ecdsa_secp521r1_sha512( 0x0603 ),
8509 * ....
8510 * } SignatureScheme;
8511 *
8512 * struct {
8513 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8514 * } SignatureSchemeList;
8515 *
8516 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8517 * value (TLS 1.2 RFC5246):
8518 * enum {
8519 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8520 * sha512(6), (255)
8521 * } HashAlgorithm;
8522 *
8523 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8524 * SignatureAlgorithm;
8525 *
8526 * struct {
8527 * HashAlgorithm hash;
8528 * SignatureAlgorithm signature;
8529 * } SignatureAndHashAlgorithm;
8530 *
8531 * SignatureAndHashAlgorithm
8532 * supported_signature_algorithms<2..2^16-2>;
8533 *
8534 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8535 * generalization of the TLS 1.2 signature algorithm extension.
8536 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8537 * `SignatureScheme` field of TLS 1.3
8538 *
8539 */
8540int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8541 const unsigned char *end, size_t *out_len )
8542{
8543 unsigned char *p = buf;
8544 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8545 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8546
8547 *out_len = 0;
8548
8549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8550
8551 /* Check if we have space for header and length field:
8552 * - extension_type (2 bytes)
8553 * - extension_data_length (2 bytes)
8554 * - supported_signature_algorithms_length (2 bytes)
8555 */
8556 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8557 p += 6;
8558
8559 /*
8560 * Write supported_signature_algorithms
8561 */
8562 supported_sig_alg = p;
8563 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8564 if( sig_alg == NULL )
8565 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8566
8567 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8568 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8570 *sig_alg,
8571 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008572 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8573 continue;
8574 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8575 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8576 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008578 *sig_alg,
8579 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008580 }
8581
8582 /* Length of supported_signature_algorithms */
8583 supported_sig_alg_len = p - supported_sig_alg;
8584 if( supported_sig_alg_len == 0 )
8585 {
8586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8587 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8588 }
8589
XiaokangQianeaf36512022-04-24 09:07:44 +00008590 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008591 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008592 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8593
XiaokangQianeaf36512022-04-24 09:07:44 +00008594 *out_len = p - buf;
8595
8596#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8597 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8598#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8599 return( 0 );
8600}
8601#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8602
XiaokangQian40a35232022-05-07 09:02:40 +00008603#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008604/*
8605 * mbedtls_ssl_parse_server_name_ext
8606 *
8607 * Structure of server_name extension:
8608 *
8609 * enum {
8610 * host_name(0), (255)
8611 * } NameType;
8612 * opaque HostName<1..2^16-1>;
8613 *
8614 * struct {
8615 * NameType name_type;
8616 * select (name_type) {
8617 * case host_name: HostName;
8618 * } name;
8619 * } ServerName;
8620 * struct {
8621 * ServerName server_name_list<1..2^16-1>
8622 * } ServerNameList;
8623 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008624MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008625int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8626 const unsigned char *buf,
8627 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008628{
8629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8630 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008631 size_t server_name_list_len, hostname_len;
8632 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008633
XiaokangQianf2a94202022-05-20 06:44:24 +00008634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008635
8636 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008637 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008638 p += 2;
8639
XiaokangQian9b2b7712022-05-17 02:57:00 +00008640 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8641 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008642 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008643 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008644 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008645 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008646 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8647 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008648
8649 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8650 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008651 /* sni_name is intended to be used only during the parsing of the
8652 * ClientHello message (it is reset to NULL before the end of
8653 * the message parsing). Thus it is ok to just point to the
8654 * reception buffer and not make a copy of it.
8655 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008656 ssl->handshake->sni_name = p + 3;
8657 ssl->handshake->sni_name_len = hostname_len;
8658 if( ssl->conf->f_sni == NULL )
8659 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008660 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008661 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008662 if( ret != 0 )
8663 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008664 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008665 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8666 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008667 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8668 }
8669 return( 0 );
8670 }
8671
8672 p += hostname_len + 3;
8673 }
8674
8675 return( 0 );
8676}
8677#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8678
XiaokangQianacb39922022-06-17 10:18:48 +00008679#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008680MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008681int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8682 const unsigned char *buf,
8683 const unsigned char *end )
8684{
8685 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008686 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008687 const unsigned char *protocol_name_list;
8688 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008689 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008690
8691 /* If ALPN not configured, just ignore the extension */
8692 if( ssl->conf->alpn_list == NULL )
8693 return( 0 );
8694
8695 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008696 * RFC7301, section 3.1
8697 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008698 *
XiaokangQianc7403452022-06-23 03:24:12 +00008699 * struct {
8700 * ProtocolName protocol_name_list<2..2^16-1>
8701 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008702 */
8703
XiaokangQianc7403452022-06-23 03:24:12 +00008704 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008705 * protocol_name_list_len 2 bytes
8706 * protocol_name_len 1 bytes
8707 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008708 */
XiaokangQianacb39922022-06-17 10:18:48 +00008709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8710
XiaokangQianc7403452022-06-23 03:24:12 +00008711 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008712 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008713 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008714 protocol_name_list = p;
8715 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008716
8717 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008718 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008719 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008720 protocol_name_len = *p++;
8721 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8722 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008723 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008724 {
8725 MBEDTLS_SSL_PEND_FATAL_ALERT(
8726 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8727 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008728 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008729 }
8730
8731 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008732 }
8733
8734 /* Use our order of preference */
8735 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8736 {
8737 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008738 p = protocol_name_list;
8739 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008740 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008741 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008742 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008743 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008744 {
8745 ssl->alpn_chosen = *alpn;
8746 return( 0 );
8747 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008748
8749 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008750 }
8751 }
8752
XiaokangQian95d5f542022-06-24 02:29:26 +00008753 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008754 MBEDTLS_SSL_PEND_FATAL_ALERT(
8755 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8756 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8757 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8758}
8759
8760int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8761 unsigned char *buf,
8762 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008763 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008764{
8765 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008766 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008767 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008768
8769 if( ssl->alpn_chosen == NULL )
8770 {
8771 return( 0 );
8772 }
8773
XiaokangQian95d5f542022-06-24 02:29:26 +00008774 protocol_name_len = strlen( ssl->alpn_chosen );
8775 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008776
8777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8778 /*
8779 * 0 . 1 ext identifier
8780 * 2 . 3 ext length
8781 * 4 . 5 protocol list length
8782 * 6 . 6 protocol name length
8783 * 7 . 7+n protocol name
8784 */
8785 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8786
XiaokangQian95d5f542022-06-24 02:29:26 +00008787 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008788
XiaokangQian95d5f542022-06-24 02:29:26 +00008789 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8790 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008791 /* Note: the length of the chosen protocol has been checked to be less
8792 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8793 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008794 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008795
XiaokangQian95d5f542022-06-24 02:29:26 +00008796 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008797 return ( 0 );
8798}
8799#endif /* MBEDTLS_SSL_ALPN */
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#endif /* MBEDTLS_SSL_TLS_C */