blob: fa4e6930e5d0bd95b0921579f42560341dfe7d41 [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
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000300#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
301 if( src->hostname != NULL )
302 {
Xiaokang Qianecd75282022-09-28 07:11:02 +0000303 dst->hostname = mbedtls_calloc( 1, src->hostname_len );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000304 if( dst->hostname == NULL )
305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
306
307 memcpy( dst->hostname, src->hostname, src->hostname_len );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000308 }
309#endif
310
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200311 return( 0 );
312}
313
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500316static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
317{
318 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
319 if( resized_buffer == NULL )
320 return -1;
321
322 /* We want to copy len_new bytes when downsizing the buffer, and
323 * len_old bytes when upsizing, so we choose the smaller of two sizes,
324 * to fit one buffer into another. Size checks, ensuring that no data is
325 * lost, are done outside of this function. */
326 memcpy( resized_buffer, *buffer,
327 ( len_new < *len_old ) ? len_new : *len_old );
328 mbedtls_platform_zeroize( *buffer, *len_old );
329 mbedtls_free( *buffer );
330
331 *buffer = resized_buffer;
332 *len_old = len_new;
333
334 return 0;
335}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336
337static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500338 size_t in_buf_new_len,
339 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340{
341 int modified = 0;
342 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
343 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
344 if( ssl->in_buf != NULL )
345 {
346 written_in = ssl->in_msg - ssl->in_buf;
347 iv_offset_in = ssl->in_iv - ssl->in_buf;
348 len_offset_in = ssl->in_len - ssl->in_buf;
349 if( downsizing ?
350 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
351 ssl->in_buf_len < in_buf_new_len )
352 {
353 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
354 {
355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
356 }
357 else
358 {
Paul Elliottb7449902021-03-10 18:14:58 +0000359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
360 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200361 modified = 1;
362 }
363 }
364 }
365
366 if( ssl->out_buf != NULL )
367 {
368 written_out = ssl->out_msg - ssl->out_buf;
369 iv_offset_out = ssl->out_iv - ssl->out_buf;
370 len_offset_out = ssl->out_len - ssl->out_buf;
371 if( downsizing ?
372 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
373 ssl->out_buf_len < out_buf_new_len )
374 {
375 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
376 {
377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
378 }
379 else
380 {
Paul Elliottb7449902021-03-10 18:14:58 +0000381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
382 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 modified = 1;
384 }
385 }
386 }
387 if( modified )
388 {
389 /* Update pointers here to avoid doing it twice. */
390 mbedtls_ssl_reset_in_out_pointers( ssl );
391 /* Fields below might not be properly updated with record
392 * splitting or with CID, so they are manually updated here. */
393 ssl->out_msg = ssl->out_buf + written_out;
394 ssl->out_len = ssl->out_buf + len_offset_out;
395 ssl->out_iv = ssl->out_buf + iv_offset_out;
396
397 ssl->in_msg = ssl->in_buf + written_in;
398 ssl->in_len = ssl->in_buf + len_offset_in;
399 ssl->in_iv = ssl->in_buf + iv_offset_in;
400 }
401}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500402#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
403
Jerry Yudb8c48a2022-01-27 14:54:54 +0800404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800405
406#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
407typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
408 const char *label,
409 const unsigned char *random, size_t rlen,
410 unsigned char *dstbuf, size_t dlen );
411
412static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
413
414#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
415
416/* Type for the TLS PRF */
417typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
418 const unsigned char *, size_t,
419 unsigned char *, size_t);
420
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200421MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800422static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
423 int ciphersuite,
424 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200425#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800426 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200427#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800428 ssl_tls_prf_t tls_prf,
429 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400430 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800431 unsigned endpoint,
432 const mbedtls_ssl_context *ssl );
433
Andrzej Kurek25f27152022-08-17 16:09:31 -0400434#if defined(MBEDTLS_HAS_ALG_SHA_256_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_sha256( 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 );
440static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
441static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
442
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400443#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800444
Andrzej Kurek25f27152022-08-17 16:09:31 -0400445#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200446MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800447static int tls_prf_sha384( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
450 unsigned char *dstbuf, size_t dlen );
451
452static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
453static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400454#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800455
Jerry Yu438ddd82022-07-07 06:55:50 +0000456static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800457 unsigned char *buf,
458 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800459
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200460MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000461static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800462 const unsigned char *buf,
463 size_t len );
464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
465
Jerry Yu53d23e22022-02-09 16:25:09 +0800466static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800471
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800473static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400474#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
Ron Eldor51d3ab52019-05-12 14:54:30 +0300476int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
477 const unsigned char *secret, size_t slen,
478 const char *label,
479 const unsigned char *random, size_t rlen,
480 unsigned char *dstbuf, size_t dlen )
481{
482 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
483
484 switch( prf )
485 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400487#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 case MBEDTLS_SSL_TLS_PRF_SHA384:
489 tls_prf = tls_prf_sha384;
490 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400491#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400492#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 case MBEDTLS_SSL_TLS_PRF_SHA256:
494 tls_prf = tls_prf_sha256;
495 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400496#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300498 default:
499 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
500 }
501
502 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
503}
504
Jerry Yuc73c6182022-02-08 20:29:25 +0800505#if defined(MBEDTLS_X509_CRT_PARSE_C)
506static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
507{
508#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
509 if( session->peer_cert != NULL )
510 {
511 mbedtls_x509_crt_free( session->peer_cert );
512 mbedtls_free( session->peer_cert );
513 session->peer_cert = NULL;
514 }
515#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
516 if( session->peer_cert_digest != NULL )
517 {
518 /* Zeroization is not necessary. */
519 mbedtls_free( session->peer_cert_digest );
520 session->peer_cert_digest = NULL;
521 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
522 session->peer_cert_digest_len = 0;
523 }
524#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
525}
526#endif /* MBEDTLS_X509_CRT_PARSE_C */
527
528void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
529 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
530{
531 ((void) ciphersuite_info);
532
Andrzej Kurek25f27152022-08-17 16:09:31 -0400533#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800534 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
535 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
536 else
537#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400538#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800539 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
540 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
541 else
542#endif
543 {
544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
545 return;
546 }
547}
548
XiaokangQianadab9a62022-07-18 07:41:26 +0000549void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
550 unsigned hs_type,
551 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100552{
553 unsigned char hs_hdr[4];
554
555 /* Build HS header for checksum update. */
556 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
557 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
558 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
559 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
560
561 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
562}
563
564void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
565 unsigned hs_type,
566 unsigned char const *msg,
567 size_t msg_len )
568{
569 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
570 ssl->handshake->update_checksum( ssl, msg, msg_len );
571}
572
Jerry Yuc73c6182022-02-08 20:29:25 +0800573void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
574{
575 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400576#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800577#if defined(MBEDTLS_USE_PSA_CRYPTO)
578 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
579 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
580#else
581 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
582#endif
583#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400584#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800585#if defined(MBEDTLS_USE_PSA_CRYPTO)
586 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
587 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
588#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400589 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800590#endif
591#endif
592}
593
594static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
595 const unsigned char *buf, size_t len )
596{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400597#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800598#if defined(MBEDTLS_USE_PSA_CRYPTO)
599 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
600#else
601 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
602#endif
603#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400604#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#if defined(MBEDTLS_USE_PSA_CRYPTO)
606 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
607#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400608 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800609#endif
610#endif
611}
612
Andrzej Kurek25f27152022-08-17 16:09:31 -0400613#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800614static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
615 const unsigned char *buf, size_t len )
616{
617#if defined(MBEDTLS_USE_PSA_CRYPTO)
618 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
619#else
620 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
621#endif
622}
623#endif
624
Andrzej Kurek25f27152022-08-17 16:09:31 -0400625#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800626static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
627 const unsigned char *buf, size_t len )
628{
629#if defined(MBEDTLS_USE_PSA_CRYPTO)
630 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
631#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400632 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800633#endif
634}
635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640
Andrzej Kurek25f27152022-08-17 16:09:31 -0400641#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500642#if defined(MBEDTLS_USE_PSA_CRYPTO)
643 handshake->fin_sha256_psa = psa_hash_operation_init();
644 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
645#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200647 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200648#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500649#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400650#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500652 handshake->fin_sha384_psa = psa_hash_operation_init();
653 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500654#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400655 mbedtls_sha512_init( &handshake->fin_sha384 );
656 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200657#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500658#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659
660 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_DHM_C)
663 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200664#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200665#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200667#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200668#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200669 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200670#if defined(MBEDTLS_SSL_CLI_C)
671 handshake->ecjpake_cache = NULL;
672 handshake->ecjpake_cache_len = 0;
673#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200674#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200675
Gilles Peskineeccd8882020-03-10 12:19:08 +0100676#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200677 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200678#endif
679
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200680#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
681 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
682#endif
Hanno Becker75173122019-02-06 16:18:31 +0000683
684#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
685 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
686 mbedtls_pk_init( &handshake->peer_pubkey );
687#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200688}
689
Hanno Beckera18d1322018-01-03 14:27:32 +0000690void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200693
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100694#if defined(MBEDTLS_USE_PSA_CRYPTO)
695 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
696 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100697#else
698 mbedtls_cipher_init( &transform->cipher_ctx_enc );
699 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100700#endif
701
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000702#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100703#if defined(MBEDTLS_USE_PSA_CRYPTO)
704 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
705 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100706#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_init( &transform->md_ctx_enc );
708 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000709#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100710#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200711}
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200716}
717
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200718MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000720{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200721 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000722 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200724 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200726 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200727 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728
729 /*
730 * Either the pointers are now NULL or cleared properly and can be freed.
731 * Now allocate missing structures.
732 */
733 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200734 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200735 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200736 }
Paul Bakker48916f92012-09-16 19:57:18 +0000737
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200738 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200740 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 }
Paul Bakker48916f92012-09-16 19:57:18 +0000742
Paul Bakker82788fb2014-10-20 13:59:19 +0200743 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200744 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200745 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500747#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
748 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400749
Andrzej Kurek4a063792020-10-21 15:08:44 +0200750 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
751 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500752#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000753
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200754 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000755 if( ssl->handshake == NULL ||
756 ssl->transform_negotiate == NULL ||
757 ssl->session_negotiate == NULL )
758 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_free( ssl->handshake );
762 mbedtls_free( ssl->transform_negotiate );
763 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200764
765 ssl->handshake = NULL;
766 ssl->transform_negotiate = NULL;
767 ssl->session_negotiate = NULL;
768
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000770 }
771
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200772 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000774 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200775 ssl_handshake_params_init( ssl->handshake );
776
Jerry Yud0766ec2022-09-22 10:46:57 +0800777#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
778 defined(MBEDTLS_SSL_SRV_C) && \
779 defined(MBEDTLS_SSL_SESSION_TICKETS)
780 ssl->handshake->new_session_tickets_count =
781 ssl->conf->new_session_tickets_count ;
782#endif
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
786 {
787 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200788
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200789 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
790 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
791 else
792 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200793
Hanno Becker0f57a652020-02-05 10:37:26 +0000794 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200795 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200796#endif
797
Brett Warrene0edc842021-08-17 09:53:13 +0100798/*
799 * curve_list is translated to IANA TLS group identifiers here because
800 * mbedtls_ssl_conf_curves returns void and so can't return
801 * any error codes.
802 */
803#if defined(MBEDTLS_ECP_C)
804#if !defined(MBEDTLS_DEPRECATED_REMOVED)
805 /* Heap allocate and translate curve_list from internal to IANA group ids */
806 if ( ssl->conf->curve_list != NULL )
807 {
808 size_t length;
809 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
810
811 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
812 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
813
814 /* Leave room for zero termination */
815 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
816 if ( group_list == NULL )
817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
818
819 for( size_t i = 0; i < length; i++ )
820 {
821 const mbedtls_ecp_curve_info *info =
822 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
823 if ( info == NULL )
824 {
825 mbedtls_free( group_list );
826 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
827 }
828 group_list[i] = info->tls_id;
829 }
830
831 group_list[length] = 0;
832
833 ssl->handshake->group_list = group_list;
834 ssl->handshake->group_list_heap_allocated = 1;
835 }
836 else
837 {
838 ssl->handshake->group_list = ssl->conf->group_list;
839 ssl->handshake->group_list_heap_allocated = 0;
840 }
841#endif /* MBEDTLS_DEPRECATED_REMOVED */
842#endif /* MBEDTLS_ECP_C */
843
Jerry Yuf017ee42022-01-12 15:49:48 +0800844#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
845#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800846#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800847 /* Heap allocate and translate sig_hashes from internal hash identifiers to
848 signature algorithms IANA identifiers. */
849 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800850 ssl->conf->sig_hashes != NULL )
851 {
852 const int *md;
853 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800854 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800855 uint16_t *p;
856
Jerry Yub476a442022-01-21 18:14:45 +0800857#if defined(static_assert)
858 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
859 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
860 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800861#endif
862
Jerry Yuf017ee42022-01-12 15:49:48 +0800863 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
864 {
865 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
866 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800867#if defined(MBEDTLS_ECDSA_C)
868 sig_algs_len += sizeof( uint16_t );
869#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800870
Jerry Yub476a442022-01-21 18:14:45 +0800871#if defined(MBEDTLS_RSA_C)
872 sig_algs_len += sizeof( uint16_t );
873#endif
874 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
875 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800876 }
877
Jerry Yub476a442022-01-21 18:14:45 +0800878 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
880
Jerry Yub476a442022-01-21 18:14:45 +0800881 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
882 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800883 if( ssl->handshake->sig_algs == NULL )
884 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
885
886 p = (uint16_t *)ssl->handshake->sig_algs;
887 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
888 {
889 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
890 if( hash == MBEDTLS_SSL_HASH_NONE )
891 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800892#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800893 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
894 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800895#endif
896#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800897 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
898 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800899#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200901 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800902 ssl->handshake->sig_algs_heap_allocated = 1;
903 }
904 else
Jerry Yua69269a2022-01-17 21:06:01 +0800905#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800906 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800907 ssl->handshake->sig_algs_heap_allocated = 0;
908 }
Jerry Yucc539102022-06-27 16:27:35 +0800909#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800910#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000911 return( 0 );
912}
913
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200914#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200915/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200916MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200917static int ssl_cookie_write_dummy( void *ctx,
918 unsigned char **p, unsigned char *end,
919 const unsigned char *cli_id, size_t cli_id_len )
920{
921 ((void) ctx);
922 ((void) p);
923 ((void) end);
924 ((void) cli_id);
925 ((void) cli_id_len);
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200928}
929
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200930MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931static int ssl_cookie_check_dummy( void *ctx,
932 const unsigned char *cookie, size_t cookie_len,
933 const unsigned char *cli_id, size_t cli_id_len )
934{
935 ((void) ctx);
936 ((void) cookie);
937 ((void) cookie_len);
938 ((void) cli_id);
939 ((void) cli_id_len);
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200942}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200943#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200944
Paul Bakker5121ce52009-01-03 21:22:43 +0000945/*
946 * Initialize an SSL context
947 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200948void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
949{
950 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
951}
952
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200953MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800954static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
955{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100956 const mbedtls_ssl_config *conf = ssl->conf;
957
Ronald Cron6f135e12021-12-08 16:57:54 +0100958#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100959 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
960 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000961 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
962 {
963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
964 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
965 }
966
Jerry Yu60835a82021-08-04 10:13:52 +0800967 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
968 return( 0 );
969 }
970#endif
971
972#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100973 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800974 {
975 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
976 return( 0 );
977 }
978#endif
979
Ronald Cron6f135e12021-12-08 16:57:54 +0100980#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100981 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800982 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
984 {
985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
986 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
987 }
988
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000989 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
990 {
991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
992 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
993 }
994
995
Ronald Crone1d3f062022-02-10 14:50:54 +0100996 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
997 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +0800998 }
999#endif
1000
1001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1002 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1003}
1004
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001005MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001006static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1007{
1008 int ret;
1009 ret = ssl_conf_version_check( ssl );
1010 if( ret != 0 )
1011 return( ret );
1012
1013 /* Space for further checks */
1014
1015 return( 0 );
1016}
1017
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001018/*
1019 * Setup an SSL context
1020 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001021
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001022int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001023 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001024{
Janos Follath865b3eb2019-12-16 11:46:15 +00001025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001026 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1027 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001029 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001030
Jerry Yu60835a82021-08-04 10:13:52 +08001031 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1032 return( ret );
1033
Paul Bakker62f2dee2012-09-28 07:31:51 +00001034 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001035 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001036 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001037
1038 /* Set to NULL in case of an error condition */
1039 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001040
Darryl Greenb33cc762019-11-28 14:29:44 +00001041#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1042 ssl->in_buf_len = in_buf_len;
1043#endif
1044 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001045 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_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;
Angus Grattond8213d02016-05-25 20:56:48 +10001050 }
1051
Darryl Greenb33cc762019-11-28 14:29:44 +00001052#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1053 ssl->out_buf_len = out_buf_len;
1054#endif
1055 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001056 if( ssl->out_buf == NULL )
1057 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001059 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001060 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 }
1062
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001063 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001064
Johan Pascalb62bb512015-12-03 21:56:45 +01001065#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001066 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001067#endif
1068
Paul Bakker48916f92012-09-16 19:57:18 +00001069 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001070 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
1072 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001073
1074error:
1075 mbedtls_free( ssl->in_buf );
1076 mbedtls_free( ssl->out_buf );
1077
1078 ssl->conf = NULL;
1079
Darryl Greenb33cc762019-11-28 14:29:44 +00001080#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1081 ssl->in_buf_len = 0;
1082 ssl->out_buf_len = 0;
1083#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001084 ssl->in_buf = NULL;
1085 ssl->out_buf = NULL;
1086
1087 ssl->in_hdr = NULL;
1088 ssl->in_ctr = NULL;
1089 ssl->in_len = NULL;
1090 ssl->in_iv = NULL;
1091 ssl->in_msg = NULL;
1092
1093 ssl->out_hdr = NULL;
1094 ssl->out_ctr = NULL;
1095 ssl->out_len = NULL;
1096 ssl->out_iv = NULL;
1097 ssl->out_msg = NULL;
1098
k-stachowiak9f7798e2018-07-31 16:52:32 +02001099 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001100}
1101
1102/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001103 * Reset an initialized and used SSL context for re-use while retaining
1104 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001105 *
1106 * If partial is non-zero, keep data in the input buffer and client ID.
1107 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001108 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001109void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1110 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001111{
Darryl Greenb33cc762019-11-28 14:29:44 +00001112#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1113 size_t in_buf_len = ssl->in_buf_len;
1114 size_t out_buf_len = ssl->out_buf_len;
1115#else
1116 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1117 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1118#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001119
Hanno Beckerb0302c42021-08-03 09:39:42 +01001120#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1121 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001122#endif
1123
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001124 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001125 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001126
Hanno Beckerb0302c42021-08-03 09:39:42 +01001127 mbedtls_ssl_reset_in_out_pointers( ssl );
1128
1129 /* Reset incoming message parsing */
1130 ssl->in_offt = NULL;
1131 ssl->nb_zero = 0;
1132 ssl->in_msgtype = 0;
1133 ssl->in_msglen = 0;
1134 ssl->in_hslen = 0;
1135 ssl->keep_current_message = 0;
1136 ssl->transform_in = NULL;
1137
1138#if defined(MBEDTLS_SSL_PROTO_DTLS)
1139 ssl->next_record_offset = 0;
1140 ssl->in_epoch = 0;
1141#endif
1142
1143 /* Keep current datagram if partial == 1 */
1144 if( partial == 0 )
1145 {
1146 ssl->in_left = 0;
1147 memset( ssl->in_buf, 0, in_buf_len );
1148 }
1149
Ronald Cronad8c17b2022-06-10 17:18:09 +02001150 ssl->send_alert = 0;
1151
Hanno Beckerb0302c42021-08-03 09:39:42 +01001152 /* Reset outgoing message writing */
1153 ssl->out_msgtype = 0;
1154 ssl->out_msglen = 0;
1155 ssl->out_left = 0;
1156 memset( ssl->out_buf, 0, out_buf_len );
1157 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1158 ssl->transform_out = NULL;
1159
1160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1161 mbedtls_ssl_dtls_replay_reset( ssl );
1162#endif
1163
XiaokangQian2b01dc32022-01-21 02:53:13 +00001164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001165 if( ssl->transform )
1166 {
1167 mbedtls_ssl_transform_free( ssl->transform );
1168 mbedtls_free( ssl->transform );
1169 ssl->transform = NULL;
1170 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1172
XiaokangQian2b01dc32022-01-21 02:53:13 +00001173#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1174 mbedtls_ssl_transform_free( ssl->transform_application );
1175 mbedtls_free( ssl->transform_application );
1176 ssl->transform_application = NULL;
1177
1178 if( ssl->handshake != NULL )
1179 {
1180 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1181 mbedtls_free( ssl->handshake->transform_earlydata );
1182 ssl->handshake->transform_earlydata = NULL;
1183
1184 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1185 mbedtls_free( ssl->handshake->transform_handshake );
1186 ssl->handshake->transform_handshake = NULL;
1187 }
1188
XiaokangQian2b01dc32022-01-21 02:53:13 +00001189#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001190}
1191
1192int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1193{
1194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1195
1196 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1197
XiaokangQian78b1fa72022-01-19 06:56:30 +00001198 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001199
1200 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_SSL_RENEGOTIATION)
1202 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001203 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001204
1205 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1207 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001210
Hanno Beckerb0302c42021-08-03 09:39:42 +01001211 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001212 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001213 if( ssl->session )
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_ssl_session_free( ssl->session );
1216 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001217 ssl->session = NULL;
1218 }
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001221 ssl->alpn_chosen = NULL;
1222#endif
1223
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001224#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001225#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001226 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001227#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001228 {
1229 mbedtls_free( ssl->cli_id );
1230 ssl->cli_id = NULL;
1231 ssl->cli_id_len = 0;
1232 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001233#endif
1234
Paul Bakker48916f92012-09-16 19:57:18 +00001235 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1236 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001237
1238 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001239}
1240
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001241/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001242 * Reset an initialized and used SSL context for re-use while retaining
1243 * all application-set variables, function pointers and data.
1244 */
1245int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1246{
Hanno Becker43aefe22020-02-05 10:44:56 +00001247 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001248}
1249
1250/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 * SSL set accessors
1252 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001253void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001255 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256}
1257
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001258void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001259{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001260 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001261}
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001264void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001266 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001267}
1268#endif
1269
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001270void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001272 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001273}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001276
Hanno Becker1841b0a2018-08-24 11:13:57 +01001277void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1278 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001279{
1280 ssl->disable_datagram_packing = !allow_packing;
1281}
1282
1283void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1284 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001285{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001286 conf->hs_timeout_min = min;
1287 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001288}
1289#endif
1290
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001291void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001293 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294}
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001298 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001299 void *p_vrfy )
1300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001301 conf->f_vrfy = f_vrfy;
1302 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001305
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001306void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001307 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 void *p_rng )
1309{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001310 conf->f_rng = f_rng;
1311 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001312}
1313
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001314void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001315 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 void *p_dbg )
1317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001318 conf->f_dbg = f_dbg;
1319 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320}
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001323 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001324 mbedtls_ssl_send_t *f_send,
1325 mbedtls_ssl_recv_t *f_recv,
1326 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001327{
1328 ssl->p_bio = p_bio;
1329 ssl->f_send = f_send;
1330 ssl->f_recv = f_recv;
1331 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001332}
1333
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001334#if defined(MBEDTLS_SSL_PROTO_DTLS)
1335void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1336{
1337 ssl->mtu = mtu;
1338}
1339#endif
1340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001341void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001342{
1343 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001344}
1345
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001346void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1347 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001348 mbedtls_ssl_set_timer_t *f_set_timer,
1349 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001350{
1351 ssl->p_timer = p_timer;
1352 ssl->f_set_timer = f_set_timer;
1353 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001354
1355 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001356 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001357}
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001360void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001361 void *p_cache,
1362 mbedtls_ssl_cache_get_t *f_get_cache,
1363 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001365 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001366 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001367 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_SSL_CLI_C)
1372int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373{
Janos Follath865b3eb2019-12-16 11:46:15 +00001374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001375
1376 if( ssl == NULL ||
1377 session == NULL ||
1378 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001379 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001382 }
1383
Hanno Beckere810bbc2021-05-14 16:01:05 +01001384 if( ssl->handshake->resume == 1 )
1385 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1386
Jerry Yu21092062022-10-10 21:21:31 +08001387#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1388 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001389 {
Jerry Yu21092062022-10-10 21:21:31 +08001390 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1391 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1392
1393 if( mbedtls_ssl_validate_ciphersuite(
1394 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1395 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1396 {
1397 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1398 session->ciphersuite ) );
1399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1400 }
Jerry Yu40afab62022-10-08 10:42:13 +08001401 }
Jerry Yu21092062022-10-10 21:21:31 +08001402#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001403
Hanno Becker52055ae2019-02-06 14:30:46 +00001404 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1405 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001406 return( ret );
1407
Paul Bakker0a597072012-09-25 21:55:46 +00001408 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001409
1410 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001414void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1415 const int *ciphersuites )
1416{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001417 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418}
1419
Ronald Cron6f135e12021-12-08 16:57:54 +01001420#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001421void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001422 const int kex_modes )
1423{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001424 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001425}
Ronald Cron6f135e12021-12-08 16:57:54 +01001426#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001429void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001430 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001431{
1432 conf->cert_profile = profile;
1433}
1434
Glenn Strauss36872db2022-01-22 05:06:31 -05001435static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1436{
1437 mbedtls_ssl_key_cert *cur = key_cert, *next;
1438
1439 while( cur != NULL )
1440 {
1441 next = cur->next;
1442 mbedtls_free( cur );
1443 cur = next;
1444 }
1445}
1446
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001447/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001448MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001449static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1450 mbedtls_x509_crt *cert,
1451 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001452{
niisato8ee24222018-06-25 19:05:48 +09001453 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001454
Glenn Strauss36872db2022-01-22 05:06:31 -05001455 if( cert == NULL )
1456 {
1457 /* Free list if cert is null */
1458 ssl_key_cert_free( *head );
1459 *head = NULL;
1460 return( 0 );
1461 }
1462
niisato8ee24222018-06-25 19:05:48 +09001463 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1464 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001465 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001466
niisato8ee24222018-06-25 19:05:48 +09001467 new_cert->cert = cert;
1468 new_cert->key = key;
1469 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001470
Glenn Strauss36872db2022-01-22 05:06:31 -05001471 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001472 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001473 {
niisato8ee24222018-06-25 19:05:48 +09001474 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001475 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001476 else
1477 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001478 mbedtls_ssl_key_cert *cur = *head;
1479 while( cur->next != NULL )
1480 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001481 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001482 }
1483
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001484 return( 0 );
1485}
1486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001487int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001488 mbedtls_x509_crt *own_cert,
1489 mbedtls_pk_context *pk_key )
1490{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001491 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001492}
1493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001494void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001495 mbedtls_x509_crt *ca_chain,
1496 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001498 conf->ca_chain = ca_chain;
1499 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001500
1501#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1502 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1503 * cannot be used together. */
1504 conf->f_ca_cb = NULL;
1505 conf->p_ca_cb = NULL;
1506#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001507}
Hanno Becker5adaad92019-03-27 16:54:37 +00001508
1509#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1510void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001511 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001512 void *p_ca_cb )
1513{
1514 conf->f_ca_cb = f_ca_cb;
1515 conf->p_ca_cb = p_ca_cb;
1516
1517 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1518 * cannot be used together. */
1519 conf->ca_chain = NULL;
1520 conf->ca_crl = NULL;
1521}
1522#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001524
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001525#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001526const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1527 size_t *name_len )
1528{
1529 *name_len = ssl->handshake->sni_name_len;
1530 return( ssl->handshake->sni_name );
1531}
1532
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001533int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1534 mbedtls_x509_crt *own_cert,
1535 mbedtls_pk_context *pk_key )
1536{
1537 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1538 own_cert, pk_key ) );
1539}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001540
1541void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1542 mbedtls_x509_crt *ca_chain,
1543 mbedtls_x509_crl *ca_crl )
1544{
1545 ssl->handshake->sni_ca_chain = ca_chain;
1546 ssl->handshake->sni_ca_crl = ca_crl;
1547}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001548
Glenn Strauss999ef702022-03-11 01:37:23 -05001549#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1550void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1551 const mbedtls_x509_crt *crt)
1552{
1553 ssl->handshake->dn_hints = crt;
1554}
1555#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1556
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001557void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1558 int authmode )
1559{
1560 ssl->handshake->sni_authmode = authmode;
1561}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001562#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1563
Hanno Becker8927c832019-04-03 12:52:50 +01001564#if defined(MBEDTLS_X509_CRT_PARSE_C)
1565void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1566 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1567 void *p_vrfy )
1568{
1569 ssl->f_vrfy = f_vrfy;
1570 ssl->p_vrfy = p_vrfy;
1571}
1572#endif
1573
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001574#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001575/*
1576 * Set EC J-PAKE password for current handshake
1577 */
1578int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1579 const unsigned char *pw,
1580 size_t pw_len )
1581{
1582 mbedtls_ecjpake_role role;
1583
Janos Follath8eb64132016-06-03 15:40:57 +01001584 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586
1587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1588 role = MBEDTLS_ECJPAKE_SERVER;
1589 else
1590 role = MBEDTLS_ECJPAKE_CLIENT;
1591
1592 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1593 role,
1594 MBEDTLS_MD_SHA256,
1595 MBEDTLS_ECP_DP_SECP256R1,
1596 pw, pw_len ) );
1597}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001598#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001599
Gilles Peskineeccd8882020-03-10 12:19:08 +01001600#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001601
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001602MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001603static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1604{
1605#if defined(MBEDTLS_USE_PSA_CRYPTO)
1606 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1607 return( 1 );
1608#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001609 if( conf->psk != NULL )
1610 return( 1 );
1611
1612 return( 0 );
1613}
1614
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001615static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1616{
1617 /* Remove reference to existing PSK, if any. */
1618#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001620 {
1621 /* The maintenance of the PSK key slot is the
1622 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001623 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001624 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001625#endif /* MBEDTLS_USE_PSA_CRYPTO */
1626 if( conf->psk != NULL )
1627 {
1628 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1629
1630 mbedtls_free( conf->psk );
1631 conf->psk = NULL;
1632 conf->psk_len = 0;
1633 }
1634
1635 /* Remove reference to PSK identity, if any. */
1636 if( conf->psk_identity != NULL )
1637 {
1638 mbedtls_free( conf->psk_identity );
1639 conf->psk_identity = NULL;
1640 conf->psk_identity_len = 0;
1641 }
1642}
1643
Hanno Becker7390c712018-11-15 13:33:04 +00001644/* This function assumes that PSK identity in the SSL config is unset.
1645 * It checks that the provided identity is well-formed and attempts
1646 * to make a copy of it in the SSL config.
1647 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001648MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001649static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1650 unsigned char const *psk_identity,
1651 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001652{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001653 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001654 if( psk_identity == NULL ||
1655 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001656 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001657 {
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659 }
1660
Hanno Becker7390c712018-11-15 13:33:04 +00001661 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1662 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001664
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001665 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001666 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001667
1668 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001669}
1670
Hanno Becker7390c712018-11-15 13:33:04 +00001671int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1672 const unsigned char *psk, size_t psk_len,
1673 const unsigned char *psk_identity, size_t psk_identity_len )
1674{
Janos Follath865b3eb2019-12-16 11:46:15 +00001675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001676
1677 /* We currently only support one PSK, raw or opaque. */
1678 if( ssl_conf_psk_is_configured( conf ) )
1679 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001680
1681 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001682 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001684 if( psk_len == 0 )
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1688
Hanno Becker7390c712018-11-15 13:33:04 +00001689 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1690 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1691 conf->psk_len = psk_len;
1692 memcpy( conf->psk, psk, conf->psk_len );
1693
1694 /* Check and set PSK Identity */
1695 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1696 if( ret != 0 )
1697 ssl_conf_remove_psk( conf );
1698
1699 return( ret );
1700}
1701
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001702static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1703{
1704#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001705 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001706 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001707 /* The maintenance of the external PSK key slot is the
1708 * user's responsibility. */
1709 if( ssl->handshake->psk_opaque_is_internal )
1710 {
1711 psa_destroy_key( ssl->handshake->psk_opaque );
1712 ssl->handshake->psk_opaque_is_internal = 0;
1713 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001714 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001715 }
Neil Armstronge952a302022-05-03 10:22:14 +02001716#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001717 if( ssl->handshake->psk != NULL )
1718 {
1719 mbedtls_platform_zeroize( ssl->handshake->psk,
1720 ssl->handshake->psk_len );
1721 mbedtls_free( ssl->handshake->psk );
1722 ssl->handshake->psk_len = 0;
1723 }
Neil Armstronge952a302022-05-03 10:22:14 +02001724#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001725}
1726
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001727int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1728 const unsigned char *psk, size_t psk_len )
1729{
Neil Armstrong501c9322022-05-03 09:35:09 +02001730#if defined(MBEDTLS_USE_PSA_CRYPTO)
1731 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001733 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001735#endif /* MBEDTLS_USE_PSA_CRYPTO */
1736
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001737 if( psk == NULL || ssl->handshake == NULL )
1738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1739
1740 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1742
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001743 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001744
Neil Armstrong501c9322022-05-03 09:35:09 +02001745#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001746#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1747 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1748 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001749 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001750 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1751 else
1752 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1753 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1754 }
1755#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001756
Jerry Yu568ec252022-07-22 21:27:34 +08001757#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001758 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1759 {
1760 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1761 psa_set_key_usage_flags( &key_attributes,
1762 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1763 }
1764#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1765
Neil Armstrong501c9322022-05-03 09:35:09 +02001766 psa_set_key_algorithm( &key_attributes, alg );
1767 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1768
1769 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1770 if( status != PSA_SUCCESS )
1771 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1772
1773 /* Allow calling psa_destroy_key() on psk remove */
1774 ssl->handshake->psk_opaque_is_internal = 1;
1775 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1776#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001777 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001779
1780 ssl->handshake->psk_len = psk_len;
1781 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1782
1783 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001784#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001785}
1786
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001787#if defined(MBEDTLS_USE_PSA_CRYPTO)
1788int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001789 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001790 const unsigned char *psk_identity,
1791 size_t psk_identity_len )
1792{
Janos Follath865b3eb2019-12-16 11:46:15 +00001793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001794
1795 /* We currently only support one PSK, raw or opaque. */
1796 if( ssl_conf_psk_is_configured( conf ) )
1797 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001798
Hanno Becker7390c712018-11-15 13:33:04 +00001799 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001800 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001802 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001803
1804 /* Check and set PSK Identity */
1805 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1806 psk_identity_len );
1807 if( ret != 0 )
1808 ssl_conf_remove_psk( conf );
1809
1810 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001811}
1812
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001813int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1814 mbedtls_svc_key_id_t psk )
1815{
1816 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1817 ( ssl->handshake == NULL ) )
1818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1819
1820 ssl_remove_psk( ssl );
1821 ssl->handshake->psk_opaque = psk;
1822 return( 0 );
1823}
1824#endif /* MBEDTLS_USE_PSA_CRYPTO */
1825
Jerry Yu8897c072022-08-12 13:56:53 +08001826#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001827void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1828 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1829 size_t),
1830 void *p_psk )
1831{
1832 conf->f_psk = f_psk;
1833 conf->p_psk = p_psk;
1834}
Jerry Yu8897c072022-08-12 13:56:53 +08001835#endif /* MBEDTLS_SSL_SRV_C */
1836
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001837#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1838
1839#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001840static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1841 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001842{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001843#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001844 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001847 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001848 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001849 return( MBEDTLS_SSL_MODE_STREAM );
1850}
1851
1852#else /* MBEDTLS_USE_PSA_CRYPTO */
1853
1854static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1855 mbedtls_cipher_mode_t mode )
1856{
1857#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1858 if( mode == MBEDTLS_MODE_CBC )
1859 return( MBEDTLS_SSL_MODE_CBC );
1860#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1861
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001862#if defined(MBEDTLS_GCM_C) || \
1863 defined(MBEDTLS_CCM_C) || \
1864 defined(MBEDTLS_CHACHAPOLY_C)
1865 if( mode == MBEDTLS_MODE_GCM ||
1866 mode == MBEDTLS_MODE_CCM ||
1867 mode == MBEDTLS_MODE_CHACHAPOLY )
1868 return( MBEDTLS_SSL_MODE_AEAD );
1869#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001870
1871 return( MBEDTLS_SSL_MODE_STREAM );
1872}
Gilles Peskine301711e2022-04-26 16:57:05 +02001873#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001874
Gilles Peskinee108d982022-04-26 16:50:40 +02001875static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1876 mbedtls_ssl_mode_t base_mode,
1877 int encrypt_then_mac )
1878{
1879#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1880 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1881 base_mode == MBEDTLS_SSL_MODE_CBC )
1882 {
1883 return( MBEDTLS_SSL_MODE_CBC_ETM );
1884 }
1885#else
1886 (void) encrypt_then_mac;
1887#endif
1888 return( base_mode );
1889}
1890
Neil Armstrongab555e02022-04-04 11:07:59 +02001891mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001892 const mbedtls_ssl_transform *transform )
1893{
Gilles Peskinee108d982022-04-26 16:50:40 +02001894 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001896 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001897#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001898 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1899#endif
1900 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001901
Gilles Peskinee108d982022-04-26 16:50:40 +02001902 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001903#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001904 encrypt_then_mac = transform->encrypt_then_mac;
1905#endif
1906 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001907}
1908
Neil Armstrongab555e02022-04-04 11:07:59 +02001909mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001910#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001911 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001912#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001913 const mbedtls_ssl_ciphersuite_t *suite )
1914{
Gilles Peskinee108d982022-04-26 16:50:40 +02001915 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1916
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001917#if defined(MBEDTLS_USE_PSA_CRYPTO)
1918 psa_status_t status;
1919 psa_algorithm_t alg;
1920 psa_key_type_t type;
1921 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001922 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1923 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001924 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001925#else
1926 const mbedtls_cipher_info_t *cipher =
1927 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001928 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001929 {
1930 base_mode =
1931 mbedtls_ssl_get_base_mode(
1932 mbedtls_cipher_info_get_mode( cipher ) );
1933 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001934#endif /* MBEDTLS_USE_PSA_CRYPTO */
1935
Gilles Peskinee108d982022-04-26 16:50:40 +02001936#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1937 int encrypt_then_mac = 0;
1938#endif
1939 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001940}
1941
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001942#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001943
1944#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001945/* Serialization of TLS 1.3 sessions:
1946 *
1947 * struct {
1948 * uint64 ticket_received;
1949 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001950 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001951 * } ClientOnlyData;
1952 *
1953 * struct {
1954 * uint8 endpoint;
1955 * uint8 ciphersuite[2];
1956 * uint32 ticket_age_add;
1957 * uint8 ticket_flags;
1958 * opaque resumption_key<0..255>;
Xiaokang Qianecd75282022-09-28 07:11:02 +00001959 * opaque hostname<1..2^8-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001960 * select ( endpoint ) {
1961 * case client: ClientOnlyData;
1962 * case server: uint64 start_time;
1963 * };
1964 * } serialized_session_tls13;
1965 *
1966 */
1967#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001968MBEDTLS_CHECK_RETURN_CRITICAL
1969static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1970 unsigned char *buf,
1971 size_t buf_len,
1972 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001973{
1974 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001975 size_t needed = 1 /* endpoint */
1976 + 2 /* ciphersuite */
1977 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001978 + 1 /* ticket_flags */
1979 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001980 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001981
1982 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1984 needed += session->resumption_key_len; /* resumption_key */
1985
Jerry Yu438ddd82022-07-07 06:55:50 +00001986#if defined(MBEDTLS_HAVE_TIME)
1987 needed += 8; /* start_time or ticket_received */
1988#endif
1989
1990#if defined(MBEDTLS_SSL_CLI_C)
1991 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1992 {
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001993#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1994 needed += 1 /* hostname_len */
1995 + session->hostname_len; /* hostname */
1996#endif
1997
Jerry Yu438ddd82022-07-07 06:55:50 +00001998 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08001999 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002000
2001 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002002 if( session->ticket_len > SIZE_MAX - needed )
2003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002004
Jerry Yue28d9742022-08-18 15:44:03 +08002005 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002006 }
2007#endif /* MBEDTLS_SSL_CLI_C */
2008
Jerry Yue36fdd62022-08-17 21:31:36 +08002009 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002011 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002012
2013 p[0] = session->endpoint;
2014 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2015 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2016 p[7] = session->ticket_flags;
2017
2018 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002019 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002020 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002021 memcpy( p, session->resumption_key, session->resumption_key_len );
2022 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002023
Xiaokang Qianecd75282022-09-28 07:11:02 +00002024 p[0] = session->hostname_len;
2025 p++;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002026#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2027 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Xiaokang Qianecd75282022-09-28 07:11:02 +00002028 session->hostname_len > 0 &&
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002029 session->hostname != NULL )
2030 {
2031 /* save host name */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002032 memcpy( p, session->hostname, session->hostname_len );
2033 p += session->hostname_len;
2034 }
2035#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2036
Jerry Yu438ddd82022-07-07 06:55:50 +00002037#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2038 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2039 {
2040 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2041 p += 8;
2042 }
2043#endif /* MBEDTLS_HAVE_TIME */
2044
2045#if defined(MBEDTLS_SSL_CLI_C)
2046 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2047 {
2048#if defined(MBEDTLS_HAVE_TIME)
2049 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2050 p += 8;
2051#endif
2052 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2053 p += 4;
2054
2055 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2056 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002057
2058 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002059 {
2060 memcpy( p, session->ticket, session->ticket_len );
2061 p += session->ticket_len;
2062 }
2063 }
2064#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002065 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002066}
2067
2068MBEDTLS_CHECK_RETURN_CRITICAL
2069static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2070 const unsigned char *buf,
2071 size_t len )
2072{
2073 const unsigned char *p = buf;
2074 const unsigned char *end = buf + len;
2075
2076 if( end - p < 9 )
2077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2078 session->endpoint = p[0];
2079 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2080 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2081 session->ticket_flags = p[7];
2082
2083 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002084 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002085 p += 9;
2086
Jerry Yubc7c1a42022-07-21 22:57:37 +08002087 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2089
Jerry Yubc7c1a42022-07-21 22:57:37 +08002090 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002092 memcpy( session->resumption_key, p, session->resumption_key_len );
2093 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002094
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002095#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2096 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2097 {
2098 /* load host name */
2099 session->hostname_len = p[0];
2100 p += 1;
2101 if( end - p < session->hostname_len )
2102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qianecd75282022-09-28 07:11:02 +00002103 if( session->hostname_len > 0 )
2104 {
2105 session->hostname = mbedtls_calloc( 1, session->hostname_len );
2106 if( session->hostname == NULL )
2107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2108 memcpy( session->hostname, p, session->hostname_len );
2109 p += session->hostname_len;
2110 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002111 }
2112#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2113
Jerry Yu438ddd82022-07-07 06:55:50 +00002114#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2115 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2116 {
2117 if( end - p < 8 )
2118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2119 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2120 p += 8;
2121 }
2122#endif /* MBEDTLS_HAVE_TIME */
2123
2124#if defined(MBEDTLS_SSL_CLI_C)
2125 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2126 {
2127#if defined(MBEDTLS_HAVE_TIME)
2128 if( end - p < 8 )
2129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2130 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2131 p += 8;
2132#endif
2133 if( end - p < 4 )
2134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2135 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2136 p += 4;
2137
2138 if( end - p < 2 )
2139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2140 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2141 p += 2;
2142
2143 if( end - p < ( long int )session->ticket_len )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145 if( session->ticket_len > 0 )
2146 {
2147 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2148 if( session->ticket == NULL )
2149 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2150 memcpy( session->ticket, p, session->ticket_len );
2151 p += session->ticket_len;
2152 }
2153 }
2154#endif /* MBEDTLS_SSL_CLI_C */
2155
2156 return( 0 );
2157
2158}
2159#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002160MBEDTLS_CHECK_RETURN_CRITICAL
2161static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2162 unsigned char *buf,
2163 size_t buf_len,
2164 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002165{
2166 ((void) session);
2167 ((void) buf);
2168 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002169 *olen = 0;
2170 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002171}
Jerry Yu438ddd82022-07-07 06:55:50 +00002172
2173static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2174 unsigned char *buf,
2175 size_t buf_len )
2176{
2177 ((void) session);
2178 ((void) buf);
2179 ((void) buf_len);
2180 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2181}
2182#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002183#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2184
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002185psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002186 size_t taglen,
2187 psa_algorithm_t *alg,
2188 psa_key_type_t *key_type,
2189 size_t *key_size )
2190{
2191 switch ( mbedtls_cipher_type )
2192 {
2193 case MBEDTLS_CIPHER_AES_128_CBC:
2194 *alg = PSA_ALG_CBC_NO_PADDING;
2195 *key_type = PSA_KEY_TYPE_AES;
2196 *key_size = 128;
2197 break;
2198 case MBEDTLS_CIPHER_AES_128_CCM:
2199 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2200 *key_type = PSA_KEY_TYPE_AES;
2201 *key_size = 128;
2202 break;
2203 case MBEDTLS_CIPHER_AES_128_GCM:
2204 *alg = PSA_ALG_GCM;
2205 *key_type = PSA_KEY_TYPE_AES;
2206 *key_size = 128;
2207 break;
2208 case MBEDTLS_CIPHER_AES_192_CCM:
2209 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2210 *key_type = PSA_KEY_TYPE_AES;
2211 *key_size = 192;
2212 break;
2213 case MBEDTLS_CIPHER_AES_192_GCM:
2214 *alg = PSA_ALG_GCM;
2215 *key_type = PSA_KEY_TYPE_AES;
2216 *key_size = 192;
2217 break;
2218 case MBEDTLS_CIPHER_AES_256_CBC:
2219 *alg = PSA_ALG_CBC_NO_PADDING;
2220 *key_type = PSA_KEY_TYPE_AES;
2221 *key_size = 256;
2222 break;
2223 case MBEDTLS_CIPHER_AES_256_CCM:
2224 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2225 *key_type = PSA_KEY_TYPE_AES;
2226 *key_size = 256;
2227 break;
2228 case MBEDTLS_CIPHER_AES_256_GCM:
2229 *alg = PSA_ALG_GCM;
2230 *key_type = PSA_KEY_TYPE_AES;
2231 *key_size = 256;
2232 break;
2233 case MBEDTLS_CIPHER_ARIA_128_CBC:
2234 *alg = PSA_ALG_CBC_NO_PADDING;
2235 *key_type = PSA_KEY_TYPE_ARIA;
2236 *key_size = 128;
2237 break;
2238 case MBEDTLS_CIPHER_ARIA_128_CCM:
2239 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2240 *key_type = PSA_KEY_TYPE_ARIA;
2241 *key_size = 128;
2242 break;
2243 case MBEDTLS_CIPHER_ARIA_128_GCM:
2244 *alg = PSA_ALG_GCM;
2245 *key_type = PSA_KEY_TYPE_ARIA;
2246 *key_size = 128;
2247 break;
2248 case MBEDTLS_CIPHER_ARIA_192_CCM:
2249 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2250 *key_type = PSA_KEY_TYPE_ARIA;
2251 *key_size = 192;
2252 break;
2253 case MBEDTLS_CIPHER_ARIA_192_GCM:
2254 *alg = PSA_ALG_GCM;
2255 *key_type = PSA_KEY_TYPE_ARIA;
2256 *key_size = 192;
2257 break;
2258 case MBEDTLS_CIPHER_ARIA_256_CBC:
2259 *alg = PSA_ALG_CBC_NO_PADDING;
2260 *key_type = PSA_KEY_TYPE_ARIA;
2261 *key_size = 256;
2262 break;
2263 case MBEDTLS_CIPHER_ARIA_256_CCM:
2264 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2265 *key_type = PSA_KEY_TYPE_ARIA;
2266 *key_size = 256;
2267 break;
2268 case MBEDTLS_CIPHER_ARIA_256_GCM:
2269 *alg = PSA_ALG_GCM;
2270 *key_type = PSA_KEY_TYPE_ARIA;
2271 *key_size = 256;
2272 break;
2273 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2274 *alg = PSA_ALG_CBC_NO_PADDING;
2275 *key_type = PSA_KEY_TYPE_CAMELLIA;
2276 *key_size = 128;
2277 break;
2278 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2279 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2280 *key_type = PSA_KEY_TYPE_CAMELLIA;
2281 *key_size = 128;
2282 break;
2283 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2284 *alg = PSA_ALG_GCM;
2285 *key_type = PSA_KEY_TYPE_CAMELLIA;
2286 *key_size = 128;
2287 break;
2288 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2289 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2290 *key_type = PSA_KEY_TYPE_CAMELLIA;
2291 *key_size = 192;
2292 break;
2293 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2294 *alg = PSA_ALG_GCM;
2295 *key_type = PSA_KEY_TYPE_CAMELLIA;
2296 *key_size = 192;
2297 break;
2298 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2299 *alg = PSA_ALG_CBC_NO_PADDING;
2300 *key_type = PSA_KEY_TYPE_CAMELLIA;
2301 *key_size = 256;
2302 break;
2303 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2304 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2305 *key_type = PSA_KEY_TYPE_CAMELLIA;
2306 *key_size = 256;
2307 break;
2308 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2309 *alg = PSA_ALG_GCM;
2310 *key_type = PSA_KEY_TYPE_CAMELLIA;
2311 *key_size = 256;
2312 break;
2313 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2314 *alg = PSA_ALG_CHACHA20_POLY1305;
2315 *key_type = PSA_KEY_TYPE_CHACHA20;
2316 *key_size = 256;
2317 break;
2318 case MBEDTLS_CIPHER_NULL:
2319 *alg = MBEDTLS_SSL_NULL_CIPHER;
2320 *key_type = 0;
2321 *key_size = 0;
2322 break;
2323 default:
2324 return PSA_ERROR_NOT_SUPPORTED;
2325 }
2326
2327 return PSA_SUCCESS;
2328}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002329#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002330
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002331#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002332int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2333 const unsigned char *dhm_P, size_t P_len,
2334 const unsigned char *dhm_G, size_t G_len )
2335{
Janos Follath865b3eb2019-12-16 11:46:15 +00002336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002337
Glenn Strausscee11292021-12-20 01:43:17 -05002338 mbedtls_mpi_free( &conf->dhm_P );
2339 mbedtls_mpi_free( &conf->dhm_G );
2340
Hanno Beckera90658f2017-10-04 15:29:08 +01002341 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2342 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2343 {
2344 mbedtls_mpi_free( &conf->dhm_P );
2345 mbedtls_mpi_free( &conf->dhm_G );
2346 return( ret );
2347 }
2348
2349 return( 0 );
2350}
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002352int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002353{
Janos Follath865b3eb2019-12-16 11:46:15 +00002354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002355
Glenn Strausscee11292021-12-20 01:43:17 -05002356 mbedtls_mpi_free( &conf->dhm_P );
2357 mbedtls_mpi_free( &conf->dhm_G );
2358
Gilles Peskinee5702482021-06-11 21:59:08 +02002359 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2360 &conf->dhm_P ) ) != 0 ||
2361 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2362 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002363 {
2364 mbedtls_mpi_free( &conf->dhm_P );
2365 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002366 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002367 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002368
2369 return( 0 );
2370}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002371#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002372
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002373#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2374/*
2375 * Set the minimum length for Diffie-Hellman parameters
2376 */
2377void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2378 unsigned int bitlen )
2379{
2380 conf->dhm_min_bitlen = bitlen;
2381}
2382#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2383
Gilles Peskineeccd8882020-03-10 12:19:08 +01002384#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002385#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002386/*
2387 * Set allowed/preferred hashes for handshake signatures
2388 */
2389void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2390 const int *hashes )
2391{
2392 conf->sig_hashes = hashes;
2393}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002394#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002395
Jerry Yuf017ee42022-01-12 15:49:48 +08002396/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002397void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2398 const uint16_t* sig_algs )
2399{
Jerry Yuf017ee42022-01-12 15:49:48 +08002400#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2401 conf->sig_hashes = NULL;
2402#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2403 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002404}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002405#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002406
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002407#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002408#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002409/*
2410 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002411 *
2412 * mbedtls_ssl_setup() takes the provided list
2413 * and translates it to a list of IANA TLS group identifiers,
2414 * stored in ssl->handshake->group_list.
2415 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002416 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002417void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002418 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002419{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002420 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002421 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002422}
Brett Warrene0edc842021-08-17 09:53:13 +01002423#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002424#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002425
Brett Warrene0edc842021-08-17 09:53:13 +01002426/*
2427 * Set the allowed groups
2428 */
2429void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2430 const uint16_t *group_list )
2431{
2432#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2433 conf->curve_list = NULL;
2434#endif
2435 conf->group_list = group_list;
2436}
2437
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002438#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440{
Hanno Becker947194e2017-04-07 13:25:49 +01002441 /* Initialize to suppress unnecessary compiler warning */
2442 size_t hostname_len = 0;
2443
2444 /* Check if new hostname is valid before
2445 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002446 if( hostname != NULL )
2447 {
2448 hostname_len = strlen( hostname );
2449
2450 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2452 }
2453
2454 /* Now it's clear that we will overwrite the old hostname,
2455 * so we can free it safely */
2456
2457 if( ssl->hostname != NULL )
2458 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002459 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002460 mbedtls_free( ssl->hostname );
2461 }
2462
2463 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002464
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002466 {
2467 ssl->hostname = NULL;
2468 }
2469 else
2470 {
2471 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002472 if( ssl->hostname == NULL )
2473 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002474
Hanno Becker947194e2017-04-07 13:25:49 +01002475 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002476
Hanno Becker947194e2017-04-07 13:25:49 +01002477 ssl->hostname[hostname_len] = '\0';
2478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
2480 return( 0 );
2481}
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002482
2483int mbedtls_ssl_reset_hostname( mbedtls_ssl_context *ssl,
2484 const char *hostname,
2485 const char *rec_hostname )
2486{
2487 /* Initialize to suppress unnecessary compiler warning */
2488 size_t rec_hostname_len = 0;
2489
2490 if( hostname == NULL || rec_hostname == NULL )
2491 return( 0 );
2492
2493 rec_hostname_len = strlen( rec_hostname );
2494 if( rec_hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2496
2497 if( rec_hostname_len == strlen( hostname ) &&
2498 memcmp( hostname, rec_hostname, rec_hostname_len ) == 0 )
2499 return( 0 );
2500
2501 if( ssl->hostname != NULL )
2502 {
2503 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
2504 mbedtls_free( ssl->hostname );
2505 ssl->hostname = NULL;
2506 ssl->hostname = mbedtls_calloc( 1, rec_hostname_len + 1 );
2507 if( ssl->hostname == NULL )
2508 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2509 memcpy( ssl->hostname, rec_hostname, rec_hostname_len );
2510 ssl->hostname[rec_hostname_len] = '\0';
2511 }
2512
2513 return( 0 );
2514}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002515#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002517#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002518void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002520 const unsigned char *, size_t),
2521 void *p_sni )
2522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002523 conf->f_sni = f_sni;
2524 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002529int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002530{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002531 size_t cur_len, tot_len;
2532 const char **p;
2533
2534 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002535 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2536 * MUST NOT be truncated."
2537 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002538 */
2539 tot_len = 0;
2540 for( p = protos; *p != NULL; p++ )
2541 {
2542 cur_len = strlen( *p );
2543 tot_len += cur_len;
2544
Ronald Cron8216dd32020-04-23 16:41:44 +02002545 if( ( cur_len == 0 ) ||
2546 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2547 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002549 }
2550
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002551 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002552
2553 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002554}
2555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002557{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002558 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002561
Johan Pascalb62bb512015-12-03 21:56:45 +01002562#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002563void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2564 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002565{
2566 conf->dtls_srtp_mki_support = support_mki_value;
2567}
2568
Ron Eldoref72faf2018-07-12 11:54:20 +03002569int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2570 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002571 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002572{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002573 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002574 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002576 }
Ron Eldor591f1622018-01-22 12:30:04 +02002577
2578 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002579 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002580 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002581 }
Ron Eldor591f1622018-01-22 12:30:04 +02002582
2583 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2584 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002585 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002586}
2587
Ron Eldoref72faf2018-07-12 11:54:20 +03002588int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002589 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002590{
Johan Pascal253d0262020-09-22 13:04:45 +02002591 const mbedtls_ssl_srtp_profile *p;
2592 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002593
Johan Pascal253d0262020-09-22 13:04:45 +02002594 /* check the profiles list: all entry must be valid,
2595 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002596 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2597 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2598 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002599 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002600 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002601 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002602 list_size++;
2603 }
2604 else
2605 {
2606 /* unsupported value, stop parsing and set the size to an error value */
2607 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002608 }
2609 }
2610
Johan Pascal5ef72d22020-10-28 17:05:47 +01002611 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002612 {
Johan Pascal253d0262020-09-22 13:04:45 +02002613 conf->dtls_srtp_profile_list = NULL;
2614 conf->dtls_srtp_profile_list_len = 0;
2615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2616 }
2617
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002618 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002619 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002620
2621 return( 0 );
2622}
2623
Johan Pascal5ef72d22020-10-28 17:05:47 +01002624void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2625 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002626{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002627 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2628 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002629 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002630 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002631 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002632 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002633 else
2634 {
2635 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002636 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2637 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002638 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002639}
Johan Pascalb62bb512015-12-03 21:56:45 +01002640#endif /* MBEDTLS_SSL_DTLS_SRTP */
2641
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302642#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002643void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002644{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002645 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002646}
2647
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002648void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002649{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002650 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002651}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302652#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002653
Janos Follath088ce432017-04-10 12:42:31 +01002654#if defined(MBEDTLS_SSL_SRV_C)
2655void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2656 char cert_req_ca_list )
2657{
2658 conf->cert_req_ca_list = cert_req_ca_list;
2659}
2660#endif
2661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002663void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002665 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002666}
2667#endif
2668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002670void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002672 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002673}
2674#endif
2675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002677int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002680 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002683 }
2684
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002685 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002686
2687 return( 0 );
2688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002691void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002693 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002694}
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002697void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002698{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002699 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002700}
2701
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002702void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002704 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002705}
2706
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002707void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002708 const unsigned char period[8] )
2709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002710 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002715#if defined(MBEDTLS_SSL_CLI_C)
2716void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002717{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002718 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002719}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002720#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002721
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002722#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002723
Jerry Yud0766ec2022-09-22 10:46:57 +08002724#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002725void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2726 uint16_t num_tickets )
2727{
Jerry Yud0766ec2022-09-22 10:46:57 +08002728 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002729}
2730#endif
2731
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002732void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2733 mbedtls_ssl_ticket_write_t *f_ticket_write,
2734 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2735 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002736{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002737 conf->f_ticket_write = f_ticket_write;
2738 conf->f_ticket_parse = f_ticket_parse;
2739 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002740}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002743
Hanno Becker7e6c1782021-06-08 09:24:55 +01002744void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2745 mbedtls_ssl_export_keys_t *f_export_keys,
2746 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002747{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002748 ssl->f_export_keys = f_export_keys;
2749 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002750}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002751
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002752#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002753void mbedtls_ssl_conf_async_private_cb(
2754 mbedtls_ssl_config *conf,
2755 mbedtls_ssl_async_sign_t *f_async_sign,
2756 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2757 mbedtls_ssl_async_resume_t *f_async_resume,
2758 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002759 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002760{
2761 conf->f_async_sign_start = f_async_sign;
2762 conf->f_async_decrypt_start = f_async_decrypt;
2763 conf->f_async_resume = f_async_resume;
2764 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002765 conf->p_async_config_data = async_config_data;
2766}
2767
Gilles Peskine8f97af72018-04-26 11:46:10 +02002768void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2769{
2770 return( conf->p_async_config_data );
2771}
2772
Gilles Peskine1febfef2018-04-30 11:54:39 +02002773void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002774{
2775 if( ssl->handshake == NULL )
2776 return( NULL );
2777 else
2778 return( ssl->handshake->user_async_ctx );
2779}
2780
Gilles Peskine1febfef2018-04-30 11:54:39 +02002781void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002782 void *ctx )
2783{
2784 if( ssl->handshake != NULL )
2785 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002786}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002787#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789/*
2790 * SSL get accessors
2791 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002792uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002793{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002794 if( ssl->session != NULL )
2795 return( ssl->session->verify_result );
2796
2797 if( ssl->session_negotiate != NULL )
2798 return( ssl->session_negotiate->verify_result );
2799
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002800 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801}
2802
Glenn Strauss8f526902022-01-13 00:04:49 -05002803int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2804{
2805 if( ssl == NULL || ssl->session == NULL )
2806 return( 0 );
2807
2808 return( ssl->session->ciphersuite );
2809}
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002812{
Paul Bakker926c8e42013-03-06 10:23:34 +01002813 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002814 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002817}
2818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002823 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002824 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002825 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002826 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002827 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002828 default:
2829 return( "unknown (DTLS)" );
2830 }
2831 }
2832#endif
2833
Glenn Strauss60bfe602022-03-14 19:04:24 -04002834 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002835 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002836 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002838 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002839 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002840 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002841 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002842 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002843}
2844
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002845#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002846size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2847{
David Horstmann95d516f2021-05-04 18:36:56 +01002848 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002849 size_t read_mfl;
2850
2851 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2852 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2853 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2854 {
2855 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2856 }
2857
2858 /* Check if a smaller max length was negotiated */
2859 if( ssl->session_out != NULL )
2860 {
2861 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2862 if( read_mfl < max_len )
2863 {
2864 max_len = read_mfl;
2865 }
2866 }
2867
2868 // During a handshake, use the value being negotiated
2869 if( ssl->session_negotiate != NULL )
2870 {
2871 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2872 if( read_mfl < max_len )
2873 {
2874 max_len = read_mfl;
2875 }
2876 }
2877
2878 return( max_len );
2879}
2880
2881size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002882{
2883 size_t max_len;
2884
2885 /*
2886 * Assume mfl_code is correct since it was checked when set
2887 */
Angus Grattond8213d02016-05-25 20:56:48 +10002888 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002889
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002890 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002891 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002892 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002893 {
Angus Grattond8213d02016-05-25 20:56:48 +10002894 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002895 }
2896
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002897 /* During a handshake, use the value being negotiated */
2898 if( ssl->session_negotiate != NULL &&
2899 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2900 {
2901 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2902 }
2903
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002904 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002905}
2906#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2907
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002909size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002910{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002911 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2912 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2913 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2914 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2915 return ( 0 );
2916
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002917 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2918 return( ssl->mtu );
2919
2920 if( ssl->mtu == 0 )
2921 return( ssl->handshake->mtu );
2922
2923 return( ssl->mtu < ssl->handshake->mtu ?
2924 ssl->mtu : ssl->handshake->mtu );
2925}
2926#endif /* MBEDTLS_SSL_PROTO_DTLS */
2927
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002928int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2929{
2930 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2931
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002932#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2933 !defined(MBEDTLS_SSL_PROTO_DTLS)
2934 (void) ssl;
2935#endif
2936
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002937#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002938 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002939
2940 if( max_len > mfl )
2941 max_len = mfl;
2942#endif
2943
2944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002945 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002946 {
Hanno Becker89490712020-02-05 10:50:12 +00002947 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002948 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2949 const size_t overhead = (size_t) ret;
2950
2951 if( ret < 0 )
2952 return( ret );
2953
2954 if( mtu <= overhead )
2955 {
2956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2957 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2958 }
2959
2960 if( max_len > mtu - overhead )
2961 max_len = mtu - overhead;
2962 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002963#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002964
Hanno Becker0defedb2018-08-10 12:35:02 +01002965#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2966 !defined(MBEDTLS_SSL_PROTO_DTLS)
2967 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002968#endif
2969
2970 return( (int) max_len );
2971}
2972
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002973int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2974{
2975 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2976
2977#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2978 (void) ssl;
2979#endif
2980
2981#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2982 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2983
2984 if( max_len > mfl )
2985 max_len = mfl;
2986#endif
2987
2988 return( (int) max_len );
2989}
2990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_X509_CRT_PARSE_C)
2992const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002993{
2994 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002995 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002996
Hanno Beckere6824572019-02-07 13:18:46 +00002997#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002998 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002999#else
3000 return( NULL );
3001#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003006int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3007 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003008{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003009 int ret;
3010
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003011 if( ssl == NULL ||
3012 dst == NULL ||
3013 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003014 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003017 }
3018
Hanno Beckere810bbc2021-05-14 16:01:05 +01003019 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3020 * idempotent: Each session can only be exported once.
3021 *
3022 * (This is in preparation for TLS 1.3 support where we will
3023 * need the ability to export multiple sessions (aka tickets),
3024 * which will be achieved by calling mbedtls_ssl_get_session()
3025 * multiple times until it fails.)
3026 *
3027 * Check whether we have already exported the current session,
3028 * and fail if so.
3029 */
3030 if( ssl->session->exported == 1 )
3031 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3032
3033 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3034 if( ret != 0 )
3035 return( ret );
3036
3037 /* Remember that we've exported the session. */
3038 ssl->session->exported = 1;
3039 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003042
Paul Bakker5121ce52009-01-03 21:22:43 +00003043/*
Hanno Beckera835da52019-05-16 12:39:07 +01003044 * Define ticket header determining Mbed TLS version
3045 * and structure of the ticket.
3046 */
3047
Hanno Becker94ef3b32019-05-16 12:50:45 +01003048/*
Hanno Becker50b59662019-05-28 14:30:45 +01003049 * Define bitflag determining compile-time settings influencing
3050 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003051 */
3052
Hanno Becker50b59662019-05-28 14:30:45 +01003053#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003054#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003055#else
Hanno Becker3e088662019-05-29 11:10:18 +01003056#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003057#endif /* MBEDTLS_HAVE_TIME */
3058
3059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003060#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003061#else
Hanno Becker3e088662019-05-29 11:10:18 +01003062#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003063#endif /* MBEDTLS_X509_CRT_PARSE_C */
3064
3065#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003066#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003067#else
Hanno Becker3e088662019-05-29 11:10:18 +01003068#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003069#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3070
3071#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003072#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003073#else
Hanno Becker3e088662019-05-29 11:10:18 +01003074#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003075#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3076
Hanno Becker94ef3b32019-05-16 12:50:45 +01003077#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003078#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003079#else
Hanno Becker3e088662019-05-29 11:10:18 +01003080#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003081#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3082
Hanno Becker94ef3b32019-05-16 12:50:45 +01003083#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3084#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3085#else
3086#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3087#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3088
Hanno Becker3e088662019-05-29 11:10:18 +01003089#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3090#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3091#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3092#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003093#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3094#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003095
Hanno Becker50b59662019-05-28 14:30:45 +01003096#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003097 ( (uint16_t) ( \
3098 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3099 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3100 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3101 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003102 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003103 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003104
Hanno Beckerf8787072019-05-16 12:41:07 +01003105static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003106 MBEDTLS_VERSION_MAJOR,
3107 MBEDTLS_VERSION_MINOR,
3108 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003109 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3110 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003111};
Hanno Beckera835da52019-05-16 12:39:07 +01003112
3113/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003114 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003115 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003116 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003117 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003118 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003119 * opaque mbedtls_version[3]; // library version: major, minor, patch
3120 * opaque session_format[2]; // library-version specific 16-bit field
3121 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003122 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003123 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003124 * Note: When updating the format, remember to keep
3125 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003126 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003127 * // In this version, `session_format` determines
3128 * // the setting of those compile-time
3129 * // configuration options which influence
3130 * // the structure of mbedtls_ssl_session.
3131 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003132 * uint8_t minor_ver; // Protocol minor version. Possible values:
3133 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003134 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003135 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003136 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003137 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003138 * serialized_session_tls12 data;
3139 *
3140 * };
3141 *
3142 * } serialized_session;
3143 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003144 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003145
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003146MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003147static int ssl_session_save( const mbedtls_ssl_session *session,
3148 unsigned char omit_header,
3149 unsigned char *buf,
3150 size_t buf_len,
3151 size_t *olen )
3152{
3153 unsigned char *p = buf;
3154 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003155 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003156#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3157 size_t out_len;
3158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3159#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003160 if( session == NULL )
3161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3162
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003163 if( !omit_header )
3164 {
3165 /*
3166 * Add Mbed TLS version identifier
3167 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003168 used += sizeof( ssl_serialized_session_header );
3169
3170 if( used <= buf_len )
3171 {
3172 memcpy( p, ssl_serialized_session_header,
3173 sizeof( ssl_serialized_session_header ) );
3174 p += sizeof( ssl_serialized_session_header );
3175 }
3176 }
3177
3178 /*
3179 * TLS version identifier
3180 */
3181 used += 1;
3182 if( used <= buf_len )
3183 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003184 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003185 }
3186
3187 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003188 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003189 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003190 {
3191#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003192 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003193 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003194 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003195#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3196
Jerry Yu251a12e2022-07-13 15:15:48 +08003197#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3198 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003199 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3200 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003201 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003202 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003203 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003204#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3205
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003206 default:
3207 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3208 }
3209
3210 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003211 if( used > buf_len )
3212 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003213
3214 return( 0 );
3215}
3216
3217/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003218 * Public wrapper for ssl_session_save()
3219 */
3220int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3221 unsigned char *buf,
3222 size_t buf_len,
3223 size_t *olen )
3224{
3225 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3226}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003227
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003228/*
3229 * Deserialize session, see mbedtls_ssl_session_save() for format.
3230 *
3231 * This internal version is wrapped by a public function that cleans up in
3232 * case of error, and has an extra option omit_header.
3233 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003234MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003235static int ssl_session_load( mbedtls_ssl_session *session,
3236 unsigned char omit_header,
3237 const unsigned char *buf,
3238 size_t len )
3239{
3240 const unsigned char *p = buf;
3241 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003242 size_t remaining_len;
3243
3244
3245 if( session == NULL )
3246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003247
3248 if( !omit_header )
3249 {
3250 /*
3251 * Check Mbed TLS version identifier
3252 */
3253
3254 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3256
3257 if( memcmp( p, ssl_serialized_session_header,
3258 sizeof( ssl_serialized_session_header ) ) != 0 )
3259 {
3260 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3261 }
3262 p += sizeof( ssl_serialized_session_header );
3263 }
3264
3265 /*
3266 * TLS version identifier
3267 */
3268 if( 1 > (size_t)( end - p ) )
3269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003270 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003271
3272 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003273 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003274 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003275 {
3276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003277 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003278 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003279#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3280
Jerry Yu438ddd82022-07-07 06:55:50 +00003281#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3282 case MBEDTLS_SSL_VERSION_TLS1_3:
3283 return( ssl_tls13_session_load( session, p, remaining_len ) );
3284#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3285
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003286 default:
3287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3288 }
3289}
3290
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003291/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003292 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003293 */
3294int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3295 const unsigned char *buf,
3296 size_t len )
3297{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003298 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003299
3300 if( ret != 0 )
3301 mbedtls_ssl_session_free( session );
3302
3303 return( ret );
3304}
3305
3306/*
Paul Bakker1961b702013-01-25 14:49:24 +01003307 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003309MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003310static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3311{
3312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3313
Ronald Cron66dbf912022-02-02 15:33:46 +01003314 /*
3315 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003316 * that were written into the output buffer by the previous handshake step,
3317 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003318 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3319 * We proceed to the next handshake step only when all data from the
3320 * previous one have been sent to the peer, thus we make sure that this is
3321 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3322 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3323 * we have to wait before to go ahead.
3324 * In the case of TLS 1.3, handshake step handlers do not send data to the
3325 * peer. Data are only sent here and through
3326 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003327 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003328 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003329 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3330 return( ret );
3331
3332#if defined(MBEDTLS_SSL_PROTO_DTLS)
3333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3334 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3335 {
3336 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3337 return( ret );
3338 }
3339#endif /* MBEDTLS_SSL_PROTO_DTLS */
3340
3341 return( ret );
3342}
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003345{
Hanno Becker41934dd2021-08-07 19:13:43 +01003346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003347
Hanno Becker41934dd2021-08-07 19:13:43 +01003348 if( ssl == NULL ||
3349 ssl->conf == NULL ||
3350 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003351 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003352 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003354 }
3355
3356 ret = ssl_prepare_handshake_step( ssl );
3357 if( ret != 0 )
3358 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003359
Jerry Yue7047812021-09-13 19:26:39 +08003360 ret = mbedtls_ssl_handle_pending_alert( ssl );
3361 if( ret != 0 )
3362 goto cleanup;
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003365 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003366 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3368 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003369
Ronald Cron9f0fba32022-02-10 16:45:15 +01003370 switch( ssl->state )
3371 {
3372 case MBEDTLS_SSL_HELLO_REQUEST:
3373 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3374 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003375
Ronald Cron9f0fba32022-02-10 16:45:15 +01003376 case MBEDTLS_SSL_CLIENT_HELLO:
3377 ret = mbedtls_ssl_write_client_hello( ssl );
3378 break;
3379
3380 default:
3381#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003382 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003383 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3384 else
3385 ret = mbedtls_ssl_handshake_client_step( ssl );
3386#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3387 ret = mbedtls_ssl_handshake_client_step( ssl );
3388#else
3389 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3390#endif
3391 }
Jerry Yub9930e72021-08-06 17:11:51 +08003392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003393#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003396 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003397#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003398 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003399 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003400#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003401
3402#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3403 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3404 ret = mbedtls_ssl_handshake_server_step( ssl );
3405#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003407#endif
3408
Jerry Yue7047812021-09-13 19:26:39 +08003409 if( ret != 0 )
3410 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003411 /* handshake_step return error. And it is same
3412 * with alert_reason.
3413 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003414 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003415 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003416 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003417 goto cleanup;
3418 }
3419 }
3420
3421cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003422 return( ret );
3423}
3424
3425/*
3426 * Perform the SSL handshake
3427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003429{
3430 int ret = 0;
3431
Hanno Beckera817ea42020-10-20 15:20:23 +01003432 /* Sanity checks */
3433
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003434 if( ssl == NULL || ssl->conf == NULL )
3435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3436
Hanno Beckera817ea42020-10-20 15:20:23 +01003437#if defined(MBEDTLS_SSL_PROTO_DTLS)
3438 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3439 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3440 {
3441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3442 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3444 }
3445#endif /* MBEDTLS_SSL_PROTO_DTLS */
3446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003448
Hanno Beckera817ea42020-10-20 15:20:23 +01003449 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003450 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003453
3454 if( ret != 0 )
3455 break;
3456 }
3457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
3460 return( ret );
3461}
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463#if defined(MBEDTLS_SSL_RENEGOTIATION)
3464#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003465/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003466 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003467 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003468MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003470{
Janos Follath865b3eb2019-12-16 11:46:15 +00003471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003474
3475 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3477 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003478
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003479 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003480 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003482 return( ret );
3483 }
3484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003486
3487 return( 0 );
3488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003490
3491/*
3492 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 * - any side: calling mbedtls_ssl_renegotiate(),
3494 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3495 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003496 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003497 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003499 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003500int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003501{
Janos Follath865b3eb2019-12-16 11:46:15 +00003502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003505
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003506 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3507 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003508
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003509 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3510 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003514 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003515 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003516 ssl->handshake->out_msg_seq = 1;
3517 else
3518 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003519 }
3520#endif
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3523 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003528 return( ret );
3529 }
3530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003532
3533 return( 0 );
3534}
3535
3536/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003537 * Renegotiate current connection on client,
3538 * or request renegotiation on server
3539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003543
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003544 if( ssl == NULL || ssl->conf == NULL )
3545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003550 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003551 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003555
3556 /* Did we already try/start sending HelloRequest? */
3557 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003559
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003560 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003561 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003565 /*
3566 * On client, either start the renegotiation process or,
3567 * if already in progress, continue the handshake
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003570 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003571 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003573
Hanno Becker40cdaa12020-02-05 10:48:27 +00003574 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003575 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003577 return( ret );
3578 }
3579 }
3580 else
3581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003585 return( ret );
3586 }
3587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003589
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003590 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003593
Gilles Peskine9b562d52018-04-25 20:32:43 +02003594void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003595{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003596 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3597
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003598 if( handshake == NULL )
3599 return;
3600
Brett Warrene0edc842021-08-17 09:53:13 +01003601#if defined(MBEDTLS_ECP_C)
3602#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3603 if ( ssl->handshake->group_list_heap_allocated )
3604 mbedtls_free( (void*) handshake->group_list );
3605 handshake->group_list = NULL;
3606#endif /* MBEDTLS_DEPRECATED_REMOVED */
3607#endif /* MBEDTLS_ECP_C */
3608
Jerry Yuf017ee42022-01-12 15:49:48 +08003609#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3610#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3611 if ( ssl->handshake->sig_algs_heap_allocated )
3612 mbedtls_free( (void*) handshake->sig_algs );
3613 handshake->sig_algs = NULL;
3614#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003615#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3616 if( ssl->handshake->certificate_request_context )
3617 {
3618 mbedtls_free( (void*) handshake->certificate_request_context );
3619 }
3620#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003621#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3622
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003623#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3624 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3625 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003626 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003627 handshake->async_in_progress = 0;
3628 }
3629#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3630
Andrzej Kurek25f27152022-08-17 16:09:31 -04003631#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003632#if defined(MBEDTLS_USE_PSA_CRYPTO)
3633 psa_hash_abort( &handshake->fin_sha256_psa );
3634#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003635 mbedtls_sha256_free( &handshake->fin_sha256 );
3636#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003637#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003638#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003639#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003640 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003641#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003642 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003643#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003644#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646#if defined(MBEDTLS_DHM_C)
3647 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003648#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003649#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003651#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003652#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003653 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003654#if defined(MBEDTLS_SSL_CLI_C)
3655 mbedtls_free( handshake->ecjpake_cache );
3656 handshake->ecjpake_cache = NULL;
3657 handshake->ecjpake_cache_len = 0;
3658#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003659#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003660
Janos Follath4ae5c292016-02-10 11:27:43 +00003661#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3662 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003663 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003665#endif
3666
Gilles Peskineeccd8882020-03-10 12:19:08 +01003667#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003668#if defined(MBEDTLS_USE_PSA_CRYPTO)
3669 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3670 {
3671 /* The maintenance of the external PSK key slot is the
3672 * user's responsibility. */
3673 if( ssl->handshake->psk_opaque_is_internal )
3674 {
3675 psa_destroy_key( ssl->handshake->psk_opaque );
3676 ssl->handshake->psk_opaque_is_internal = 0;
3677 }
3678 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3679 }
Neil Armstronge952a302022-05-03 10:22:14 +02003680#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003681 if( handshake->psk != NULL )
3682 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003683 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003684 mbedtls_free( handshake->psk );
3685 }
Neil Armstronge952a302022-05-03 10:22:14 +02003686#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003687#endif
3688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3690 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003691 /*
3692 * Free only the linked list wrapper, not the keys themselves
3693 * since the belong to the SNI callback
3694 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003695 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003697
Gilles Peskineeccd8882020-03-10 12:19:08 +01003698#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003699 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003700 if( handshake->ecrs_peer_cert != NULL )
3701 {
3702 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3703 mbedtls_free( handshake->ecrs_peer_cert );
3704 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003705#endif
3706
Hanno Becker75173122019-02-06 16:18:31 +00003707#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3708 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3709 mbedtls_pk_free( &handshake->peer_pubkey );
3710#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3711
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003712#if defined(MBEDTLS_SSL_CLI_C) && \
3713 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3714 mbedtls_free( handshake->cookie );
3715#endif /* MBEDTLS_SSL_CLI_C &&
3716 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003717
3718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003719 mbedtls_ssl_flight_free( handshake->flight );
3720 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003721#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003722
Ronald Cronf12b81d2022-03-15 10:42:41 +01003723#if defined(MBEDTLS_ECDH_C) && \
3724 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003725 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003726 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003727#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3728
Ronald Cron6f135e12021-12-08 16:57:54 +01003729#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003730 mbedtls_ssl_transform_free( handshake->transform_handshake );
3731 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003732 mbedtls_free( handshake->transform_earlydata );
3733 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003734#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003735
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003736
3737#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3738 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3739 * processes datagrams and the fact that a datagram is allowed to have
3740 * several records in it, it is possible that the I/O buffers are not
3741 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003742 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3743 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003744#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003745
Jerry Yuba9c7272021-10-30 11:54:10 +08003746 /* mbedtls_platform_zeroize MUST be last one in this function */
3747 mbedtls_platform_zeroize( handshake,
3748 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003749}
3750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003752{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003753 if( session == NULL )
3754 return;
3755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003757 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003758#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003759
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003760#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003762#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003763
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003764#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3765 mbedtls_free( session->hostname );
3766#endif
3767
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003768 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003769}
3770
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003771#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003772
3773#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3774#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3775#else
3776#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3777#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3778
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003779#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003780
3781#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3782#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3783#else
3784#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3785#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3786
3787#if defined(MBEDTLS_SSL_ALPN)
3788#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3789#else
3790#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3791#endif /* MBEDTLS_SSL_ALPN */
3792
3793#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3794#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3795#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3796#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3797
3798#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3799 ( (uint32_t) ( \
3800 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3801 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3802 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3803 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3804 0u ) )
3805
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003806static unsigned char ssl_serialized_context_header[] = {
3807 MBEDTLS_VERSION_MAJOR,
3808 MBEDTLS_VERSION_MINOR,
3809 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003810 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3811 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3812 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3813 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3814 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003815};
3816
Paul Bakker5121ce52009-01-03 21:22:43 +00003817/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003818 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003819 *
3820 * The format of the serialized data is:
3821 * (in the presentation language of TLS, RFC 8446 section 3)
3822 *
3823 * // header
3824 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003825 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003826 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003827 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003828 * Note: When updating the format, remember to keep these
3829 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003830 *
3831 * // session sub-structure
3832 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3833 * // transform sub-structure
3834 * uint8 random[64]; // ServerHello.random+ClientHello.random
3835 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3836 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3837 * // fields from ssl_context
3838 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3839 * uint64 in_window_top; // DTLS: last validated record seq_num
3840 * uint64 in_window; // DTLS: bitmask for replay protection
3841 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3842 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3843 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3844 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3845 *
3846 * Note that many fields of the ssl_context or sub-structures are not
3847 * serialized, as they fall in one of the following categories:
3848 *
3849 * 1. forced value (eg in_left must be 0)
3850 * 2. pointer to dynamically-allocated memory (eg session, transform)
3851 * 3. value can be re-derived from other data (eg session keys from MS)
3852 * 4. value was temporary (eg content of input buffer)
3853 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003854 */
3855int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3856 unsigned char *buf,
3857 size_t buf_len,
3858 size_t *olen )
3859{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003860 unsigned char *p = buf;
3861 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003862 size_t session_len;
3863 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003864
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003865 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003866 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3867 * this function's documentation.
3868 *
3869 * These are due to assumptions/limitations in the implementation. Some of
3870 * them are likely to stay (no handshake in progress) some might go away
3871 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003872 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003873 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003874 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003875 {
3876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003878 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003879 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003880 {
3881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003883 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 /* Double-check that sub-structures are indeed ready */
3885 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003886 {
3887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003889 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003890 /* There must be no pending incoming or outgoing data */
3891 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003892 {
3893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003895 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003896 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 {
3898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003900 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003901 /* Protocol must be DLTS, not TLS */
3902 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003903 {
3904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003906 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003907 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003908 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003909 {
3910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003912 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003913 /* We must be using an AEAD ciphersuite */
3914 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003915 {
3916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003918 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003919 /* Renegotiation must not be enabled */
3920#if defined(MBEDTLS_SSL_RENEGOTIATION)
3921 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003922 {
3923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003925 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003926#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003927
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003928 /*
3929 * Version and format identifier
3930 */
3931 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003932
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003933 if( used <= buf_len )
3934 {
3935 memcpy( p, ssl_serialized_context_header,
3936 sizeof( ssl_serialized_context_header ) );
3937 p += sizeof( ssl_serialized_context_header );
3938 }
3939
3940 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003941 * Session (length + data)
3942 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003943 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003944 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3945 return( ret );
3946
3947 used += 4 + session_len;
3948 if( used <= buf_len )
3949 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003950 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3951 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003952
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003953 ret = ssl_session_save( ssl->session, 1,
3954 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003955 if( ret != 0 )
3956 return( ret );
3957
3958 p += session_len;
3959 }
3960
3961 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003962 * Transform
3963 */
3964 used += sizeof( ssl->transform->randbytes );
3965 if( used <= buf_len )
3966 {
3967 memcpy( p, ssl->transform->randbytes,
3968 sizeof( ssl->transform->randbytes ) );
3969 p += sizeof( ssl->transform->randbytes );
3970 }
3971
3972#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3973 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3974 if( used <= buf_len )
3975 {
3976 *p++ = ssl->transform->in_cid_len;
3977 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3978 p += ssl->transform->in_cid_len;
3979
3980 *p++ = ssl->transform->out_cid_len;
3981 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3982 p += ssl->transform->out_cid_len;
3983 }
3984#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3985
3986 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003987 * Saved fields from top-level ssl_context structure
3988 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003989 used += 4;
3990 if( used <= buf_len )
3991 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003992 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3993 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003994 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003995
3996#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3997 used += 16;
3998 if( used <= buf_len )
3999 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004000 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4001 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004002
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004003 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4004 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004005 }
4006#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4007
4008#if defined(MBEDTLS_SSL_PROTO_DTLS)
4009 used += 1;
4010 if( used <= buf_len )
4011 {
4012 *p++ = ssl->disable_datagram_packing;
4013 }
4014#endif /* MBEDTLS_SSL_PROTO_DTLS */
4015
Jerry Yuae0b2e22021-10-08 15:21:19 +08004016 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004017 if( used <= buf_len )
4018 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004019 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4020 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004021 }
4022
4023#if defined(MBEDTLS_SSL_PROTO_DTLS)
4024 used += 2;
4025 if( used <= buf_len )
4026 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004027 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4028 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004029 }
4030#endif /* MBEDTLS_SSL_PROTO_DTLS */
4031
4032#if defined(MBEDTLS_SSL_ALPN)
4033 {
4034 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004035 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004036 : 0;
4037
4038 used += 1 + alpn_len;
4039 if( used <= buf_len )
4040 {
4041 *p++ = alpn_len;
4042
4043 if( ssl->alpn_chosen != NULL )
4044 {
4045 memcpy( p, ssl->alpn_chosen, alpn_len );
4046 p += alpn_len;
4047 }
4048 }
4049 }
4050#endif /* MBEDTLS_SSL_ALPN */
4051
4052 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004053 * Done
4054 */
4055 *olen = used;
4056
4057 if( used > buf_len )
4058 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004059
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004060 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4061
Hanno Becker43aefe22020-02-05 10:44:56 +00004062 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004063}
4064
4065/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004066 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004067 *
4068 * This internal version is wrapped by a public function that cleans up in
4069 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004071MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004072static int ssl_context_load( mbedtls_ssl_context *ssl,
4073 const unsigned char *buf,
4074 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004075{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004076 const unsigned char *p = buf;
4077 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004078 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004080
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004081 /*
4082 * The context should have been freshly setup or reset.
4083 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004084 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004085 * renegotiating, or if the user mistakenly loaded a session first.)
4086 */
4087 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4088 ssl->session != NULL )
4089 {
4090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4091 }
4092
4093 /*
4094 * We can't check that the config matches the initial one, but we can at
4095 * least check it matches the requirements for serializing.
4096 */
4097 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004098 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4099 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004101 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004102#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004103 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004104 {
4105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4106 }
4107
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004108 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4109
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004110 /*
4111 * Check version identifier
4112 */
4113 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4115
4116 if( memcmp( p, ssl_serialized_context_header,
4117 sizeof( ssl_serialized_context_header ) ) != 0 )
4118 {
4119 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4120 }
4121 p += sizeof( ssl_serialized_context_header );
4122
4123 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004124 * Session
4125 */
4126 if( (size_t)( end - p ) < 4 )
4127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4128
4129 session_len = ( (size_t) p[0] << 24 ) |
4130 ( (size_t) p[1] << 16 ) |
4131 ( (size_t) p[2] << 8 ) |
4132 ( (size_t) p[3] );
4133 p += 4;
4134
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004135 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004136 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004137 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004138 ssl->session_in = ssl->session;
4139 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004140 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004141
4142 if( (size_t)( end - p ) < session_len )
4143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4144
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004145 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004146 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004147 {
4148 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004149 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004150 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004151
4152 p += session_len;
4153
4154 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004155 * Transform
4156 */
4157
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004158 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004159 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004160 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004161 ssl->transform_in = ssl->transform;
4162 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004163 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004164
4165 /* Read random bytes and populate structure */
4166 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004169 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004170 ssl->session->ciphersuite,
4171 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004172#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004173 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004174#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004175 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4176 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004177 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004178 ssl->conf->endpoint,
4179 ssl );
4180 if( ret != 0 )
4181 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004183 p += sizeof( ssl->transform->randbytes );
4184
4185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4186 /* Read connection IDs and store them */
4187 if( (size_t)( end - p ) < 1 )
4188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4189
4190 ssl->transform->in_cid_len = *p++;
4191
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004192 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4194
4195 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4196 p += ssl->transform->in_cid_len;
4197
4198 ssl->transform->out_cid_len = *p++;
4199
4200 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4202
4203 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4204 p += ssl->transform->out_cid_len;
4205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4206
4207 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004208 * Saved fields from top-level ssl_context structure
4209 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004210 if( (size_t)( end - p ) < 4 )
4211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4212
4213 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4214 ( (uint32_t) p[1] << 16 ) |
4215 ( (uint32_t) p[2] << 8 ) |
4216 ( (uint32_t) p[3] );
4217 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004218
4219#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4220 if( (size_t)( end - p ) < 16 )
4221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4222
4223 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4224 ( (uint64_t) p[1] << 48 ) |
4225 ( (uint64_t) p[2] << 40 ) |
4226 ( (uint64_t) p[3] << 32 ) |
4227 ( (uint64_t) p[4] << 24 ) |
4228 ( (uint64_t) p[5] << 16 ) |
4229 ( (uint64_t) p[6] << 8 ) |
4230 ( (uint64_t) p[7] );
4231 p += 8;
4232
4233 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4234 ( (uint64_t) p[1] << 48 ) |
4235 ( (uint64_t) p[2] << 40 ) |
4236 ( (uint64_t) p[3] << 32 ) |
4237 ( (uint64_t) p[4] << 24 ) |
4238 ( (uint64_t) p[5] << 16 ) |
4239 ( (uint64_t) p[6] << 8 ) |
4240 ( (uint64_t) p[7] );
4241 p += 8;
4242#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4243
4244#if defined(MBEDTLS_SSL_PROTO_DTLS)
4245 if( (size_t)( end - p ) < 1 )
4246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4247
4248 ssl->disable_datagram_packing = *p++;
4249#endif /* MBEDTLS_SSL_PROTO_DTLS */
4250
Jerry Yud9a94fe2021-09-28 18:58:59 +08004251 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004253 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4254 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004255
4256#if defined(MBEDTLS_SSL_PROTO_DTLS)
4257 if( (size_t)( end - p ) < 2 )
4258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4259
4260 ssl->mtu = ( p[0] << 8 ) | p[1];
4261 p += 2;
4262#endif /* MBEDTLS_SSL_PROTO_DTLS */
4263
4264#if defined(MBEDTLS_SSL_ALPN)
4265 {
4266 uint8_t alpn_len;
4267 const char **cur;
4268
4269 if( (size_t)( end - p ) < 1 )
4270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4271
4272 alpn_len = *p++;
4273
4274 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4275 {
4276 /* alpn_chosen should point to an item in the configured list */
4277 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4278 {
4279 if( strlen( *cur ) == alpn_len &&
4280 memcmp( p, cur, alpn_len ) == 0 )
4281 {
4282 ssl->alpn_chosen = *cur;
4283 break;
4284 }
4285 }
4286 }
4287
4288 /* can only happen on conf mismatch */
4289 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4291
4292 p += alpn_len;
4293 }
4294#endif /* MBEDTLS_SSL_ALPN */
4295
4296 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004297 * Forced fields from top-level ssl_context structure
4298 *
4299 * Most of them already set to the correct value by mbedtls_ssl_init() and
4300 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4301 */
4302 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004303 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004304
Hanno Becker361b10d2019-08-30 10:42:49 +01004305 /* Adjust pointers for header fields of outgoing records to
4306 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004307 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004308
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004309#if defined(MBEDTLS_SSL_PROTO_DTLS)
4310 ssl->in_epoch = 1;
4311#endif
4312
4313 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4314 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004315 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4316 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004317 if( ssl->handshake != NULL )
4318 {
4319 mbedtls_ssl_handshake_free( ssl );
4320 mbedtls_free( ssl->handshake );
4321 ssl->handshake = NULL;
4322 }
4323
4324 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004325 * Done - should have consumed entire buffer
4326 */
4327 if( p != end )
4328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004329
4330 return( 0 );
4331}
4332
4333/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004334 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004335 */
4336int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4337 const unsigned char *buf,
4338 size_t len )
4339{
4340 int ret = ssl_context_load( context, buf, len );
4341
4342 if( ret != 0 )
4343 mbedtls_ssl_free( context );
4344
4345 return( ret );
4346}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004347#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004348
4349/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 * Free an SSL context
4351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004353{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004354 if( ssl == NULL )
4355 return;
4356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004358
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004359 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 {
sander-visserb8aa2072020-05-06 22:05:13 +02004361#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4362 size_t out_buf_len = ssl->out_buf_len;
4363#else
4364 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4365#endif
4366
Darryl Greenb33cc762019-11-28 14:29:44 +00004367 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004369 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 }
4371
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004372 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 {
sander-visserb8aa2072020-05-06 22:05:13 +02004374#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4375 size_t in_buf_len = ssl->in_buf_len;
4376#else
4377 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4378#endif
4379
Darryl Greenb33cc762019-11-28 14:29:44 +00004380 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004382 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 }
4384
Paul Bakker48916f92012-09-16 19:57:18 +00004385 if( ssl->transform )
4386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 mbedtls_ssl_transform_free( ssl->transform );
4388 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004389 }
4390
4391 if( ssl->handshake )
4392 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004393 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4395 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 mbedtls_free( ssl->handshake );
4398 mbedtls_free( ssl->transform_negotiate );
4399 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004400 }
4401
Ronald Cron6f135e12021-12-08 16:57:54 +01004402#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004403 mbedtls_ssl_transform_free( ssl->transform_application );
4404 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004405#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004406
Paul Bakkerc0463502013-02-14 11:19:38 +01004407 if( ssl->session )
4408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 mbedtls_ssl_session_free( ssl->session );
4410 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004411 }
4412
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004413#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004414 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004416 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004419#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004420
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004421#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004423#endif
4424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004426
Paul Bakker86f04f42013-02-14 11:20:09 +01004427 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004428 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429}
4430
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004431/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004432 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004433 */
4434void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4435{
4436 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4437}
4438
Gilles Peskineae270bf2021-06-02 00:05:29 +02004439/* The selection should be the same as mbedtls_x509_crt_profile_default in
4440 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004441 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004442 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443 * about this list.
4444 */
Brett Warrene0edc842021-08-17 09:53:13 +01004445static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004446#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004448#endif
4449#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004450 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004451#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004452#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004453 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004454#endif
4455#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004456 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004457#endif
4458#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004459 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004460#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004461#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004462 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004463#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004464#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004465 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004466#endif
4467#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004468 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004469#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004470 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004471};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004472
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004473static int ssl_preset_suiteb_ciphersuites[] = {
4474 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4475 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4476 0
4477};
4478
Gilles Peskineeccd8882020-03-10 12:19:08 +01004479#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004480
Jerry Yu909df7b2022-01-22 11:56:27 +08004481/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004482 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004483 * rules SHOULD be upheld.
4484 * - No duplicate entries.
4485 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004486 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004487 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004488 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004489static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004490
Andrzej Kurek25f27152022-08-17 16:09:31 -04004491#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 +08004492 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4493 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004494#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004495 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004496
Andrzej Kurek25f27152022-08-17 16:09:31 -04004497#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 +08004498 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4499 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004500#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004501 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4502
Andrzej Kurek25f27152022-08-17 16:09:31 -04004503#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 +08004504 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4505 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004506#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004507 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004508
Andrzej Kurek25f27152022-08-17 16:09:31 -04004509#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 +08004510 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004511#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 +08004512
Andrzej Kurek25f27152022-08-17 16:09:31 -04004513#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 +08004514 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004515#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 +08004516
Andrzej Kurek25f27152022-08-17 16:09:31 -04004517#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 +08004518 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004519#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 +08004520
Andrzej Kurek25f27152022-08-17 16:09:31 -04004521#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 +08004522 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4523#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4524
Andrzej Kurek25f27152022-08-17 16:09:31 -04004525#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 +08004526 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4527#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4528
Andrzej Kurek25f27152022-08-17 16:09:31 -04004529#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 +08004530 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4531#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4532
Gabor Mezei15b95a62022-05-09 16:37:58 +02004533 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004534};
4535
4536/* NOTICE: see above */
4537#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4538static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004539#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004540#if defined(MBEDTLS_ECDSA_C)
4541 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004542#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004543#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4544 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4545#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004546#if defined(MBEDTLS_RSA_C)
4547 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4548#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004549#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004550#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004551#if defined(MBEDTLS_ECDSA_C)
4552 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004553#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004554#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4555 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4556#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004557#if defined(MBEDTLS_RSA_C)
4558 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4559#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004560#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004561#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004562#if defined(MBEDTLS_ECDSA_C)
4563 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004564#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004565#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4566 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4567#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004568#if defined(MBEDTLS_RSA_C)
4569 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4570#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004571#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004572 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004573};
4574#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4575/* NOTICE: see above */
4576static uint16_t ssl_preset_suiteb_sig_algs[] = {
4577
Andrzej Kurek25f27152022-08-17 16:09:31 -04004578#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 +08004579 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4580 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004581#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004582 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4583
Andrzej Kurek25f27152022-08-17 16:09:31 -04004584#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 +08004585 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4586 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004587#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004588 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004589
Andrzej Kurek25f27152022-08-17 16:09:31 -04004590#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 +08004591 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004592#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 +08004593
Andrzej Kurek25f27152022-08-17 16:09:31 -04004594#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 +08004595 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004596#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004597
Gabor Mezei15b95a62022-05-09 16:37:58 +02004598 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004599};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004600
Jerry Yu909df7b2022-01-22 11:56:27 +08004601/* NOTICE: see above */
4602#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4603static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004604#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004605#if defined(MBEDTLS_ECDSA_C)
4606 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004607#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004608#if defined(MBEDTLS_RSA_C)
4609 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4610#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004611#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004612#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004613#if defined(MBEDTLS_ECDSA_C)
4614 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004615#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004616#if defined(MBEDTLS_RSA_C)
4617 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4618#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004619#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004620 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004621};
Jerry Yu909df7b2022-01-22 11:56:27 +08004622#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4623
Jerry Yu1a8b4812022-01-20 17:56:50 +08004624#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004625
Brett Warrene0edc842021-08-17 09:53:13 +01004626static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004627#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004628 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004629#endif
4630#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004631 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004632#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004633 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004634};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004635
Jerry Yu1a8b4812022-01-20 17:56:50 +08004636#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004637/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004638 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004639MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004640static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004641{
4642 size_t i, j;
4643 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004644
Gabor Mezei15b95a62022-05-09 16:37:58 +02004645 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004646 {
Jerry Yuf377d642022-01-25 10:43:59 +08004647 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004648 {
Jerry Yuf377d642022-01-25 10:43:59 +08004649 if( sig_algs[i] != sig_algs[j] )
4650 continue;
4651 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4652 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4653 sig_algs[i], j, i );
4654 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004655 }
4656 }
4657 return( ret );
4658}
4659
4660#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4661
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004662/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004663 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004664 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004665int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004666 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004667{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004668#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004670#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004671
Jerry Yu1a8b4812022-01-20 17:56:50 +08004672#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004673 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004674 {
4675 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4676 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4677 }
4678
Jerry Yuf377d642022-01-25 10:43:59 +08004679 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004680 {
4681 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4682 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4683 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004684
4685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004686 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004687 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004688 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004689 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4690 }
4691
Jerry Yuf377d642022-01-25 10:43:59 +08004692 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004693 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004694 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004695 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4696 }
4697#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004698#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4699
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004700 /* Use the functions here so that they are covered in tests,
4701 * but otherwise access member directly for efficiency */
4702 mbedtls_ssl_conf_endpoint( conf, endpoint );
4703 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004704
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004705 /*
4706 * Things that are common to all presets
4707 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004708#if defined(MBEDTLS_SSL_CLI_C)
4709 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4710 {
4711 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4712#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4713 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4714#endif
4715 }
4716#endif
4717
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4719 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4720#endif
4721
4722#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4723 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4724#endif
4725
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004726#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004727 conf->f_cookie_write = ssl_cookie_write_dummy;
4728 conf->f_cookie_check = ssl_cookie_check_dummy;
4729#endif
4730
4731#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4732 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4733#endif
4734
Janos Follath088ce432017-04-10 12:42:31 +01004735#if defined(MBEDTLS_SSL_SRV_C)
4736 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004737 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004738#endif
4739
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004740#if defined(MBEDTLS_SSL_PROTO_DTLS)
4741 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4742 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4743#endif
4744
4745#if defined(MBEDTLS_SSL_RENEGOTIATION)
4746 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004747 memset( conf->renego_period, 0x00, 2 );
4748 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004749#endif
4750
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004751#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004752 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4753 {
4754 const unsigned char dhm_p[] =
4755 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4756 const unsigned char dhm_g[] =
4757 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004758
Hanno Beckere2defad2021-07-24 05:59:17 +01004759 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4760 dhm_p, sizeof( dhm_p ),
4761 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4762 {
4763 return( ret );
4764 }
4765 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004766#endif
4767
Ronald Cron6f135e12021-12-08 16:57:54 +01004768#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004769#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004770 mbedtls_ssl_conf_new_session_tickets(
4771 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4772#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004773 /*
4774 * Allow all TLS 1.3 key exchange modes by default.
4775 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004776 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004777#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004778
XiaokangQian4d3a6042022-04-21 13:46:17 +00004779 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4780 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004782 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004783 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004784#else
XiaokangQian060d8672022-04-21 09:24:56 +00004785 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004786#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004787 }
4788 else
4789 {
4790#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4791 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4792 {
4793 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4794 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4795 }
4796 else
4797 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4798 {
4799 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4800 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4801 }
4802#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4803 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4804 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4805#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4806 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4807 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4808#else
4809 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4810#endif
4811 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004812
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004813 /*
4814 * Preset-specific defaults
4815 */
4816 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004817 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004818 /*
4819 * NSA Suite B
4820 */
4821 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004822
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004823 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004824
4825#if defined(MBEDTLS_X509_CRT_PARSE_C)
4826 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004827#endif
4828
Gilles Peskineeccd8882020-03-10 12:19:08 +01004829#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004830#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4831 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4832 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4833 else
4834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4835 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004836#endif
4837
Brett Warrene0edc842021-08-17 09:53:13 +01004838#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4839 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004840#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004841 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004842 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004843
4844 /*
4845 * Default
4846 */
4847 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004848
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004849 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004850
4851#if defined(MBEDTLS_X509_CRT_PARSE_C)
4852 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4853#endif
4854
Gilles Peskineeccd8882020-03-10 12:19:08 +01004855#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004856#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4857 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4858 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4859 else
4860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4861 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004862#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004863
Brett Warrene0edc842021-08-17 09:53:13 +01004864#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4865 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004866#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004867 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004868
4869#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4870 conf->dhm_min_bitlen = 1024;
4871#endif
4872 }
4873
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004874 return( 0 );
4875}
4876
4877/*
4878 * Free mbedtls_ssl_config
4879 */
4880void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4881{
4882#if defined(MBEDTLS_DHM_C)
4883 mbedtls_mpi_free( &conf->dhm_P );
4884 mbedtls_mpi_free( &conf->dhm_G );
4885#endif
4886
Gilles Peskineeccd8882020-03-10 12:19:08 +01004887#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004888#if defined(MBEDTLS_USE_PSA_CRYPTO)
4889 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4890 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004891 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4892 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004893#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004894 if( conf->psk != NULL )
4895 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004896 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004897 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004898 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004899 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004900 }
4901
4902 if( conf->psk_identity != NULL )
4903 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004904 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004905 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004906 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004907 conf->psk_identity_len = 0;
4908 }
4909#endif
4910
4911#if defined(MBEDTLS_X509_CRT_PARSE_C)
4912 ssl_key_cert_free( conf->key_cert );
4913#endif
4914
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004915 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004916}
4917
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004918#if defined(MBEDTLS_PK_C) && \
4919 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004920/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004921 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925#if defined(MBEDTLS_RSA_C)
4926 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4927 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004928#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_ECDSA_C)
4930 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4931 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004934}
4935
Hanno Becker7e5437a2017-04-28 17:15:26 +01004936unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4937{
4938 switch( type ) {
4939 case MBEDTLS_PK_RSA:
4940 return( MBEDTLS_SSL_SIG_RSA );
4941 case MBEDTLS_PK_ECDSA:
4942 case MBEDTLS_PK_ECKEY:
4943 return( MBEDTLS_SSL_SIG_ECDSA );
4944 default:
4945 return( MBEDTLS_SSL_SIG_ANON );
4946 }
4947}
4948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004950{
4951 switch( sig )
4952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953#if defined(MBEDTLS_RSA_C)
4954 case MBEDTLS_SSL_SIG_RSA:
4955 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ECDSA_C)
4958 case MBEDTLS_SSL_SIG_ECDSA:
4959 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960#endif
4961 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004963 }
4964}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004965#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004966
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004967/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004968 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971{
4972 switch( hash )
4973 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004974#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975 case MBEDTLS_SSL_HASH_MD5:
4976 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004977#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004978#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 case MBEDTLS_SSL_HASH_SHA1:
4980 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004981#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004982#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 case MBEDTLS_SSL_HASH_SHA224:
4984 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004985#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004986#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 case MBEDTLS_SSL_HASH_SHA256:
4988 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004989#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004990#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991 case MBEDTLS_SSL_HASH_SHA384:
4992 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004993#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 case MBEDTLS_SSL_HASH_SHA512:
4996 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004997#endif
4998 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005000 }
5001}
5002
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005003/*
5004 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5005 */
5006unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5007{
5008 switch( md )
5009 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005011 case MBEDTLS_MD_MD5:
5012 return( MBEDTLS_SSL_HASH_MD5 );
5013#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005015 case MBEDTLS_MD_SHA1:
5016 return( MBEDTLS_SSL_HASH_SHA1 );
5017#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005019 case MBEDTLS_MD_SHA224:
5020 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005021#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005022#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005023 case MBEDTLS_MD_SHA256:
5024 return( MBEDTLS_SSL_HASH_SHA256 );
5025#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005026#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005027 case MBEDTLS_MD_SHA384:
5028 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005029#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005030#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005031 case MBEDTLS_MD_SHA512:
5032 return( MBEDTLS_SSL_HASH_SHA512 );
5033#endif
5034 default:
5035 return( MBEDTLS_SSL_HASH_NONE );
5036 }
5037}
5038
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005039/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005040 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005041 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005042 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005043int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005044{
Brett Warrene0edc842021-08-17 09:53:13 +01005045 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005046
Brett Warrene0edc842021-08-17 09:53:13 +01005047 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005048 return( -1 );
5049
Brett Warrene0edc842021-08-17 09:53:13 +01005050 for( ; *group_list != 0; group_list++ )
5051 {
5052 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005053 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005054 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005055
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005056 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005057}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005058
5059#if defined(MBEDTLS_ECP_C)
5060/*
5061 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5062 */
5063int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5064{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005065 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005066 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005067
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005068 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005069 return -1;
5070
5071 uint16_t tls_id = grp_info->tls_id;
5072
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005073 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5074}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005075#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077#if defined(MBEDTLS_X509_CRT_PARSE_C)
5078int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5079 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005080 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005081 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005082{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005083 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005084 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005085 const char *ext_oid;
5086 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005089 {
5090 /* Server part of the key exchange */
5091 switch( ciphersuite->key_exchange )
5092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 case MBEDTLS_KEY_EXCHANGE_RSA:
5094 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005095 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005096 break;
5097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5099 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5100 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5101 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005102 break;
5103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5105 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005106 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005107 break;
5108
5109 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005110 case MBEDTLS_KEY_EXCHANGE_NONE:
5111 case MBEDTLS_KEY_EXCHANGE_PSK:
5112 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5113 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005114 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005115 usage = 0;
5116 }
5117 }
5118 else
5119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5121 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005122 }
5123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005125 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005126 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005127 ret = -1;
5128 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5133 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005134 }
5135 else
5136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5138 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005139 }
5140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005142 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005143 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005144 ret = -1;
5145 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005146
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005147 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005150
Jerry Yu148165c2021-09-24 23:20:59 +08005151#if defined(MBEDTLS_USE_PSA_CRYPTO)
5152int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5153 const mbedtls_md_type_t md,
5154 unsigned char *dst,
5155 size_t dst_len,
5156 size_t *olen )
5157{
Ronald Cronf6893e12022-01-07 22:09:01 +01005158 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5159 psa_hash_operation_t *hash_operation_to_clone;
5160 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5161
Jerry Yu148165c2021-09-24 23:20:59 +08005162 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005163
5164 switch( md )
5165 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005166#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005167 case MBEDTLS_MD_SHA384:
5168 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5169 break;
5170#endif
5171
Andrzej Kurek25f27152022-08-17 16:09:31 -04005172#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005173 case MBEDTLS_MD_SHA256:
5174 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5175 break;
5176#endif
5177
5178 default:
5179 goto exit;
5180 }
5181
5182 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5183 if( status != PSA_SUCCESS )
5184 goto exit;
5185
5186 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5187 if( status != PSA_SUCCESS )
5188 goto exit;
5189
5190exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005191 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005192}
5193#else /* MBEDTLS_USE_PSA_CRYPTO */
5194
Andrzej Kurek25f27152022-08-17 16:09:31 -04005195#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005196MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005197static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5198 unsigned char *dst,
5199 size_t dst_len,
5200 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005201{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005202 int ret;
5203 mbedtls_sha512_context sha512;
5204
5205 if( dst_len < 48 )
5206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5207
5208 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005209 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005210
5211 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5212 {
5213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5214 goto exit;
5215 }
5216
5217 *olen = 48;
5218
5219exit:
5220
5221 mbedtls_sha512_free( &sha512 );
5222 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005223}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005224#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005225
Andrzej Kurek25f27152022-08-17 16:09:31 -04005226#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005227MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005228static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5229 unsigned char *dst,
5230 size_t dst_len,
5231 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005232{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005233 int ret;
5234 mbedtls_sha256_context sha256;
5235
5236 if( dst_len < 32 )
5237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5238
5239 mbedtls_sha256_init( &sha256 );
5240 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005241
Jerry Yu24c0ec32021-09-09 14:21:07 +08005242 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5243 {
5244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5245 goto exit;
5246 }
5247
5248 *olen = 32;
5249
5250exit:
5251
5252 mbedtls_sha256_free( &sha256 );
5253 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005254}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005255#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005256
Jerry Yu000f9762021-09-14 11:12:51 +08005257int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5258 const mbedtls_md_type_t md,
5259 unsigned char *dst,
5260 size_t dst_len,
5261 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005262{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005263 switch( md )
5264 {
5265
Andrzej Kurek25f27152022-08-17 16:09:31 -04005266#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005267 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005268 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005269#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005270
Andrzej Kurek25f27152022-08-17 16:09:31 -04005271#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005272 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005273 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005274#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005275
5276 default:
5277 break;
5278 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005280}
XiaokangQian647719a2021-12-07 09:16:29 +00005281
Jerry Yu148165c2021-09-24 23:20:59 +08005282#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005283
Gabor Mezei078e8032022-04-27 21:17:56 +02005284#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5285/* mbedtls_ssl_parse_sig_alg_ext()
5286 *
5287 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5288 * value (TLS 1.3 RFC8446):
5289 * enum {
5290 * ....
5291 * ecdsa_secp256r1_sha256( 0x0403 ),
5292 * ecdsa_secp384r1_sha384( 0x0503 ),
5293 * ecdsa_secp521r1_sha512( 0x0603 ),
5294 * ....
5295 * } SignatureScheme;
5296 *
5297 * struct {
5298 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5299 * } SignatureSchemeList;
5300 *
5301 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5302 * value (TLS 1.2 RFC5246):
5303 * enum {
5304 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5305 * sha512(6), (255)
5306 * } HashAlgorithm;
5307 *
5308 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5309 * SignatureAlgorithm;
5310 *
5311 * struct {
5312 * HashAlgorithm hash;
5313 * SignatureAlgorithm signature;
5314 * } SignatureAndHashAlgorithm;
5315 *
5316 * SignatureAndHashAlgorithm
5317 * supported_signature_algorithms<2..2^16-2>;
5318 *
5319 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5320 * generalization of the TLS 1.2 signature algorithm extension.
5321 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5322 * `SignatureScheme` field of TLS 1.3
5323 *
5324 */
5325int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5326 const unsigned char *buf,
5327 const unsigned char *end )
5328{
5329 const unsigned char *p = buf;
5330 size_t supported_sig_algs_len = 0;
5331 const unsigned char *supported_sig_algs_end;
5332 uint16_t sig_alg;
5333 uint32_t common_idx = 0;
5334
5335 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5336 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5337 p += 2;
5338
5339 memset( ssl->handshake->received_sig_algs, 0,
5340 sizeof(ssl->handshake->received_sig_algs) );
5341
5342 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5343 supported_sig_algs_end = p + supported_sig_algs_len;
5344 while( p < supported_sig_algs_end )
5345 {
5346 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5347 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5348 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005349 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5350 sig_alg,
5351 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005352#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005353 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005354 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5355 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5356 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005357 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005358 }
5359#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005360
Jerry Yuf3b46b52022-06-19 16:52:27 +08005361 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5362 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005363
5364 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5365 {
5366 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5367 common_idx += 1;
5368 }
5369 }
5370 /* Check that we consumed all the message. */
5371 if( p != end )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 1,
5374 ( "Signature algorithms extension length misaligned" ) );
5375 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5376 MBEDTLS_ERR_SSL_DECODE_ERROR );
5377 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5378 }
5379
5380 if( common_idx == 0 )
5381 {
5382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5383 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5384 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5385 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5386 }
5387
Gabor Mezei15b95a62022-05-09 16:37:58 +02005388 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005389 return( 0 );
5390}
5391
5392#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5393
Jerry Yudc7bd172022-02-17 13:44:15 +08005394#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5395
5396#if defined(MBEDTLS_USE_PSA_CRYPTO)
5397
5398static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5399 mbedtls_svc_key_id_t key,
5400 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005401 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005402 const unsigned char* seed, size_t seed_length,
5403 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005404 const unsigned char* other_secret,
5405 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005406 size_t capacity )
5407{
5408 psa_status_t status;
5409
5410 status = psa_key_derivation_setup( derivation, alg );
5411 if( status != PSA_SUCCESS )
5412 return( status );
5413
5414 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5415 {
5416 status = psa_key_derivation_input_bytes( derivation,
5417 PSA_KEY_DERIVATION_INPUT_SEED,
5418 seed, seed_length );
5419 if( status != PSA_SUCCESS )
5420 return( status );
5421
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005422 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005423 {
5424 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005425 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5426 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005427 if( status != PSA_SUCCESS )
5428 return( status );
5429 }
5430
Jerry Yudc7bd172022-02-17 13:44:15 +08005431 if( mbedtls_svc_key_id_is_null( key ) )
5432 {
5433 status = psa_key_derivation_input_bytes(
5434 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005435 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005436 }
5437 else
5438 {
5439 status = psa_key_derivation_input_key(
5440 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5441 }
5442 if( status != PSA_SUCCESS )
5443 return( status );
5444
5445 status = psa_key_derivation_input_bytes( derivation,
5446 PSA_KEY_DERIVATION_INPUT_LABEL,
5447 label, label_length );
5448 if( status != PSA_SUCCESS )
5449 return( status );
5450 }
5451 else
5452 {
5453 return( PSA_ERROR_NOT_SUPPORTED );
5454 }
5455
5456 status = psa_key_derivation_set_capacity( derivation, capacity );
5457 if( status != PSA_SUCCESS )
5458 return( status );
5459
5460 return( PSA_SUCCESS );
5461}
5462
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005463MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005464static int tls_prf_generic( mbedtls_md_type_t md_type,
5465 const unsigned char *secret, size_t slen,
5466 const char *label,
5467 const unsigned char *random, size_t rlen,
5468 unsigned char *dstbuf, size_t dlen )
5469{
5470 psa_status_t status;
5471 psa_algorithm_t alg;
5472 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5473 psa_key_derivation_operation_t derivation =
5474 PSA_KEY_DERIVATION_OPERATION_INIT;
5475
5476 if( md_type == MBEDTLS_MD_SHA384 )
5477 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5478 else
5479 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5480
5481 /* Normally a "secret" should be long enough to be impossible to
5482 * find by brute force, and in particular should not be empty. But
5483 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5484 * and for this use case it makes sense to have a 0-length "secret".
5485 * Since the key API doesn't allow importing a key of length 0,
5486 * keep master_key=0, which setup_psa_key_derivation() understands
5487 * to mean a 0-length "secret" input. */
5488 if( slen != 0 )
5489 {
5490 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5491 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5492 psa_set_key_algorithm( &key_attributes, alg );
5493 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5494
5495 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5496 if( status != PSA_SUCCESS )
5497 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5498 }
5499
5500 status = setup_psa_key_derivation( &derivation,
5501 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005502 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005503 random, rlen,
5504 (unsigned char const *) label,
5505 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005506 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005507 dlen );
5508 if( status != PSA_SUCCESS )
5509 {
5510 psa_key_derivation_abort( &derivation );
5511 psa_destroy_key( master_key );
5512 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5513 }
5514
5515 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5516 if( status != PSA_SUCCESS )
5517 {
5518 psa_key_derivation_abort( &derivation );
5519 psa_destroy_key( master_key );
5520 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5521 }
5522
5523 status = psa_key_derivation_abort( &derivation );
5524 if( status != PSA_SUCCESS )
5525 {
5526 psa_destroy_key( master_key );
5527 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5528 }
5529
5530 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5531 status = psa_destroy_key( master_key );
5532 if( status != PSA_SUCCESS )
5533 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5534
5535 return( 0 );
5536}
5537
5538#else /* MBEDTLS_USE_PSA_CRYPTO */
5539
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005540MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005541static int tls_prf_generic( mbedtls_md_type_t md_type,
5542 const unsigned char *secret, size_t slen,
5543 const char *label,
5544 const unsigned char *random, size_t rlen,
5545 unsigned char *dstbuf, size_t dlen )
5546{
5547 size_t nb;
5548 size_t i, j, k, md_len;
5549 unsigned char *tmp;
5550 size_t tmp_len = 0;
5551 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5552 const mbedtls_md_info_t *md_info;
5553 mbedtls_md_context_t md_ctx;
5554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5555
5556 mbedtls_md_init( &md_ctx );
5557
5558 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5560
5561 md_len = mbedtls_md_get_size( md_info );
5562
5563 tmp_len = md_len + strlen( label ) + rlen;
5564 tmp = mbedtls_calloc( 1, tmp_len );
5565 if( tmp == NULL )
5566 {
5567 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5568 goto exit;
5569 }
5570
5571 nb = strlen( label );
5572 memcpy( tmp + md_len, label, nb );
5573 memcpy( tmp + md_len + nb, random, rlen );
5574 nb += rlen;
5575
5576 /*
5577 * Compute P_<hash>(secret, label + random)[0..dlen]
5578 */
5579 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5580 goto exit;
5581
5582 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5583 if( ret != 0 )
5584 goto exit;
5585 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5586 if( ret != 0 )
5587 goto exit;
5588 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5589 if( ret != 0 )
5590 goto exit;
5591
5592 for( i = 0; i < dlen; i += md_len )
5593 {
5594 ret = mbedtls_md_hmac_reset ( &md_ctx );
5595 if( ret != 0 )
5596 goto exit;
5597 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5598 if( ret != 0 )
5599 goto exit;
5600 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5601 if( ret != 0 )
5602 goto exit;
5603
5604 ret = mbedtls_md_hmac_reset ( &md_ctx );
5605 if( ret != 0 )
5606 goto exit;
5607 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5608 if( ret != 0 )
5609 goto exit;
5610 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5611 if( ret != 0 )
5612 goto exit;
5613
5614 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5615
5616 for( j = 0; j < k; j++ )
5617 dstbuf[i + j] = h_i[j];
5618 }
5619
5620exit:
5621 mbedtls_md_free( &md_ctx );
5622
5623 mbedtls_platform_zeroize( tmp, tmp_len );
5624 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5625
5626 mbedtls_free( tmp );
5627
5628 return( ret );
5629}
5630#endif /* MBEDTLS_USE_PSA_CRYPTO */
5631
Andrzej Kurek25f27152022-08-17 16:09:31 -04005632#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005633MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005634static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5635 const char *label,
5636 const unsigned char *random, size_t rlen,
5637 unsigned char *dstbuf, size_t dlen )
5638{
5639 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5640 label, random, rlen, dstbuf, dlen ) );
5641}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005642#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005643
Andrzej Kurek25f27152022-08-17 16:09:31 -04005644#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005645MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005646static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5647 const char *label,
5648 const unsigned char *random, size_t rlen,
5649 unsigned char *dstbuf, size_t dlen )
5650{
5651 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5652 label, random, rlen, dstbuf, dlen ) );
5653}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005654#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005655
Jerry Yuf009d862022-02-17 14:01:37 +08005656/*
5657 * Set appropriate PRF function and other SSL / TLS1.2 functions
5658 *
5659 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005660 * - hash associated with the ciphersuite (only used by TLS 1.2)
5661 *
5662 * Outputs:
5663 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5664 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005665MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005666static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005667 mbedtls_md_type_t hash )
5668{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005669#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005670 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005671 {
5672 handshake->tls_prf = tls_prf_sha384;
5673 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5674 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5675 }
5676 else
5677#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005678#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005679 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005680 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005681 handshake->tls_prf = tls_prf_sha256;
5682 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5683 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5684 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005685#else
Jerry Yuf009d862022-02-17 14:01:37 +08005686 {
Jerry Yuf009d862022-02-17 14:01:37 +08005687 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005688 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5690 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005691#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005692
5693 return( 0 );
5694}
Jerry Yud6ab2352022-02-17 14:03:43 +08005695
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005696/*
5697 * Compute master secret if needed
5698 *
5699 * Parameters:
5700 * [in/out] handshake
5701 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5702 * (PSA-PSK) ciphersuite_info, psk_opaque
5703 * [out] premaster (cleared)
5704 * [out] master
5705 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5706 * debug: conf->f_dbg, conf->p_dbg
5707 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005708 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005709 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005710MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005711static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5712 unsigned char *master,
5713 const mbedtls_ssl_context *ssl )
5714{
5715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5716
5717 /* cf. RFC 5246, Section 8.1:
5718 * "The master secret is always exactly 48 bytes in length." */
5719 size_t const master_secret_len = 48;
5720
5721#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5722 unsigned char session_hash[48];
5723#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5724
5725 /* The label for the KDF used for key expansion.
5726 * This is either "master secret" or "extended master secret"
5727 * depending on whether the Extended Master Secret extension
5728 * is used. */
5729 char const *lbl = "master secret";
5730
Przemek Stekielae4ed302022-04-05 17:15:55 +02005731 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005732 * - If the Extended Master Secret extension is not used,
5733 * this is ClientHello.Random + ServerHello.Random
5734 * (see Sect. 8.1 in RFC 5246).
5735 * - If the Extended Master Secret extension is used,
5736 * this is the transcript of the handshake so far.
5737 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005738 unsigned char const *seed = handshake->randbytes;
5739 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005740
5741#if !defined(MBEDTLS_DEBUG_C) && \
5742 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5743 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5744 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5745 ssl = NULL; /* make sure we don't use it except for those cases */
5746 (void) ssl;
5747#endif
5748
5749 if( handshake->resume != 0 )
5750 {
5751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5752 return( 0 );
5753 }
5754
5755#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5756 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5757 {
5758 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005759 seed = session_hash;
5760 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005761
5762 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005763 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005764 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005765#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005766
Przemek Stekiel99114f32022-04-22 11:20:09 +02005767#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005768 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005769 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005770 {
5771 /* Perform PSK-to-MS expansion in a single step. */
5772 psa_status_t status;
5773 psa_algorithm_t alg;
5774 mbedtls_svc_key_id_t psk;
5775 psa_key_derivation_operation_t derivation =
5776 PSA_KEY_DERIVATION_OPERATION_INIT;
5777 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5778
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5780
5781 psk = mbedtls_ssl_get_opaque_psk( ssl );
5782
5783 if( hash_alg == MBEDTLS_MD_SHA384 )
5784 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5785 else
5786 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5787
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005788 size_t other_secret_len = 0;
5789 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005790
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005791 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005792 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005793 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005794 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005795 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005796 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005797 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005798 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005799 other_secret_len = 48;
5800 other_secret = handshake->premaster + 2;
5801 break;
5802 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005803 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005804 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5805 other_secret = handshake->premaster + 2;
5806 break;
5807 default:
5808 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005809 }
5810
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005811 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005812 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005813 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005814 (unsigned char const *) lbl,
5815 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005816 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005817 master_secret_len );
5818 if( status != PSA_SUCCESS )
5819 {
5820 psa_key_derivation_abort( &derivation );
5821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5822 }
5823
5824 status = psa_key_derivation_output_bytes( &derivation,
5825 master,
5826 master_secret_len );
5827 if( status != PSA_SUCCESS )
5828 {
5829 psa_key_derivation_abort( &derivation );
5830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5831 }
5832
5833 status = psa_key_derivation_abort( &derivation );
5834 if( status != PSA_SUCCESS )
5835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5836 }
5837 else
5838#endif
5839 {
5840 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005841 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005842 master,
5843 master_secret_len );
5844 if( ret != 0 )
5845 {
5846 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5847 return( ret );
5848 }
5849
5850 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5851 handshake->premaster,
5852 handshake->pmslen );
5853
5854 mbedtls_platform_zeroize( handshake->premaster,
5855 sizeof(handshake->premaster) );
5856 }
5857
5858 return( 0 );
5859}
5860
Jerry Yud62f87e2022-02-17 14:09:02 +08005861int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5862{
5863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5864 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5865 ssl->handshake->ciphersuite_info;
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5868
5869 /* Set PRF, calc_verify and calc_finished function pointers */
5870 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005871 ciphersuite_info->mac );
5872 if( ret != 0 )
5873 {
5874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5875 return( ret );
5876 }
5877
5878 /* Compute master secret if needed */
5879 ret = ssl_compute_master( ssl->handshake,
5880 ssl->session_negotiate->master,
5881 ssl );
5882 if( ret != 0 )
5883 {
5884 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5885 return( ret );
5886 }
5887
5888 /* Swap the client and server random values:
5889 * - MS derivation wanted client+server (RFC 5246 8.1)
5890 * - key derivation wants server+client (RFC 5246 6.3) */
5891 {
5892 unsigned char tmp[64];
5893 memcpy( tmp, ssl->handshake->randbytes, 64 );
5894 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5895 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5896 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5897 }
5898
5899 /* Populate transform structure */
5900 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5901 ssl->session_negotiate->ciphersuite,
5902 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005903#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005904 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005905#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005906 ssl->handshake->tls_prf,
5907 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005908 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005909 ssl->conf->endpoint,
5910 ssl );
5911 if( ret != 0 )
5912 {
5913 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5914 return( ret );
5915 }
5916
5917 /* We no longer need Server/ClientHello.random values */
5918 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5919 sizeof( ssl->handshake->randbytes ) );
5920
5921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5922
5923 return( 0 );
5924}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005925
Ronald Cron4dcbca92022-03-07 10:21:40 +01005926int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5927{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005928 switch( md )
5929 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005930#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005931 case MBEDTLS_SSL_HASH_SHA384:
5932 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5933 break;
5934#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005935#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005936 case MBEDTLS_SSL_HASH_SHA256:
5937 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5938 break;
5939#endif
5940 default:
5941 return( -1 );
5942 }
5943
Ronald Cronc2f13a02022-03-07 10:25:24 +01005944 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005945}
5946
Andrzej Kurek25f27152022-08-17 16:09:31 -04005947#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005948void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5949 unsigned char *hash,
5950 size_t *hlen )
5951{
5952#if defined(MBEDTLS_USE_PSA_CRYPTO)
5953 size_t hash_size;
5954 psa_status_t status;
5955 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5956
5957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5958 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5959 if( status != PSA_SUCCESS )
5960 {
5961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5962 return;
5963 }
5964
5965 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5966 if( status != PSA_SUCCESS )
5967 {
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5969 return;
5970 }
5971
5972 *hlen = 32;
5973 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5975#else
5976 mbedtls_sha256_context sha256;
5977
5978 mbedtls_sha256_init( &sha256 );
5979
5980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5981
5982 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5983 mbedtls_sha256_finish( &sha256, hash );
5984
5985 *hlen = 32;
5986
5987 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5989
5990 mbedtls_sha256_free( &sha256 );
5991#endif /* MBEDTLS_USE_PSA_CRYPTO */
5992 return;
5993}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005994#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005995
Andrzej Kurek25f27152022-08-17 16:09:31 -04005996#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005997void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5998 unsigned char *hash,
5999 size_t *hlen )
6000{
6001#if defined(MBEDTLS_USE_PSA_CRYPTO)
6002 size_t hash_size;
6003 psa_status_t status;
6004 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6005
6006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6007 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6008 if( status != PSA_SUCCESS )
6009 {
6010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6011 return;
6012 }
6013
6014 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6015 if( status != PSA_SUCCESS )
6016 {
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6018 return;
6019 }
6020
6021 *hlen = 48;
6022 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6024#else
6025 mbedtls_sha512_context sha512;
6026
6027 mbedtls_sha512_init( &sha512 );
6028
6029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6030
Andrzej Kureka242e832022-08-11 10:03:14 -04006031 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006032 mbedtls_sha512_finish( &sha512, hash );
6033
6034 *hlen = 48;
6035
6036 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6038
6039 mbedtls_sha512_free( &sha512 );
6040#endif /* MBEDTLS_USE_PSA_CRYPTO */
6041 return;
6042}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006043#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006044
Neil Armstrong80f6f322022-05-03 17:56:38 +02006045#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6046 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006047int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6048{
6049 unsigned char *p = ssl->handshake->premaster;
6050 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6051 const unsigned char *psk = NULL;
6052 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006053 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006054
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006055 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006056 {
6057 /*
6058 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006059 * checked before calling this function.
6060 *
6061 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006062 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006063 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006064 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6065 {
6066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6068 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006069 }
6070
6071 /*
6072 * PMS = struct {
6073 * opaque other_secret<0..2^16-1>;
6074 * opaque psk<0..2^16-1>;
6075 * };
6076 * with "other_secret" depending on the particular key exchange
6077 */
6078#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6079 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6080 {
6081 if( end - p < 2 )
6082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6083
6084 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6085 p += 2;
6086
6087 if( end < p || (size_t)( end - p ) < psk_len )
6088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6089
6090 memset( p, 0, psk_len );
6091 p += psk_len;
6092 }
6093 else
6094#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6095#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6096 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6097 {
6098 /*
6099 * other_secret already set by the ClientKeyExchange message,
6100 * and is 48 bytes long
6101 */
6102 if( end - p < 2 )
6103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6104
6105 *p++ = 0;
6106 *p++ = 48;
6107 p += 48;
6108 }
6109 else
6110#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6111#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6112 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6113 {
6114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6115 size_t len;
6116
6117 /* Write length only when we know the actual value */
6118 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6119 p + 2, end - ( p + 2 ), &len,
6120 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6121 {
6122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6123 return( ret );
6124 }
6125 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6126 p += 2 + len;
6127
6128 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6129 }
6130 else
6131#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006132#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006133 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6134 {
6135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6136 size_t zlen;
6137
6138 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6139 p + 2, end - ( p + 2 ),
6140 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6141 {
6142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6143 return( ret );
6144 }
6145
6146 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6147 p += 2 + zlen;
6148
6149 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6150 MBEDTLS_DEBUG_ECDH_Z );
6151 }
6152 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006153#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6157 }
6158
6159 /* opaque psk<0..2^16-1>; */
6160 if( end - p < 2 )
6161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6162
6163 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6164 p += 2;
6165
6166 if( end < p || (size_t)( end - p ) < psk_len )
6167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6168
6169 memcpy( p, psk, psk_len );
6170 p += psk_len;
6171
6172 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6173
6174 return( 0 );
6175}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006176#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006177
6178#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006179MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006180static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6181
6182#if defined(MBEDTLS_SSL_PROTO_DTLS)
6183int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6184{
6185 /* If renegotiation is not enforced, retransmit until we would reach max
6186 * timeout if we were using the usual handshake doubling scheme */
6187 if( ssl->conf->renego_max_records < 0 )
6188 {
6189 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6190 unsigned char doublings = 1;
6191
6192 while( ratio != 0 )
6193 {
6194 ++doublings;
6195 ratio >>= 1;
6196 }
6197
6198 if( ++ssl->renego_records_seen > doublings )
6199 {
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6201 return( 0 );
6202 }
6203 }
6204
6205 return( ssl_write_hello_request( ssl ) );
6206}
6207#endif
6208#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006209
Jerry Yud9526692022-02-17 14:23:47 +08006210/*
6211 * Handshake functions
6212 */
6213#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6214/* No certificate support -> dummy functions */
6215int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6216{
6217 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6218 ssl->handshake->ciphersuite_info;
6219
6220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6221
6222 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6223 {
6224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6225 ssl->state++;
6226 return( 0 );
6227 }
6228
6229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6231}
6232
6233int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6234{
6235 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6236 ssl->handshake->ciphersuite_info;
6237
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6239
6240 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6241 {
6242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6243 ssl->state++;
6244 return( 0 );
6245 }
6246
6247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6249}
6250
6251#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6252/* Some certificate support -> implement write and parse */
6253
6254int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6255{
6256 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6257 size_t i, n;
6258 const mbedtls_x509_crt *crt;
6259 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6260 ssl->handshake->ciphersuite_info;
6261
6262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6263
6264 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6265 {
6266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6267 ssl->state++;
6268 return( 0 );
6269 }
6270
6271#if defined(MBEDTLS_SSL_CLI_C)
6272 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6273 {
6274 if( ssl->handshake->client_auth == 0 )
6275 {
6276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6277 ssl->state++;
6278 return( 0 );
6279 }
6280 }
6281#endif /* MBEDTLS_SSL_CLI_C */
6282#if defined(MBEDTLS_SSL_SRV_C)
6283 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6284 {
6285 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6286 {
6287 /* Should never happen because we shouldn't have picked the
6288 * ciphersuite if we don't have a certificate. */
6289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6290 }
6291 }
6292#endif
6293
6294 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6295
6296 /*
6297 * 0 . 0 handshake type
6298 * 1 . 3 handshake length
6299 * 4 . 6 length of all certs
6300 * 7 . 9 length of cert. 1
6301 * 10 . n-1 peer certificate
6302 * n . n+2 length of cert. 2
6303 * n+3 . ... upper level cert, etc.
6304 */
6305 i = 7;
6306 crt = mbedtls_ssl_own_cert( ssl );
6307
6308 while( crt != NULL )
6309 {
6310 n = crt->raw.len;
6311 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6312 {
6313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6314 " > %" MBEDTLS_PRINTF_SIZET,
6315 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6316 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6317 }
6318
6319 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6320 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6321 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6322
6323 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6324 i += n; crt = crt->next;
6325 }
6326
6327 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6328 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6329 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6330
6331 ssl->out_msglen = i;
6332 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6333 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6334
6335 ssl->state++;
6336
6337 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6338 {
6339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6340 return( ret );
6341 }
6342
6343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6344
6345 return( ret );
6346}
6347
6348#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6349
6350#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006351MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006352static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6353 unsigned char *crt_buf,
6354 size_t crt_buf_len )
6355{
6356 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6357
6358 if( peer_crt == NULL )
6359 return( -1 );
6360
6361 if( peer_crt->raw.len != crt_buf_len )
6362 return( -1 );
6363
6364 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6365}
6366#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006367MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006368static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6369 unsigned char *crt_buf,
6370 size_t crt_buf_len )
6371{
6372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6373 unsigned char const * const peer_cert_digest =
6374 ssl->session->peer_cert_digest;
6375 mbedtls_md_type_t const peer_cert_digest_type =
6376 ssl->session->peer_cert_digest_type;
6377 mbedtls_md_info_t const * const digest_info =
6378 mbedtls_md_info_from_type( peer_cert_digest_type );
6379 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6380 size_t digest_len;
6381
6382 if( peer_cert_digest == NULL || digest_info == NULL )
6383 return( -1 );
6384
6385 digest_len = mbedtls_md_get_size( digest_info );
6386 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6387 return( -1 );
6388
6389 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6390 if( ret != 0 )
6391 return( -1 );
6392
6393 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6394}
6395#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6396#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6397
6398/*
6399 * Once the certificate message is read, parse it into a cert chain and
6400 * perform basic checks, but leave actual verification to the caller
6401 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006402MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006403static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6404 mbedtls_x509_crt *chain )
6405{
6406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6407#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6408 int crt_cnt=0;
6409#endif
6410 size_t i, n;
6411 uint8_t alert;
6412
6413 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6414 {
6415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6416 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6417 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6418 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6419 }
6420
6421 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6422 {
6423 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6424 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6425 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6426 }
6427
6428 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6429 {
6430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6432 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6433 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6434 }
6435
6436 i = mbedtls_ssl_hs_hdr_len( ssl );
6437
6438 /*
6439 * Same message structure as in mbedtls_ssl_write_certificate()
6440 */
6441 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6442
6443 if( ssl->in_msg[i] != 0 ||
6444 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6445 {
6446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6447 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6448 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6449 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6450 }
6451
6452 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6453 i += 3;
6454
6455 /* Iterate through and parse the CRTs in the provided chain. */
6456 while( i < ssl->in_hslen )
6457 {
6458 /* Check that there's room for the next CRT's length fields. */
6459 if ( i + 3 > ssl->in_hslen ) {
6460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6461 mbedtls_ssl_send_alert_message( ssl,
6462 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6463 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6464 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6465 }
6466 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6467 * anything beyond 2**16 ~ 64K. */
6468 if( ssl->in_msg[i] != 0 )
6469 {
6470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6471 mbedtls_ssl_send_alert_message( ssl,
6472 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6473 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6474 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6475 }
6476
6477 /* Read length of the next CRT in the chain. */
6478 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6479 | (unsigned int) ssl->in_msg[i + 2];
6480 i += 3;
6481
6482 if( n < 128 || i + n > ssl->in_hslen )
6483 {
6484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6485 mbedtls_ssl_send_alert_message( ssl,
6486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6487 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6488 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6489 }
6490
6491 /* Check if we're handling the first CRT in the chain. */
6492#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6493 if( crt_cnt++ == 0 &&
6494 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6495 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6496 {
6497 /* During client-side renegotiation, check that the server's
6498 * end-CRTs hasn't changed compared to the initial handshake,
6499 * mitigating the triple handshake attack. On success, reuse
6500 * the original end-CRT instead of parsing it again. */
6501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6502 if( ssl_check_peer_crt_unchanged( ssl,
6503 &ssl->in_msg[i],
6504 n ) != 0 )
6505 {
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6507 mbedtls_ssl_send_alert_message( ssl,
6508 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6509 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6510 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6511 }
6512
6513 /* Now we can safely free the original chain. */
6514 ssl_clear_peer_cert( ssl->session );
6515 }
6516#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6517
6518 /* Parse the next certificate in the chain. */
6519#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6520 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6521#else
6522 /* If we don't need to store the CRT chain permanently, parse
6523 * it in-place from the input buffer instead of making a copy. */
6524 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6525#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6526 switch( ret )
6527 {
6528 case 0: /*ok*/
6529 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6530 /* Ignore certificate with an unknown algorithm: maybe a
6531 prior certificate was already trusted. */
6532 break;
6533
6534 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6535 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6536 goto crt_parse_der_failed;
6537
6538 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6539 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6540 goto crt_parse_der_failed;
6541
6542 default:
6543 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6544 crt_parse_der_failed:
6545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6546 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6547 return( ret );
6548 }
6549
6550 i += n;
6551 }
6552
6553 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6554 return( 0 );
6555}
6556
6557#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006558MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006559static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6560{
6561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6562 return( -1 );
6563
6564 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6565 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6566 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6567 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6568 {
Ronald Cron19385882022-06-15 16:26:13 +02006569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006570 return( 0 );
6571 }
6572 return( -1 );
6573}
6574#endif /* MBEDTLS_SSL_SRV_C */
6575
6576/* Check if a certificate message is expected.
6577 * Return either
6578 * - SSL_CERTIFICATE_EXPECTED, or
6579 * - SSL_CERTIFICATE_SKIP
6580 * indicating whether a Certificate message is expected or not.
6581 */
6582#define SSL_CERTIFICATE_EXPECTED 0
6583#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006584MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006585static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6586 int authmode )
6587{
6588 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6589 ssl->handshake->ciphersuite_info;
6590
6591 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6592 return( SSL_CERTIFICATE_SKIP );
6593
6594#if defined(MBEDTLS_SSL_SRV_C)
6595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6596 {
6597 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6598 return( SSL_CERTIFICATE_SKIP );
6599
6600 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6601 {
6602 ssl->session_negotiate->verify_result =
6603 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6604 return( SSL_CERTIFICATE_SKIP );
6605 }
6606 }
6607#else
6608 ((void) authmode);
6609#endif /* MBEDTLS_SSL_SRV_C */
6610
6611 return( SSL_CERTIFICATE_EXPECTED );
6612}
6613
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006614MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006615static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6616 int authmode,
6617 mbedtls_x509_crt *chain,
6618 void *rs_ctx )
6619{
6620 int ret = 0;
6621 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6622 ssl->handshake->ciphersuite_info;
6623 int have_ca_chain = 0;
6624
6625 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6626 void *p_vrfy;
6627
6628 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6629 return( 0 );
6630
6631 if( ssl->f_vrfy != NULL )
6632 {
6633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6634 f_vrfy = ssl->f_vrfy;
6635 p_vrfy = ssl->p_vrfy;
6636 }
6637 else
6638 {
6639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6640 f_vrfy = ssl->conf->f_vrfy;
6641 p_vrfy = ssl->conf->p_vrfy;
6642 }
6643
6644 /*
6645 * Main check: verify certificate
6646 */
6647#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6648 if( ssl->conf->f_ca_cb != NULL )
6649 {
6650 ((void) rs_ctx);
6651 have_ca_chain = 1;
6652
6653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6654 ret = mbedtls_x509_crt_verify_with_ca_cb(
6655 chain,
6656 ssl->conf->f_ca_cb,
6657 ssl->conf->p_ca_cb,
6658 ssl->conf->cert_profile,
6659 ssl->hostname,
6660 &ssl->session_negotiate->verify_result,
6661 f_vrfy, p_vrfy );
6662 }
6663 else
6664#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6665 {
6666 mbedtls_x509_crt *ca_chain;
6667 mbedtls_x509_crl *ca_crl;
6668
6669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6670 if( ssl->handshake->sni_ca_chain != NULL )
6671 {
6672 ca_chain = ssl->handshake->sni_ca_chain;
6673 ca_crl = ssl->handshake->sni_ca_crl;
6674 }
6675 else
6676#endif
6677 {
6678 ca_chain = ssl->conf->ca_chain;
6679 ca_crl = ssl->conf->ca_crl;
6680 }
6681
6682 if( ca_chain != NULL )
6683 have_ca_chain = 1;
6684
6685 ret = mbedtls_x509_crt_verify_restartable(
6686 chain,
6687 ca_chain, ca_crl,
6688 ssl->conf->cert_profile,
6689 ssl->hostname,
6690 &ssl->session_negotiate->verify_result,
6691 f_vrfy, p_vrfy, rs_ctx );
6692 }
6693
6694 if( ret != 0 )
6695 {
6696 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6697 }
6698
6699#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6700 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6701 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6702#endif
6703
6704 /*
6705 * Secondary checks: always done, but change 'ret' only if it was 0
6706 */
6707
6708#if defined(MBEDTLS_ECP_C)
6709 {
6710 const mbedtls_pk_context *pk = &chain->pk;
6711
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006712 /* If certificate uses an EC key, make sure the curve is OK.
6713 * This is a public key, so it can't be opaque, so can_do() is a good
6714 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006715 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006716 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006717 /* and in the unlikely case the above assumption no longer holds
6718 * we are making sure that pk_ec() here does not return a NULL
6719 */
6720 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6721 if( ec == NULL )
6722 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6725 }
Jerry Yud9526692022-02-17 14:23:47 +08006726
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006727 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6728 {
6729 ssl->session_negotiate->verify_result |=
6730 MBEDTLS_X509_BADCERT_BAD_KEY;
6731
6732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6733 if( ret == 0 )
6734 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6735 }
Jerry Yud9526692022-02-17 14:23:47 +08006736 }
6737 }
6738#endif /* MBEDTLS_ECP_C */
6739
6740 if( mbedtls_ssl_check_cert_usage( chain,
6741 ciphersuite_info,
6742 ! ssl->conf->endpoint,
6743 &ssl->session_negotiate->verify_result ) != 0 )
6744 {
6745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6746 if( ret == 0 )
6747 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6748 }
6749
6750 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6751 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6752 * with details encoded in the verification flags. All other kinds
6753 * of error codes, including those from the user provided f_vrfy
6754 * functions, are treated as fatal and lead to a failure of
6755 * ssl_parse_certificate even if verification was optional. */
6756 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6757 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6758 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6759 {
6760 ret = 0;
6761 }
6762
6763 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6764 {
6765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6766 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6767 }
6768
6769 if( ret != 0 )
6770 {
6771 uint8_t alert;
6772
6773 /* The certificate may have been rejected for several reasons.
6774 Pick one and send the corresponding alert. Which alert to send
6775 may be a subject of debate in some cases. */
6776 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6777 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6778 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6779 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6780 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6781 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6782 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6783 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6784 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6785 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6786 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6787 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6788 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6789 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6790 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6791 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6792 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6793 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6794 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6795 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6796 else
6797 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6798 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6799 alert );
6800 }
6801
6802#if defined(MBEDTLS_DEBUG_C)
6803 if( ssl->session_negotiate->verify_result != 0 )
6804 {
6805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6806 (unsigned int) ssl->session_negotiate->verify_result ) );
6807 }
6808 else
6809 {
6810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6811 }
6812#endif /* MBEDTLS_DEBUG_C */
6813
6814 return( ret );
6815}
6816
6817#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006818MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006819static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6820 unsigned char *start, size_t len )
6821{
6822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6823 /* Remember digest of the peer's end-CRT. */
6824 ssl->session_negotiate->peer_cert_digest =
6825 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6826 if( ssl->session_negotiate->peer_cert_digest == NULL )
6827 {
6828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6829 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6830 mbedtls_ssl_send_alert_message( ssl,
6831 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6832 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6833
6834 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6835 }
6836
6837 ret = mbedtls_md( mbedtls_md_info_from_type(
6838 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6839 start, len,
6840 ssl->session_negotiate->peer_cert_digest );
6841
6842 ssl->session_negotiate->peer_cert_digest_type =
6843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6844 ssl->session_negotiate->peer_cert_digest_len =
6845 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6846
6847 return( ret );
6848}
6849
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006850MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006851static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6852 unsigned char *start, size_t len )
6853{
6854 unsigned char *end = start + len;
6855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6856
6857 /* Make a copy of the peer's raw public key. */
6858 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6859 ret = mbedtls_pk_parse_subpubkey( &start, end,
6860 &ssl->handshake->peer_pubkey );
6861 if( ret != 0 )
6862 {
6863 /* We should have parsed the public key before. */
6864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6865 }
6866
6867 return( 0 );
6868}
6869#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6870
6871int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6872{
6873 int ret = 0;
6874 int crt_expected;
6875#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6876 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6877 ? ssl->handshake->sni_authmode
6878 : ssl->conf->authmode;
6879#else
6880 const int authmode = ssl->conf->authmode;
6881#endif
6882 void *rs_ctx = NULL;
6883 mbedtls_x509_crt *chain = NULL;
6884
6885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6886
6887 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6888 if( crt_expected == SSL_CERTIFICATE_SKIP )
6889 {
6890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6891 goto exit;
6892 }
6893
6894#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6895 if( ssl->handshake->ecrs_enabled &&
6896 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6897 {
6898 chain = ssl->handshake->ecrs_peer_cert;
6899 ssl->handshake->ecrs_peer_cert = NULL;
6900 goto crt_verify;
6901 }
6902#endif
6903
6904 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6905 {
6906 /* mbedtls_ssl_read_record may have sent an alert already. We
6907 let it decide whether to alert. */
6908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6909 goto exit;
6910 }
6911
6912#if defined(MBEDTLS_SSL_SRV_C)
6913 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6914 {
6915 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6916
6917 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6918 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6919
6920 goto exit;
6921 }
6922#endif /* MBEDTLS_SSL_SRV_C */
6923
6924 /* Clear existing peer CRT structure in case we tried to
6925 * reuse a session but it failed, and allocate a new one. */
6926 ssl_clear_peer_cert( ssl->session_negotiate );
6927
6928 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6929 if( chain == NULL )
6930 {
6931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6932 sizeof( mbedtls_x509_crt ) ) );
6933 mbedtls_ssl_send_alert_message( ssl,
6934 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6935 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6936
6937 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6938 goto exit;
6939 }
6940 mbedtls_x509_crt_init( chain );
6941
6942 ret = ssl_parse_certificate_chain( ssl, chain );
6943 if( ret != 0 )
6944 goto exit;
6945
6946#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6947 if( ssl->handshake->ecrs_enabled)
6948 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6949
6950crt_verify:
6951 if( ssl->handshake->ecrs_enabled)
6952 rs_ctx = &ssl->handshake->ecrs_ctx;
6953#endif
6954
6955 ret = ssl_parse_certificate_verify( ssl, authmode,
6956 chain, rs_ctx );
6957 if( ret != 0 )
6958 goto exit;
6959
6960#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6961 {
6962 unsigned char *crt_start, *pk_start;
6963 size_t crt_len, pk_len;
6964
6965 /* We parse the CRT chain without copying, so
6966 * these pointers point into the input buffer,
6967 * and are hence still valid after freeing the
6968 * CRT chain. */
6969
6970 crt_start = chain->raw.p;
6971 crt_len = chain->raw.len;
6972
6973 pk_start = chain->pk_raw.p;
6974 pk_len = chain->pk_raw.len;
6975
6976 /* Free the CRT structures before computing
6977 * digest and copying the peer's public key. */
6978 mbedtls_x509_crt_free( chain );
6979 mbedtls_free( chain );
6980 chain = NULL;
6981
6982 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6983 if( ret != 0 )
6984 goto exit;
6985
6986 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6987 if( ret != 0 )
6988 goto exit;
6989 }
6990#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6991 /* Pass ownership to session structure. */
6992 ssl->session_negotiate->peer_cert = chain;
6993 chain = NULL;
6994#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6995
6996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6997
6998exit:
6999
7000 if( ret == 0 )
7001 ssl->state++;
7002
7003#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7004 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7005 {
7006 ssl->handshake->ecrs_peer_cert = chain;
7007 chain = NULL;
7008 }
7009#endif
7010
7011 if( chain != NULL )
7012 {
7013 mbedtls_x509_crt_free( chain );
7014 mbedtls_free( chain );
7015 }
7016
7017 return( ret );
7018}
7019#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7020
Andrzej Kurek25f27152022-08-17 16:09:31 -04007021#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007022static void ssl_calc_finished_tls_sha256(
7023 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7024{
7025 int len = 12;
7026 const char *sender;
7027 unsigned char padbuf[32];
7028#if defined(MBEDTLS_USE_PSA_CRYPTO)
7029 size_t hash_size;
7030 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7031 psa_status_t status;
7032#else
7033 mbedtls_sha256_context sha256;
7034#endif
7035
7036 mbedtls_ssl_session *session = ssl->session_negotiate;
7037 if( !session )
7038 session = ssl->session;
7039
7040 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7041 ? "client finished"
7042 : "server finished";
7043
7044#if defined(MBEDTLS_USE_PSA_CRYPTO)
7045 sha256_psa = psa_hash_operation_init();
7046
7047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7048
7049 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7050 if( status != PSA_SUCCESS )
7051 {
7052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7053 return;
7054 }
7055
7056 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7057 if( status != PSA_SUCCESS )
7058 {
7059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7060 return;
7061 }
7062 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7063#else
7064
7065 mbedtls_sha256_init( &sha256 );
7066
7067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7068
7069 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7070
7071 /*
7072 * TLSv1.2:
7073 * hash = PRF( master, finished_label,
7074 * Hash( handshake ) )[0.11]
7075 */
7076
7077#if !defined(MBEDTLS_SHA256_ALT)
7078 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7079 sha256.state, sizeof( sha256.state ) );
7080#endif
7081
7082 mbedtls_sha256_finish( &sha256, padbuf );
7083 mbedtls_sha256_free( &sha256 );
7084#endif /* MBEDTLS_USE_PSA_CRYPTO */
7085
7086 ssl->handshake->tls_prf( session->master, 48, sender,
7087 padbuf, 32, buf, len );
7088
7089 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7090
7091 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7092
7093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7094}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007095#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007096
Jerry Yub7ba49e2022-02-17 14:25:53 +08007097
Andrzej Kurek25f27152022-08-17 16:09:31 -04007098#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007099static void ssl_calc_finished_tls_sha384(
7100 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7101{
7102 int len = 12;
7103 const char *sender;
7104 unsigned char padbuf[48];
7105#if defined(MBEDTLS_USE_PSA_CRYPTO)
7106 size_t hash_size;
7107 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7108 psa_status_t status;
7109#else
7110 mbedtls_sha512_context sha512;
7111#endif
7112
7113 mbedtls_ssl_session *session = ssl->session_negotiate;
7114 if( !session )
7115 session = ssl->session;
7116
7117 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7118 ? "client finished"
7119 : "server finished";
7120
7121#if defined(MBEDTLS_USE_PSA_CRYPTO)
7122 sha384_psa = psa_hash_operation_init();
7123
7124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7125
7126 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7127 if( status != PSA_SUCCESS )
7128 {
7129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7130 return;
7131 }
7132
7133 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7134 if( status != PSA_SUCCESS )
7135 {
7136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7137 return;
7138 }
7139 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7140#else
7141 mbedtls_sha512_init( &sha512 );
7142
7143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7144
Andrzej Kureka242e832022-08-11 10:03:14 -04007145 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007146
7147 /*
7148 * TLSv1.2:
7149 * hash = PRF( master, finished_label,
7150 * Hash( handshake ) )[0.11]
7151 */
7152
7153#if !defined(MBEDTLS_SHA512_ALT)
7154 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7155 sha512.state, sizeof( sha512.state ) );
7156#endif
7157 mbedtls_sha512_finish( &sha512, padbuf );
7158
7159 mbedtls_sha512_free( &sha512 );
7160#endif
7161
7162 ssl->handshake->tls_prf( session->master, 48, sender,
7163 padbuf, 48, buf, len );
7164
7165 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7166
7167 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7168
7169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7170}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007171#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007172
Jerry Yuaef00152022-02-17 14:27:31 +08007173void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7174{
7175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7176
7177 /*
7178 * Free our handshake params
7179 */
7180 mbedtls_ssl_handshake_free( ssl );
7181 mbedtls_free( ssl->handshake );
7182 ssl->handshake = NULL;
7183
7184 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007185 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007186 */
7187 if( ssl->transform )
7188 {
7189 mbedtls_ssl_transform_free( ssl->transform );
7190 mbedtls_free( ssl->transform );
7191 }
7192 ssl->transform = ssl->transform_negotiate;
7193 ssl->transform_negotiate = NULL;
7194
7195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7196}
7197
Jerry Yu2a9fff52022-02-17 14:28:51 +08007198void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7199{
7200 int resume = ssl->handshake->resume;
7201
7202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7203
7204#if defined(MBEDTLS_SSL_RENEGOTIATION)
7205 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7206 {
7207 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7208 ssl->renego_records_seen = 0;
7209 }
7210#endif
7211
7212 /*
7213 * Free the previous session and switch in the current one
7214 */
7215 if( ssl->session )
7216 {
7217#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7218 /* RFC 7366 3.1: keep the EtM state */
7219 ssl->session_negotiate->encrypt_then_mac =
7220 ssl->session->encrypt_then_mac;
7221#endif
7222
7223 mbedtls_ssl_session_free( ssl->session );
7224 mbedtls_free( ssl->session );
7225 }
7226 ssl->session = ssl->session_negotiate;
7227 ssl->session_negotiate = NULL;
7228
7229 /*
7230 * Add cache entry
7231 */
7232 if( ssl->conf->f_set_cache != NULL &&
7233 ssl->session->id_len != 0 &&
7234 resume == 0 )
7235 {
7236 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7237 ssl->session->id,
7238 ssl->session->id_len,
7239 ssl->session ) != 0 )
7240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7241 }
7242
7243#if defined(MBEDTLS_SSL_PROTO_DTLS)
7244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7245 ssl->handshake->flight != NULL )
7246 {
7247 /* Cancel handshake timer */
7248 mbedtls_ssl_set_timer( ssl, 0 );
7249
7250 /* Keep last flight around in case we need to resend it:
7251 * we need the handshake and transform structures for that */
7252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7253 }
7254 else
7255#endif
7256 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7257
7258 ssl->state++;
7259
7260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7261}
7262
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007263int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7264{
7265 int ret, hash_len;
7266
7267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7268
7269 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7270
7271 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7272
7273 /*
7274 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7275 * may define some other value. Currently (early 2016), no defined
7276 * ciphersuite does this (and this is unlikely to change as activity has
7277 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7278 */
7279 hash_len = 12;
7280
7281#if defined(MBEDTLS_SSL_RENEGOTIATION)
7282 ssl->verify_data_len = hash_len;
7283 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7284#endif
7285
7286 ssl->out_msglen = 4 + hash_len;
7287 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7288 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7289
7290 /*
7291 * In case of session resuming, invert the client and server
7292 * ChangeCipherSpec messages order.
7293 */
7294 if( ssl->handshake->resume != 0 )
7295 {
7296#if defined(MBEDTLS_SSL_CLI_C)
7297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7298 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7299#endif
7300#if defined(MBEDTLS_SSL_SRV_C)
7301 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7302 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7303#endif
7304 }
7305 else
7306 ssl->state++;
7307
7308 /*
7309 * Switch to our negotiated transform and session parameters for outbound
7310 * data.
7311 */
7312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7313
7314#if defined(MBEDTLS_SSL_PROTO_DTLS)
7315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7316 {
7317 unsigned char i;
7318
7319 /* Remember current epoch settings for resending */
7320 ssl->handshake->alt_transform_out = ssl->transform_out;
7321 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7322 sizeof( ssl->handshake->alt_out_ctr ) );
7323
7324 /* Set sequence_number to zero */
7325 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7326
7327
7328 /* Increment epoch */
7329 for( i = 2; i > 0; i-- )
7330 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7331 break;
7332
7333 /* The loop goes to its end iff the counter is wrapping */
7334 if( i == 0 )
7335 {
7336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7337 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7338 }
7339 }
7340 else
7341#endif /* MBEDTLS_SSL_PROTO_DTLS */
7342 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7343
7344 ssl->transform_out = ssl->transform_negotiate;
7345 ssl->session_out = ssl->session_negotiate;
7346
7347#if defined(MBEDTLS_SSL_PROTO_DTLS)
7348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7349 mbedtls_ssl_send_flight_completed( ssl );
7350#endif
7351
7352 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7353 {
7354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7355 return( ret );
7356 }
7357
7358#if defined(MBEDTLS_SSL_PROTO_DTLS)
7359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7360 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7361 {
7362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7363 return( ret );
7364 }
7365#endif
7366
7367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7368
7369 return( 0 );
7370}
7371
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007372#define SSL_MAX_HASH_LEN 12
7373
7374int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7375{
7376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7377 unsigned int hash_len = 12;
7378 unsigned char buf[SSL_MAX_HASH_LEN];
7379
7380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7381
7382 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7383
7384 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7385 {
7386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7387 goto exit;
7388 }
7389
7390 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7391 {
7392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7393 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7394 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7395 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7396 goto exit;
7397 }
7398
7399 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7400 {
7401 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7402 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7403 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7404 goto exit;
7405 }
7406
7407 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7408 {
7409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7410 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7411 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7412 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7413 goto exit;
7414 }
7415
7416 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7417 buf, hash_len ) != 0 )
7418 {
7419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7420 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7421 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7422 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7423 goto exit;
7424 }
7425
7426#if defined(MBEDTLS_SSL_RENEGOTIATION)
7427 ssl->verify_data_len = hash_len;
7428 memcpy( ssl->peer_verify_data, buf, hash_len );
7429#endif
7430
7431 if( ssl->handshake->resume != 0 )
7432 {
7433#if defined(MBEDTLS_SSL_CLI_C)
7434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7435 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7436#endif
7437#if defined(MBEDTLS_SSL_SRV_C)
7438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7439 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7440#endif
7441 }
7442 else
7443 ssl->state++;
7444
7445#if defined(MBEDTLS_SSL_PROTO_DTLS)
7446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7447 mbedtls_ssl_recv_flight_completed( ssl );
7448#endif
7449
7450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7451
7452exit:
7453 mbedtls_platform_zeroize( buf, hash_len );
7454 return( ret );
7455}
7456
Jerry Yu392112c2022-02-17 14:34:10 +08007457#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7458/*
7459 * Helper to get TLS 1.2 PRF from ciphersuite
7460 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7461 */
7462static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7463{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007464#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007465 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7466 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7467
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007468 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007469 return( tls_prf_sha384 );
7470#else
7471 (void) ciphersuite_id;
7472#endif
7473 return( tls_prf_sha256 );
7474}
7475#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007476
7477static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7478{
7479 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007480#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007481 if( tls_prf == tls_prf_sha384 )
7482 {
7483 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7484 }
7485 else
7486#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007487#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007488 if( tls_prf == tls_prf_sha256 )
7489 {
7490 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7491 }
7492 else
7493#endif
7494 return( MBEDTLS_SSL_TLS_PRF_NONE );
7495}
7496
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007497/*
7498 * Populate a transform structure with session keys and all the other
7499 * necessary information.
7500 *
7501 * Parameters:
7502 * - [in/out]: transform: structure to populate
7503 * [in] must be just initialised with mbedtls_ssl_transform_init()
7504 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7505 * - [in] ciphersuite
7506 * - [in] master
7507 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007508 * - [in] tls_prf: pointer to PRF to use for key derivation
7509 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007510 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007511 * - [in] endpoint: client or server
7512 * - [in] ssl: used for:
7513 * - ssl->conf->{f,p}_export_keys
7514 * [in] optionally used for:
7515 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7516 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007517MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007518static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7519 int ciphersuite,
7520 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007521#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007522 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007523#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007524 ssl_tls_prf_t tls_prf,
7525 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007526 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007527 unsigned endpoint,
7528 const mbedtls_ssl_context *ssl )
7529{
7530 int ret = 0;
7531 unsigned char keyblk[256];
7532 unsigned char *key1;
7533 unsigned char *key2;
7534 unsigned char *mac_enc;
7535 unsigned char *mac_dec;
7536 size_t mac_key_len = 0;
7537 size_t iv_copy_len;
7538 size_t keylen;
7539 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007540 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007541#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007542 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007543 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007544#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007545
7546#if defined(MBEDTLS_USE_PSA_CRYPTO)
7547 psa_key_type_t key_type;
7548 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7549 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007550 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007551 size_t key_bits;
7552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7553#endif
7554
7555#if !defined(MBEDTLS_DEBUG_C) && \
7556 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7557 if( ssl->f_export_keys == NULL )
7558 {
7559 ssl = NULL; /* make sure we don't use it except for these cases */
7560 (void) ssl;
7561 }
7562#endif
7563
7564 /*
7565 * Some data just needs copying into the structure
7566 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007567#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007568 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007569#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007570 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007571
7572#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7573 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7574#endif
7575
7576#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007577 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007578 {
7579 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7580 * generation separate. This should never happen. */
7581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7582 }
7583#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7584
7585 /*
7586 * Get various info structures
7587 */
7588 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7589 if( ciphersuite_info == NULL )
7590 {
7591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7592 ciphersuite ) );
7593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7594 }
7595
Neil Armstrongab555e02022-04-04 11:07:59 +02007596 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007597#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007598 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007599#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007600 ciphersuite_info );
7601
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007602 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7603 transform->taglen =
7604 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7605
7606#if defined(MBEDTLS_USE_PSA_CRYPTO)
7607 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7608 transform->taglen,
7609 &alg,
7610 &key_type,
7611 &key_bits ) ) != PSA_SUCCESS )
7612 {
7613 ret = psa_ssl_status_to_mbedtls( status );
7614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7615 goto end;
7616 }
7617#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007618 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7619 if( cipher_info == NULL )
7620 {
7621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7622 ciphersuite_info->cipher ) );
7623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7624 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007625#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007626
Neil Armstronge4512952022-03-08 09:08:22 +01007627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007628 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007629 if( mac_alg == 0 )
7630 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007632 (unsigned) ciphersuite_info->mac ) );
7633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7634 }
7635#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007636 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7637 if( md_info == NULL )
7638 {
7639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7640 (unsigned) ciphersuite_info->mac ) );
7641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7642 }
Neil Armstronge4512952022-03-08 09:08:22 +01007643#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007644
7645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7646 /* Copy own and peer's CID if the use of the CID
7647 * extension has been negotiated. */
7648 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7649 {
7650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7651
7652 transform->in_cid_len = ssl->own_cid_len;
7653 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7654 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7655 transform->in_cid_len );
7656
7657 transform->out_cid_len = ssl->handshake->peer_cid_len;
7658 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7659 ssl->handshake->peer_cid_len );
7660 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7661 transform->out_cid_len );
7662 }
7663#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7664
7665 /*
7666 * Compute key block using the PRF
7667 */
7668 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7669 if( ret != 0 )
7670 {
7671 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7672 return( ret );
7673 }
7674
7675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7676 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7677 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7678 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7679 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7680
7681 /*
7682 * Determine the appropriate key, IV and MAC length.
7683 */
7684
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007685#if defined(MBEDTLS_USE_PSA_CRYPTO)
7686 keylen = PSA_BITS_TO_BYTES(key_bits);
7687#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007688 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007689#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007690
7691#if defined(MBEDTLS_GCM_C) || \
7692 defined(MBEDTLS_CCM_C) || \
7693 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007694 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007695 {
7696 size_t explicit_ivlen;
7697
7698 transform->maclen = 0;
7699 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007700
7701 /* All modes haves 96-bit IVs, but the length of the static parts vary
7702 * with mode and version:
7703 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7704 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7705 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7706 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7707 * sequence number).
7708 */
7709 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007710#if defined(MBEDTLS_USE_PSA_CRYPTO)
7711 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7712#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007713 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007714#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007715 transform->fixed_ivlen = 12;
7716 else
7717 transform->fixed_ivlen = 4;
7718
7719 /* Minimum length of encrypted record */
7720 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7721 transform->minlen = explicit_ivlen + transform->taglen;
7722 }
7723 else
7724#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7725#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007726 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7727 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7728 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007729 {
Neil Armstronge4512952022-03-08 09:08:22 +01007730#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007731 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007732#else
7733 size_t block_size = cipher_info->block_size;
7734#endif /* MBEDTLS_USE_PSA_CRYPTO */
7735
7736#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007737 /* Get MAC length */
7738 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7739#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007740 /* Initialize HMAC contexts */
7741 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7742 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7743 {
7744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7745 goto end;
7746 }
7747
7748 /* Get MAC length */
7749 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007751 transform->maclen = mac_key_len;
7752
7753 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007754#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007755 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007756#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007757 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007758#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007759
7760 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007761 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007762 transform->minlen = transform->maclen;
7763 else
7764 {
7765 /*
7766 * GenericBlockCipher:
7767 * 1. if EtM is in use: one block plus MAC
7768 * otherwise: * first multiple of blocklen greater than maclen
7769 * 2. IV
7770 */
7771#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007772 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007773 {
7774 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007775 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007776 }
7777 else
7778#endif
7779 {
7780 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007781 + block_size
7782 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007783 }
7784
Glenn Strauss07c64162022-03-14 12:34:51 -04007785 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007786 {
7787 transform->minlen += transform->ivlen;
7788 }
7789 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007790 {
7791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7792 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7793 goto end;
7794 }
7795 }
7796 }
7797 else
7798#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7799 {
7800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7802 }
7803
7804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7805 (unsigned) keylen,
7806 (unsigned) transform->minlen,
7807 (unsigned) transform->ivlen,
7808 (unsigned) transform->maclen ) );
7809
7810 /*
7811 * Finally setup the cipher contexts, IVs and MAC secrets.
7812 */
7813#if defined(MBEDTLS_SSL_CLI_C)
7814 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7815 {
7816 key1 = keyblk + mac_key_len * 2;
7817 key2 = keyblk + mac_key_len * 2 + keylen;
7818
7819 mac_enc = keyblk;
7820 mac_dec = keyblk + mac_key_len;
7821
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007822 iv_copy_len = ( transform->fixed_ivlen ) ?
7823 transform->fixed_ivlen : transform->ivlen;
7824 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7825 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7826 iv_copy_len );
7827 }
7828 else
7829#endif /* MBEDTLS_SSL_CLI_C */
7830#if defined(MBEDTLS_SSL_SRV_C)
7831 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7832 {
7833 key1 = keyblk + mac_key_len * 2 + keylen;
7834 key2 = keyblk + mac_key_len * 2;
7835
7836 mac_enc = keyblk + mac_key_len;
7837 mac_dec = keyblk;
7838
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007839 iv_copy_len = ( transform->fixed_ivlen ) ?
7840 transform->fixed_ivlen : transform->ivlen;
7841 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7842 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7843 iv_copy_len );
7844 }
7845 else
7846#endif /* MBEDTLS_SSL_SRV_C */
7847 {
7848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7849 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7850 goto end;
7851 }
7852
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007853 if( ssl != NULL && ssl->f_export_keys != NULL )
7854 {
7855 ssl->f_export_keys( ssl->p_export_keys,
7856 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7857 master, 48,
7858 randbytes + 32,
7859 randbytes,
7860 tls_prf_get_type( tls_prf ) );
7861 }
7862
7863#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007864 transform->psa_alg = alg;
7865
7866 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7867 {
7868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7869 psa_set_key_algorithm( &attributes, alg );
7870 psa_set_key_type( &attributes, key_type );
7871
7872 if( ( status = psa_import_key( &attributes,
7873 key1,
7874 PSA_BITS_TO_BYTES( key_bits ),
7875 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7876 {
7877 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7878 ret = psa_ssl_status_to_mbedtls( status );
7879 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7880 goto end;
7881 }
7882
7883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7884
7885 if( ( status = psa_import_key( &attributes,
7886 key2,
7887 PSA_BITS_TO_BYTES( key_bits ),
7888 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7889 {
7890 ret = psa_ssl_status_to_mbedtls( status );
7891 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7892 goto end;
7893 }
7894 }
7895#else
7896 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7897 cipher_info ) ) != 0 )
7898 {
7899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7900 goto end;
7901 }
7902
7903 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7904 cipher_info ) ) != 0 )
7905 {
7906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7907 goto end;
7908 }
7909
7910 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7911 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7912 MBEDTLS_ENCRYPT ) ) != 0 )
7913 {
7914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7915 goto end;
7916 }
7917
7918 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7919 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7920 MBEDTLS_DECRYPT ) ) != 0 )
7921 {
7922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7923 goto end;
7924 }
7925
7926#if defined(MBEDTLS_CIPHER_MODE_CBC)
7927 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7928 {
7929 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7930 MBEDTLS_PADDING_NONE ) ) != 0 )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7933 goto end;
7934 }
7935
7936 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7937 MBEDTLS_PADDING_NONE ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7940 goto end;
7941 }
7942 }
7943#endif /* MBEDTLS_CIPHER_MODE_CBC */
7944#endif /* MBEDTLS_USE_PSA_CRYPTO */
7945
Neil Armstrong29c0c042022-03-17 17:47:28 +01007946#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7947 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7948 For AEAD-based ciphersuites, there is nothing to do here. */
7949 if( mac_key_len != 0 )
7950 {
7951#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007952 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007953
7954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007955 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007956 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7957
7958 if( ( status = psa_import_key( &attributes,
7959 mac_enc, mac_key_len,
7960 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7961 {
7962 ret = psa_ssl_status_to_mbedtls( status );
7963 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7964 goto end;
7965 }
7966
Ronald Cronfb39f152022-03-25 14:36:28 +01007967 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007968 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7969#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7970 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7971#endif
7972 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007973 /* mbedtls_ct_hmac() requires the key to be exportable */
7974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7975 PSA_KEY_USAGE_VERIFY_HASH );
7976 else
7977 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7978
7979 if( ( status = psa_import_key( &attributes,
7980 mac_dec, mac_key_len,
7981 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
7982 {
7983 ret = psa_ssl_status_to_mbedtls( status );
7984 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7985 goto end;
7986 }
7987#else
7988 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7989 if( ret != 0 )
7990 goto end;
7991 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7992 if( ret != 0 )
7993 goto end;
7994#endif /* MBEDTLS_USE_PSA_CRYPTO */
7995 }
7996#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7997
7998 ((void) mac_dec);
7999 ((void) mac_enc);
8000
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008001end:
8002 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8003 return( ret );
8004}
8005
Jerry Yuee40f9d2022-02-17 14:55:16 +08008006#if defined(MBEDTLS_USE_PSA_CRYPTO)
8007int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8008 unsigned char *hash, size_t *hashlen,
8009 unsigned char *data, size_t data_len,
8010 mbedtls_md_type_t md_alg )
8011{
8012 psa_status_t status;
8013 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008014 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008015
8016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8017
8018 if( ( status = psa_hash_setup( &hash_operation,
8019 hash_alg ) ) != PSA_SUCCESS )
8020 {
8021 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8022 goto exit;
8023 }
8024
8025 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8026 64 ) ) != PSA_SUCCESS )
8027 {
8028 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8029 goto exit;
8030 }
8031
8032 if( ( status = psa_hash_update( &hash_operation,
8033 data, data_len ) ) != PSA_SUCCESS )
8034 {
8035 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8036 goto exit;
8037 }
8038
8039 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8040 hashlen ) ) != PSA_SUCCESS )
8041 {
8042 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8043 goto exit;
8044 }
8045
8046exit:
8047 if( status != PSA_SUCCESS )
8048 {
8049 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8050 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8051 switch( status )
8052 {
8053 case PSA_ERROR_NOT_SUPPORTED:
8054 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8055 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8056 case PSA_ERROR_BUFFER_TOO_SMALL:
8057 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8058 case PSA_ERROR_INSUFFICIENT_MEMORY:
8059 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8060 default:
8061 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8062 }
8063 }
8064 return( 0 );
8065}
8066
8067#else
8068
8069int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8070 unsigned char *hash, size_t *hashlen,
8071 unsigned char *data, size_t data_len,
8072 mbedtls_md_type_t md_alg )
8073{
8074 int ret = 0;
8075 mbedtls_md_context_t ctx;
8076 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8077 *hashlen = mbedtls_md_get_size( md_info );
8078
8079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8080
8081 mbedtls_md_init( &ctx );
8082
8083 /*
8084 * digitally-signed struct {
8085 * opaque client_random[32];
8086 * opaque server_random[32];
8087 * ServerDHParams params;
8088 * };
8089 */
8090 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8091 {
8092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8093 goto exit;
8094 }
8095 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8096 {
8097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8098 goto exit;
8099 }
8100 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8101 {
8102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8103 goto exit;
8104 }
8105 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8106 {
8107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8108 goto exit;
8109 }
8110 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8111 {
8112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8113 goto exit;
8114 }
8115
8116exit:
8117 mbedtls_md_free( &ctx );
8118
8119 if( ret != 0 )
8120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8121 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8122
8123 return( ret );
8124}
8125#endif /* MBEDTLS_USE_PSA_CRYPTO */
8126
Jerry Yud9d91da2022-02-17 14:57:06 +08008127#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8128
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008129/* Find the preferred hash for a given signature algorithm. */
8130unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8131 mbedtls_ssl_context *ssl,
8132 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008133{
Gabor Mezei078e8032022-04-27 21:17:56 +02008134 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008135 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008136
8137 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008138 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008139
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008140 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008141 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008142 unsigned int hash_alg_received =
8143 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8144 received_sig_algs[i] );
8145 unsigned int sig_alg_received =
8146 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8147 received_sig_algs[i] );
8148
8149 if( sig_alg == sig_alg_received )
8150 {
8151#if defined(MBEDTLS_USE_PSA_CRYPTO)
8152 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8153 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008154 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008155 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008156
Neil Armstrong96eceb82022-06-30 18:05:05 +02008157 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8158 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8159 PSA_ALG_ECDSA( psa_hash_alg ),
8160 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008161 continue;
8162
Neil Armstrong96eceb82022-06-30 18:05:05 +02008163 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008164 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8165 PSA_ALG_RSA_PKCS1V15_SIGN(
8166 psa_hash_alg ),
8167 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008168 continue;
8169 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008170#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008171
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008172 return( hash_alg_received );
8173 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008174 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008175
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008176 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008177}
8178
8179#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8180
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008181/* Serialization of TLS 1.2 sessions:
8182 *
8183 * struct {
8184 * uint64 start_time;
8185 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008186 * uint8 session_id_len; // at most 32
8187 * opaque session_id[32];
8188 * opaque master[48]; // fixed length in the standard
8189 * uint32 verify_result;
8190 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8191 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8192 * uint32 ticket_lifetime;
8193 * uint8 mfl_code; // up to 255 according to standard
8194 * uint8 encrypt_then_mac; // 0 or 1
8195 * } serialized_session_tls12;
8196 *
8197 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008198static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008199 unsigned char *buf,
8200 size_t buf_len )
8201{
8202 unsigned char *p = buf;
8203 size_t used = 0;
8204
8205#if defined(MBEDTLS_HAVE_TIME)
8206 uint64_t start;
8207#endif
8208#if defined(MBEDTLS_X509_CRT_PARSE_C)
8209#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8210 size_t cert_len;
8211#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8212#endif /* MBEDTLS_X509_CRT_PARSE_C */
8213
8214 /*
8215 * Time
8216 */
8217#if defined(MBEDTLS_HAVE_TIME)
8218 used += 8;
8219
8220 if( used <= buf_len )
8221 {
8222 start = (uint64_t) session->start;
8223
8224 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8225 p += 8;
8226 }
8227#endif /* MBEDTLS_HAVE_TIME */
8228
8229 /*
8230 * Basic mandatory fields
8231 */
8232 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008233 + 1 /* id_len */
8234 + sizeof( session->id )
8235 + sizeof( session->master )
8236 + 4; /* verify_result */
8237
8238 if( used <= buf_len )
8239 {
8240 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8241 p += 2;
8242
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008243 *p++ = MBEDTLS_BYTE_0( session->id_len );
8244 memcpy( p, session->id, 32 );
8245 p += 32;
8246
8247 memcpy( p, session->master, 48 );
8248 p += 48;
8249
8250 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8251 p += 4;
8252 }
8253
8254 /*
8255 * Peer's end-entity certificate
8256 */
8257#if defined(MBEDTLS_X509_CRT_PARSE_C)
8258#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8259 if( session->peer_cert == NULL )
8260 cert_len = 0;
8261 else
8262 cert_len = session->peer_cert->raw.len;
8263
8264 used += 3 + cert_len;
8265
8266 if( used <= buf_len )
8267 {
8268 *p++ = MBEDTLS_BYTE_2( cert_len );
8269 *p++ = MBEDTLS_BYTE_1( cert_len );
8270 *p++ = MBEDTLS_BYTE_0( cert_len );
8271
8272 if( session->peer_cert != NULL )
8273 {
8274 memcpy( p, session->peer_cert->raw.p, cert_len );
8275 p += cert_len;
8276 }
8277 }
8278#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8279 if( session->peer_cert_digest != NULL )
8280 {
8281 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8282 if( used <= buf_len )
8283 {
8284 *p++ = (unsigned char) session->peer_cert_digest_type;
8285 *p++ = (unsigned char) session->peer_cert_digest_len;
8286 memcpy( p, session->peer_cert_digest,
8287 session->peer_cert_digest_len );
8288 p += session->peer_cert_digest_len;
8289 }
8290 }
8291 else
8292 {
8293 used += 2;
8294 if( used <= buf_len )
8295 {
8296 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8297 *p++ = 0;
8298 }
8299 }
8300#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8301#endif /* MBEDTLS_X509_CRT_PARSE_C */
8302
8303 /*
8304 * Session ticket if any, plus associated data
8305 */
8306#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8307 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8308
8309 if( used <= buf_len )
8310 {
8311 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8312 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8313 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8314
8315 if( session->ticket != NULL )
8316 {
8317 memcpy( p, session->ticket, session->ticket_len );
8318 p += session->ticket_len;
8319 }
8320
8321 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8322 p += 4;
8323 }
8324#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8325
8326 /*
8327 * Misc extension-related info
8328 */
8329#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8330 used += 1;
8331
8332 if( used <= buf_len )
8333 *p++ = session->mfl_code;
8334#endif
8335
8336#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8337 used += 1;
8338
8339 if( used <= buf_len )
8340 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8341#endif
8342
8343 return( used );
8344}
8345
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008346MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008347static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008348 const unsigned char *buf,
8349 size_t len )
8350{
8351#if defined(MBEDTLS_HAVE_TIME)
8352 uint64_t start;
8353#endif
8354#if defined(MBEDTLS_X509_CRT_PARSE_C)
8355#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8356 size_t cert_len;
8357#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8358#endif /* MBEDTLS_X509_CRT_PARSE_C */
8359
8360 const unsigned char *p = buf;
8361 const unsigned char * const end = buf + len;
8362
8363 /*
8364 * Time
8365 */
8366#if defined(MBEDTLS_HAVE_TIME)
8367 if( 8 > (size_t)( end - p ) )
8368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8369
8370 start = ( (uint64_t) p[0] << 56 ) |
8371 ( (uint64_t) p[1] << 48 ) |
8372 ( (uint64_t) p[2] << 40 ) |
8373 ( (uint64_t) p[3] << 32 ) |
8374 ( (uint64_t) p[4] << 24 ) |
8375 ( (uint64_t) p[5] << 16 ) |
8376 ( (uint64_t) p[6] << 8 ) |
8377 ( (uint64_t) p[7] );
8378 p += 8;
8379
8380 session->start = (time_t) start;
8381#endif /* MBEDTLS_HAVE_TIME */
8382
8383 /*
8384 * Basic mandatory fields
8385 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008386 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8388
8389 session->ciphersuite = ( p[0] << 8 ) | p[1];
8390 p += 2;
8391
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008392 session->id_len = *p++;
8393 memcpy( session->id, p, 32 );
8394 p += 32;
8395
8396 memcpy( session->master, p, 48 );
8397 p += 48;
8398
8399 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8400 ( (uint32_t) p[1] << 16 ) |
8401 ( (uint32_t) p[2] << 8 ) |
8402 ( (uint32_t) p[3] );
8403 p += 4;
8404
8405 /* Immediately clear invalid pointer values that have been read, in case
8406 * we exit early before we replaced them with valid ones. */
8407#if defined(MBEDTLS_X509_CRT_PARSE_C)
8408#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8409 session->peer_cert = NULL;
8410#else
8411 session->peer_cert_digest = NULL;
8412#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8413#endif /* MBEDTLS_X509_CRT_PARSE_C */
8414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8415 session->ticket = NULL;
8416#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8417
8418 /*
8419 * Peer certificate
8420 */
8421#if defined(MBEDTLS_X509_CRT_PARSE_C)
8422#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8423 /* Deserialize CRT from the end of the ticket. */
8424 if( 3 > (size_t)( end - p ) )
8425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8426
8427 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8428 p += 3;
8429
8430 if( cert_len != 0 )
8431 {
8432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8433
8434 if( cert_len > (size_t)( end - p ) )
8435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8436
8437 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8438
8439 if( session->peer_cert == NULL )
8440 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8441
8442 mbedtls_x509_crt_init( session->peer_cert );
8443
8444 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8445 p, cert_len ) ) != 0 )
8446 {
8447 mbedtls_x509_crt_free( session->peer_cert );
8448 mbedtls_free( session->peer_cert );
8449 session->peer_cert = NULL;
8450 return( ret );
8451 }
8452
8453 p += cert_len;
8454 }
8455#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8456 /* Deserialize CRT digest from the end of the ticket. */
8457 if( 2 > (size_t)( end - p ) )
8458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8459
8460 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8461 session->peer_cert_digest_len = (size_t) *p++;
8462
8463 if( session->peer_cert_digest_len != 0 )
8464 {
8465 const mbedtls_md_info_t *md_info =
8466 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8467 if( md_info == NULL )
8468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8469 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8470 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8471
8472 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8474
8475 session->peer_cert_digest =
8476 mbedtls_calloc( 1, session->peer_cert_digest_len );
8477 if( session->peer_cert_digest == NULL )
8478 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8479
8480 memcpy( session->peer_cert_digest, p,
8481 session->peer_cert_digest_len );
8482 p += session->peer_cert_digest_len;
8483 }
8484#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8485#endif /* MBEDTLS_X509_CRT_PARSE_C */
8486
8487 /*
8488 * Session ticket and associated data
8489 */
8490#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8491 if( 3 > (size_t)( end - p ) )
8492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8493
8494 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8495 p += 3;
8496
8497 if( session->ticket_len != 0 )
8498 {
8499 if( session->ticket_len > (size_t)( end - p ) )
8500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8501
8502 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8503 if( session->ticket == NULL )
8504 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8505
8506 memcpy( session->ticket, p, session->ticket_len );
8507 p += session->ticket_len;
8508 }
8509
8510 if( 4 > (size_t)( end - p ) )
8511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8512
8513 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8514 ( (uint32_t) p[1] << 16 ) |
8515 ( (uint32_t) p[2] << 8 ) |
8516 ( (uint32_t) p[3] );
8517 p += 4;
8518#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8519
8520 /*
8521 * Misc extension-related info
8522 */
8523#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8524 if( 1 > (size_t)( end - p ) )
8525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8526
8527 session->mfl_code = *p++;
8528#endif
8529
8530#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8531 if( 1 > (size_t)( end - p ) )
8532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8533
8534 session->encrypt_then_mac = *p++;
8535#endif
8536
8537 /* Done, should have consumed entire buffer */
8538 if( p != end )
8539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8540
8541 return( 0 );
8542}
Jerry Yudc7bd172022-02-17 13:44:15 +08008543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8544
XiaokangQian75d40ef2022-04-20 11:05:24 +00008545int mbedtls_ssl_validate_ciphersuite(
8546 const mbedtls_ssl_context *ssl,
8547 const mbedtls_ssl_ciphersuite_t *suite_info,
8548 mbedtls_ssl_protocol_version min_tls_version,
8549 mbedtls_ssl_protocol_version max_tls_version )
8550{
8551 (void) ssl;
8552
8553 if( suite_info == NULL )
8554 return( -1 );
8555
8556 if( ( suite_info->min_tls_version > max_tls_version ) ||
8557 ( suite_info->max_tls_version < min_tls_version ) )
8558 {
8559 return( -1 );
8560 }
8561
XiaokangQian060d8672022-04-21 09:24:56 +00008562#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008563#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8564 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8565 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8566 {
8567 return( -1 );
8568 }
8569#endif
8570
8571 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8572#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8573 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8574 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8575 {
8576 return( -1 );
8577 }
8578#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8580
8581 return( 0 );
8582}
8583
XiaokangQianeaf36512022-04-24 09:07:44 +00008584#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8585/*
8586 * Function for writing a signature algorithm extension.
8587 *
8588 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8589 * value (TLS 1.3 RFC8446):
8590 * enum {
8591 * ....
8592 * ecdsa_secp256r1_sha256( 0x0403 ),
8593 * ecdsa_secp384r1_sha384( 0x0503 ),
8594 * ecdsa_secp521r1_sha512( 0x0603 ),
8595 * ....
8596 * } SignatureScheme;
8597 *
8598 * struct {
8599 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8600 * } SignatureSchemeList;
8601 *
8602 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8603 * value (TLS 1.2 RFC5246):
8604 * enum {
8605 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8606 * sha512(6), (255)
8607 * } HashAlgorithm;
8608 *
8609 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8610 * SignatureAlgorithm;
8611 *
8612 * struct {
8613 * HashAlgorithm hash;
8614 * SignatureAlgorithm signature;
8615 * } SignatureAndHashAlgorithm;
8616 *
8617 * SignatureAndHashAlgorithm
8618 * supported_signature_algorithms<2..2^16-2>;
8619 *
8620 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8621 * generalization of the TLS 1.2 signature algorithm extension.
8622 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8623 * `SignatureScheme` field of TLS 1.3
8624 *
8625 */
8626int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8627 const unsigned char *end, size_t *out_len )
8628{
8629 unsigned char *p = buf;
8630 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8631 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8632
8633 *out_len = 0;
8634
8635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8636
8637 /* Check if we have space for header and length field:
8638 * - extension_type (2 bytes)
8639 * - extension_data_length (2 bytes)
8640 * - supported_signature_algorithms_length (2 bytes)
8641 */
8642 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8643 p += 6;
8644
8645 /*
8646 * Write supported_signature_algorithms
8647 */
8648 supported_sig_alg = p;
8649 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8650 if( sig_alg == NULL )
8651 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8652
8653 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8654 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8656 *sig_alg,
8657 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008658 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8659 continue;
8660 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8661 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8662 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008664 *sig_alg,
8665 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008666 }
8667
8668 /* Length of supported_signature_algorithms */
8669 supported_sig_alg_len = p - supported_sig_alg;
8670 if( supported_sig_alg_len == 0 )
8671 {
8672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8674 }
8675
XiaokangQianeaf36512022-04-24 09:07:44 +00008676 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008677 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008678 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8679
XiaokangQianeaf36512022-04-24 09:07:44 +00008680 *out_len = p - buf;
8681
8682#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8683 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8684#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8685 return( 0 );
8686}
8687#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8688
XiaokangQian40a35232022-05-07 09:02:40 +00008689#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008690/*
8691 * mbedtls_ssl_parse_server_name_ext
8692 *
8693 * Structure of server_name extension:
8694 *
8695 * enum {
8696 * host_name(0), (255)
8697 * } NameType;
8698 * opaque HostName<1..2^16-1>;
8699 *
8700 * struct {
8701 * NameType name_type;
8702 * select (name_type) {
8703 * case host_name: HostName;
8704 * } name;
8705 * } ServerName;
8706 * struct {
8707 * ServerName server_name_list<1..2^16-1>
8708 * } ServerNameList;
8709 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008710MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008711int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8712 const unsigned char *buf,
8713 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008714{
8715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8716 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008717 size_t server_name_list_len, hostname_len;
8718 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008719
XiaokangQianf2a94202022-05-20 06:44:24 +00008720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008721
8722 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008723 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008724 p += 2;
8725
XiaokangQian9b2b7712022-05-17 02:57:00 +00008726 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8727 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008728 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008729 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008730 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008731 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008732 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8733 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008734
8735 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8736 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008737 /* sni_name is intended to be used only during the parsing of the
8738 * ClientHello message (it is reset to NULL before the end of
8739 * the message parsing). Thus it is ok to just point to the
8740 * reception buffer and not make a copy of it.
8741 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008742 ssl->handshake->sni_name = p + 3;
8743 ssl->handshake->sni_name_len = hostname_len;
8744 if( ssl->conf->f_sni == NULL )
8745 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008746 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008747 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008748 if( ret != 0 )
8749 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008751 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8752 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008753 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8754 }
8755 return( 0 );
8756 }
8757
8758 p += hostname_len + 3;
8759 }
8760
8761 return( 0 );
8762}
8763#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8764
XiaokangQianacb39922022-06-17 10:18:48 +00008765#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008766MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008767int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8768 const unsigned char *buf,
8769 const unsigned char *end )
8770{
8771 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008772 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008773 const unsigned char *protocol_name_list;
8774 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008775 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008776
8777 /* If ALPN not configured, just ignore the extension */
8778 if( ssl->conf->alpn_list == NULL )
8779 return( 0 );
8780
8781 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008782 * RFC7301, section 3.1
8783 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008784 *
XiaokangQianc7403452022-06-23 03:24:12 +00008785 * struct {
8786 * ProtocolName protocol_name_list<2..2^16-1>
8787 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008788 */
8789
XiaokangQianc7403452022-06-23 03:24:12 +00008790 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008791 * protocol_name_list_len 2 bytes
8792 * protocol_name_len 1 bytes
8793 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008794 */
XiaokangQianacb39922022-06-17 10:18:48 +00008795 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8796
XiaokangQianc7403452022-06-23 03:24:12 +00008797 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008798 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008799 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008800 protocol_name_list = p;
8801 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008802
8803 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008804 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008805 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008806 protocol_name_len = *p++;
8807 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8808 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008809 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008810 {
8811 MBEDTLS_SSL_PEND_FATAL_ALERT(
8812 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8813 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008814 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008815 }
8816
8817 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008818 }
8819
8820 /* Use our order of preference */
8821 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8822 {
8823 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008824 p = protocol_name_list;
8825 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008826 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008827 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008828 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008829 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008830 {
8831 ssl->alpn_chosen = *alpn;
8832 return( 0 );
8833 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008834
8835 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008836 }
8837 }
8838
XiaokangQian95d5f542022-06-24 02:29:26 +00008839 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008840 MBEDTLS_SSL_PEND_FATAL_ALERT(
8841 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8842 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8843 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8844}
8845
8846int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8847 unsigned char *buf,
8848 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008849 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008850{
8851 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008852 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008853 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008854
8855 if( ssl->alpn_chosen == NULL )
8856 {
8857 return( 0 );
8858 }
8859
XiaokangQian95d5f542022-06-24 02:29:26 +00008860 protocol_name_len = strlen( ssl->alpn_chosen );
8861 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008862
8863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8864 /*
8865 * 0 . 1 ext identifier
8866 * 2 . 3 ext length
8867 * 4 . 5 protocol list length
8868 * 6 . 6 protocol name length
8869 * 7 . 7+n protocol name
8870 */
8871 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8872
XiaokangQian95d5f542022-06-24 02:29:26 +00008873 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008874
XiaokangQian95d5f542022-06-24 02:29:26 +00008875 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8876 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008877 /* Note: the length of the chosen protocol has been checked to be less
8878 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8879 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008880 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008881
XiaokangQian95d5f542022-06-24 02:29:26 +00008882 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008883 return ( 0 );
8884}
8885#endif /* MBEDTLS_SSL_ALPN */
8886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887#endif /* MBEDTLS_SSL_TLS_C */