blob: 35e9fecf5d96c9a931903b511dd4575cbacced1f [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 Qianbc663a02022-10-09 11:14:39 +0000300#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
301 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
302 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +0000303 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT && src->hostname != NULL )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000304 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000305 size_t hostname_len = strlen( src->hostname );
306 dst->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000307 if( dst->hostname == NULL )
308 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
309
Xiaokang Qian2f9efd32022-10-10 11:24:08 +0000310 strncpy( dst->hostname, src->hostname, hostname_len );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +0000311 }
312#endif
313
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314 return( 0 );
315}
316
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200318MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
320{
321 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
322 if( resized_buffer == NULL )
323 return -1;
324
325 /* We want to copy len_new bytes when downsizing the buffer, and
326 * len_old bytes when upsizing, so we choose the smaller of two sizes,
327 * to fit one buffer into another. Size checks, ensuring that no data is
328 * lost, are done outside of this function. */
329 memcpy( resized_buffer, *buffer,
330 ( len_new < *len_old ) ? len_new : *len_old );
331 mbedtls_platform_zeroize( *buffer, *len_old );
332 mbedtls_free( *buffer );
333
334 *buffer = resized_buffer;
335 *len_old = len_new;
336
337 return 0;
338}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200339
340static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500341 size_t in_buf_new_len,
342 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200343{
344 int modified = 0;
345 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
346 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
347 if( ssl->in_buf != NULL )
348 {
349 written_in = ssl->in_msg - ssl->in_buf;
350 iv_offset_in = ssl->in_iv - ssl->in_buf;
351 len_offset_in = ssl->in_len - ssl->in_buf;
352 if( downsizing ?
353 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
354 ssl->in_buf_len < in_buf_new_len )
355 {
356 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
357 {
358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
359 }
360 else
361 {
Paul Elliottb7449902021-03-10 18:14:58 +0000362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
363 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200364 modified = 1;
365 }
366 }
367 }
368
369 if( ssl->out_buf != NULL )
370 {
371 written_out = ssl->out_msg - ssl->out_buf;
372 iv_offset_out = ssl->out_iv - ssl->out_buf;
373 len_offset_out = ssl->out_len - ssl->out_buf;
374 if( downsizing ?
375 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
376 ssl->out_buf_len < out_buf_new_len )
377 {
378 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
379 {
380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
381 }
382 else
383 {
Paul Elliottb7449902021-03-10 18:14:58 +0000384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
385 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200386 modified = 1;
387 }
388 }
389 }
390 if( modified )
391 {
392 /* Update pointers here to avoid doing it twice. */
393 mbedtls_ssl_reset_in_out_pointers( ssl );
394 /* Fields below might not be properly updated with record
395 * splitting or with CID, so they are manually updated here. */
396 ssl->out_msg = ssl->out_buf + written_out;
397 ssl->out_len = ssl->out_buf + len_offset_out;
398 ssl->out_iv = ssl->out_buf + iv_offset_out;
399
400 ssl->in_msg = ssl->in_buf + written_in;
401 ssl->in_len = ssl->in_buf + len_offset_in;
402 ssl->in_iv = ssl->in_buf + iv_offset_in;
403 }
404}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500405#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
406
Jerry Yudb8c48a2022-01-27 14:54:54 +0800407#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800408
409#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
410typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
411 const char *label,
412 const unsigned char *random, size_t rlen,
413 unsigned char *dstbuf, size_t dlen );
414
415static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
416
417#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
418
419/* Type for the TLS PRF */
420typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
421 const unsigned char *, size_t,
422 unsigned char *, size_t);
423
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800425static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
426 int ciphersuite,
427 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200428#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800429 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200430#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800431 ssl_tls_prf_t tls_prf,
432 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400433 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800434 unsigned endpoint,
435 const mbedtls_ssl_context *ssl );
436
Andrzej Kurek25f27152022-08-17 16:09:31 -0400437#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200438MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800439static int tls_prf_sha256( const unsigned char *secret, size_t slen,
440 const char *label,
441 const unsigned char *random, size_t rlen,
442 unsigned char *dstbuf, size_t dlen );
443static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
444static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
445
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400446#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800447
Andrzej Kurek25f27152022-08-17 16:09:31 -0400448#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800450static int tls_prf_sha384( const unsigned char *secret, size_t slen,
451 const char *label,
452 const unsigned char *random, size_t rlen,
453 unsigned char *dstbuf, size_t dlen );
454
455static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
456static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400457#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800458
Jerry Yu438ddd82022-07-07 06:55:50 +0000459static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800460 unsigned char *buf,
461 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800462
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200463MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000464static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800465 const unsigned char *buf,
466 size_t len );
467#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
468
Jerry Yu53d23e22022-02-09 16:25:09 +0800469static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
470
Andrzej Kurek25f27152022-08-17 16:09:31 -0400471#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800472static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400473#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800474
Andrzej Kurek25f27152022-08-17 16:09:31 -0400475#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800476static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400477#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000478
Ron Eldor51d3ab52019-05-12 14:54:30 +0300479int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
480 const unsigned char *secret, size_t slen,
481 const char *label,
482 const unsigned char *random, size_t rlen,
483 unsigned char *dstbuf, size_t dlen )
484{
485 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
486
487 switch( prf )
488 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400490#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300491 case MBEDTLS_SSL_TLS_PRF_SHA384:
492 tls_prf = tls_prf_sha384;
493 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400494#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400495#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496 case MBEDTLS_SSL_TLS_PRF_SHA256:
497 tls_prf = tls_prf_sha256;
498 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400499#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300500#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300501 default:
502 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
503 }
504
505 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
506}
507
Jerry Yuc73c6182022-02-08 20:29:25 +0800508#if defined(MBEDTLS_X509_CRT_PARSE_C)
509static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
510{
511#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
512 if( session->peer_cert != NULL )
513 {
514 mbedtls_x509_crt_free( session->peer_cert );
515 mbedtls_free( session->peer_cert );
516 session->peer_cert = NULL;
517 }
518#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
519 if( session->peer_cert_digest != NULL )
520 {
521 /* Zeroization is not necessary. */
522 mbedtls_free( session->peer_cert_digest );
523 session->peer_cert_digest = NULL;
524 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
525 session->peer_cert_digest_len = 0;
526 }
527#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
528}
529#endif /* MBEDTLS_X509_CRT_PARSE_C */
530
531void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
532 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
533{
534 ((void) ciphersuite_info);
535
Andrzej Kurek25f27152022-08-17 16:09:31 -0400536#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800537 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
538 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
539 else
540#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400541#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800542 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
543 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
544 else
545#endif
546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
548 return;
549 }
550}
551
XiaokangQianadab9a62022-07-18 07:41:26 +0000552void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
553 unsigned hs_type,
554 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100555{
556 unsigned char hs_hdr[4];
557
558 /* Build HS header for checksum update. */
559 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
560 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
561 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
562 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
563
564 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
565}
566
567void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
568 unsigned hs_type,
569 unsigned char const *msg,
570 size_t msg_len )
571{
572 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
573 ssl->handshake->update_checksum( ssl, msg, msg_len );
574}
575
Jerry Yuc73c6182022-02-08 20:29:25 +0800576void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
577{
578 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400579#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800580#if defined(MBEDTLS_USE_PSA_CRYPTO)
581 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
582 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
583#else
584 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
585#endif
586#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400587#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800588#if defined(MBEDTLS_USE_PSA_CRYPTO)
589 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
590 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
591#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400592 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800593#endif
594#endif
595}
596
597static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
598 const unsigned char *buf, size_t len )
599{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800601#if defined(MBEDTLS_USE_PSA_CRYPTO)
602 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
603#else
604 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
605#endif
606#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400607#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800608#if defined(MBEDTLS_USE_PSA_CRYPTO)
609 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
610#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400611 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800612#endif
613#endif
614}
615
Andrzej Kurek25f27152022-08-17 16:09:31 -0400616#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800617static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
618 const unsigned char *buf, size_t len )
619{
620#if defined(MBEDTLS_USE_PSA_CRYPTO)
621 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
622#else
623 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
624#endif
625}
626#endif
627
Andrzej Kurek25f27152022-08-17 16:09:31 -0400628#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800629static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
630 const unsigned char *buf, size_t len )
631{
632#if defined(MBEDTLS_USE_PSA_CRYPTO)
633 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
634#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400635 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800636#endif
637}
638#endif
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200643
Andrzej Kurek25f27152022-08-17 16:09:31 -0400644#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500645#if defined(MBEDTLS_USE_PSA_CRYPTO)
646 handshake->fin_sha256_psa = psa_hash_operation_init();
647 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
648#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200650 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200651#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500652#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400653#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500654#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500655 handshake->fin_sha384_psa = psa_hash_operation_init();
656 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500657#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400658 mbedtls_sha512_init( &handshake->fin_sha384 );
659 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200660#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500661#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200662
663 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_DHM_C)
666 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200667#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200668#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200670#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200671#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200672 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200673#if defined(MBEDTLS_SSL_CLI_C)
674 handshake->ecjpake_cache = NULL;
675 handshake->ecjpake_cache_len = 0;
676#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200677#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200678
Gilles Peskineeccd8882020-03-10 12:19:08 +0100679#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200680 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200681#endif
682
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200683#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
684 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
685#endif
Hanno Becker75173122019-02-06 16:18:31 +0000686
687#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
688 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
689 mbedtls_pk_init( &handshake->peer_pubkey );
690#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200691}
692
Hanno Beckera18d1322018-01-03 14:27:32 +0000693void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200696
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100697#if defined(MBEDTLS_USE_PSA_CRYPTO)
698 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
699 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100700#else
701 mbedtls_cipher_init( &transform->cipher_ctx_enc );
702 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100703#endif
704
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000705#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100706#if defined(MBEDTLS_USE_PSA_CRYPTO)
707 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
708 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100709#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_init( &transform->md_ctx_enc );
711 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000712#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100713#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200714}
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200719}
720
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200721MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000723{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200724 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000725 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200727 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200729 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200730 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200731
732 /*
733 * Either the pointers are now NULL or cleared properly and can be freed.
734 * Now allocate missing structures.
735 */
736 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200737 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200738 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 }
Paul Bakker48916f92012-09-16 19:57:18 +0000740
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200741 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200742 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200743 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200744 }
Paul Bakker48916f92012-09-16 19:57:18 +0000745
Paul Bakker82788fb2014-10-20 13:59:19 +0200746 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200747 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200748 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200749 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500750#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
751 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400752
Andrzej Kurek4a063792020-10-21 15:08:44 +0200753 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
754 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500755#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000756
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200757 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000758 if( ssl->handshake == NULL ||
759 ssl->transform_negotiate == NULL ||
760 ssl->session_negotiate == NULL )
761 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_free( ssl->handshake );
765 mbedtls_free( ssl->transform_negotiate );
766 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200767
768 ssl->handshake = NULL;
769 ssl->transform_negotiate = NULL;
770 ssl->session_negotiate = NULL;
771
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200772 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000773 }
774
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200775 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000777 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200778 ssl_handshake_params_init( ssl->handshake );
779
Jerry Yud0766ec2022-09-22 10:46:57 +0800780#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
781 defined(MBEDTLS_SSL_SRV_C) && \
782 defined(MBEDTLS_SSL_SESSION_TICKETS)
783 ssl->handshake->new_session_tickets_count =
784 ssl->conf->new_session_tickets_count ;
785#endif
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
789 {
790 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200791
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
793 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
794 else
795 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200796
Hanno Becker0f57a652020-02-05 10:37:26 +0000797 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200798 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200799#endif
800
Brett Warrene0edc842021-08-17 09:53:13 +0100801/*
802 * curve_list is translated to IANA TLS group identifiers here because
803 * mbedtls_ssl_conf_curves returns void and so can't return
804 * any error codes.
805 */
806#if defined(MBEDTLS_ECP_C)
807#if !defined(MBEDTLS_DEPRECATED_REMOVED)
808 /* Heap allocate and translate curve_list from internal to IANA group ids */
809 if ( ssl->conf->curve_list != NULL )
810 {
811 size_t length;
812 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
813
814 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
815 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
816
817 /* Leave room for zero termination */
818 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
819 if ( group_list == NULL )
820 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
821
822 for( size_t i = 0; i < length; i++ )
823 {
824 const mbedtls_ecp_curve_info *info =
825 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
826 if ( info == NULL )
827 {
828 mbedtls_free( group_list );
829 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
830 }
831 group_list[i] = info->tls_id;
832 }
833
834 group_list[length] = 0;
835
836 ssl->handshake->group_list = group_list;
837 ssl->handshake->group_list_heap_allocated = 1;
838 }
839 else
840 {
841 ssl->handshake->group_list = ssl->conf->group_list;
842 ssl->handshake->group_list_heap_allocated = 0;
843 }
844#endif /* MBEDTLS_DEPRECATED_REMOVED */
845#endif /* MBEDTLS_ECP_C */
846
Jerry Yuf017ee42022-01-12 15:49:48 +0800847#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
848#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800849#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800850 /* Heap allocate and translate sig_hashes from internal hash identifiers to
851 signature algorithms IANA identifiers. */
852 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800853 ssl->conf->sig_hashes != NULL )
854 {
855 const int *md;
856 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800857 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800858 uint16_t *p;
859
Jerry Yub476a442022-01-21 18:14:45 +0800860#if defined(static_assert)
861 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
862 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
863 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800864#endif
865
Jerry Yuf017ee42022-01-12 15:49:48 +0800866 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
867 {
868 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
869 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800870#if defined(MBEDTLS_ECDSA_C)
871 sig_algs_len += sizeof( uint16_t );
872#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800873
Jerry Yub476a442022-01-21 18:14:45 +0800874#if defined(MBEDTLS_RSA_C)
875 sig_algs_len += sizeof( uint16_t );
876#endif
877 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
878 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800879 }
880
Jerry Yub476a442022-01-21 18:14:45 +0800881 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800882 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
883
Jerry Yub476a442022-01-21 18:14:45 +0800884 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
885 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 if( ssl->handshake->sig_algs == NULL )
887 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
888
889 p = (uint16_t *)ssl->handshake->sig_algs;
890 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
891 {
892 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
893 if( hash == MBEDTLS_SSL_HASH_NONE )
894 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800895#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800896 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
897 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800898#endif
899#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
901 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800902#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800903 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200904 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800905 ssl->handshake->sig_algs_heap_allocated = 1;
906 }
907 else
Jerry Yua69269a2022-01-17 21:06:01 +0800908#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800910 ssl->handshake->sig_algs_heap_allocated = 0;
911 }
Jerry Yucc539102022-06-27 16:27:35 +0800912#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yuf017ee42022-01-12 15:49:48 +0800913#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000914 return( 0 );
915}
916
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200917#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200918/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200919MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200920static int ssl_cookie_write_dummy( void *ctx,
921 unsigned char **p, unsigned char *end,
922 const unsigned char *cli_id, size_t cli_id_len )
923{
924 ((void) ctx);
925 ((void) p);
926 ((void) end);
927 ((void) cli_id);
928 ((void) cli_id_len);
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200931}
932
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200933MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200934static int ssl_cookie_check_dummy( void *ctx,
935 const unsigned char *cookie, size_t cookie_len,
936 const unsigned char *cli_id, size_t cli_id_len )
937{
938 ((void) ctx);
939 ((void) cookie);
940 ((void) cookie_len);
941 ((void) cli_id);
942 ((void) cli_id_len);
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200945}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200946#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200947
Paul Bakker5121ce52009-01-03 21:22:43 +0000948/*
949 * Initialize an SSL context
950 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200951void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
952{
953 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
954}
955
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200956MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800957static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
958{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100959 const mbedtls_ssl_config *conf = ssl->conf;
960
Ronald Cron6f135e12021-12-08 16:57:54 +0100961#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100962 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
963 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000964 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
965 {
966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
967 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
968 }
969
Jerry Yu60835a82021-08-04 10:13:52 +0800970 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
971 return( 0 );
972 }
973#endif
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100976 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800977 {
978 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
979 return( 0 );
980 }
981#endif
982
Ronald Cron6f135e12021-12-08 16:57:54 +0100983#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100984 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800985 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
987 {
988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
989 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
990 }
991
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000992 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
993 {
994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
995 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
996 }
997
998
Ronald Crone1d3f062022-02-10 14:50:54 +0100999 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1000 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001001 }
1002#endif
1003
1004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1005 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1006}
1007
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001008MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001009static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1010{
1011 int ret;
1012 ret = ssl_conf_version_check( ssl );
1013 if( ret != 0 )
1014 return( ret );
1015
1016 /* Space for further checks */
1017
1018 return( 0 );
1019}
1020
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001021/*
1022 * Setup an SSL context
1023 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001024
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001025int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001026 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001027{
Janos Follath865b3eb2019-12-16 11:46:15 +00001028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001029 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1030 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001032 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001033
Jerry Yu60835a82021-08-04 10:13:52 +08001034 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1035 return( ret );
1036
Paul Bakker62f2dee2012-09-28 07:31:51 +00001037 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001038 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001039 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001040
1041 /* Set to NULL in case of an error condition */
1042 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001043
Darryl Greenb33cc762019-11-28 14:29:44 +00001044#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1045 ssl->in_buf_len = in_buf_len;
1046#endif
1047 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001048 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001051 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001052 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001053 }
1054
Darryl Greenb33cc762019-11-28 14:29:44 +00001055#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1056 ssl->out_buf_len = out_buf_len;
1057#endif
1058 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001059 if( ssl->out_buf == NULL )
1060 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001062 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001063 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 }
1065
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001066 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001067
Johan Pascalb62bb512015-12-03 21:56:45 +01001068#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001069 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001070#endif
1071
Paul Bakker48916f92012-09-16 19:57:18 +00001072 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001073 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
1075 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001076
1077error:
1078 mbedtls_free( ssl->in_buf );
1079 mbedtls_free( ssl->out_buf );
1080
1081 ssl->conf = NULL;
1082
Darryl Greenb33cc762019-11-28 14:29:44 +00001083#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1084 ssl->in_buf_len = 0;
1085 ssl->out_buf_len = 0;
1086#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001087 ssl->in_buf = NULL;
1088 ssl->out_buf = NULL;
1089
1090 ssl->in_hdr = NULL;
1091 ssl->in_ctr = NULL;
1092 ssl->in_len = NULL;
1093 ssl->in_iv = NULL;
1094 ssl->in_msg = NULL;
1095
1096 ssl->out_hdr = NULL;
1097 ssl->out_ctr = NULL;
1098 ssl->out_len = NULL;
1099 ssl->out_iv = NULL;
1100 ssl->out_msg = NULL;
1101
k-stachowiak9f7798e2018-07-31 16:52:32 +02001102 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103}
1104
1105/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001106 * Reset an initialized and used SSL context for re-use while retaining
1107 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001108 *
1109 * If partial is non-zero, keep data in the input buffer and client ID.
1110 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001111 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001112void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1113 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001114{
Darryl Greenb33cc762019-11-28 14:29:44 +00001115#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1116 size_t in_buf_len = ssl->in_buf_len;
1117 size_t out_buf_len = ssl->out_buf_len;
1118#else
1119 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1120 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1121#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001122
Hanno Beckerb0302c42021-08-03 09:39:42 +01001123#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1124 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001125#endif
1126
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001127 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001128 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001129
Hanno Beckerb0302c42021-08-03 09:39:42 +01001130 mbedtls_ssl_reset_in_out_pointers( ssl );
1131
1132 /* Reset incoming message parsing */
1133 ssl->in_offt = NULL;
1134 ssl->nb_zero = 0;
1135 ssl->in_msgtype = 0;
1136 ssl->in_msglen = 0;
1137 ssl->in_hslen = 0;
1138 ssl->keep_current_message = 0;
1139 ssl->transform_in = NULL;
1140
1141#if defined(MBEDTLS_SSL_PROTO_DTLS)
1142 ssl->next_record_offset = 0;
1143 ssl->in_epoch = 0;
1144#endif
1145
1146 /* Keep current datagram if partial == 1 */
1147 if( partial == 0 )
1148 {
1149 ssl->in_left = 0;
1150 memset( ssl->in_buf, 0, in_buf_len );
1151 }
1152
Ronald Cronad8c17b2022-06-10 17:18:09 +02001153 ssl->send_alert = 0;
1154
Hanno Beckerb0302c42021-08-03 09:39:42 +01001155 /* Reset outgoing message writing */
1156 ssl->out_msgtype = 0;
1157 ssl->out_msglen = 0;
1158 ssl->out_left = 0;
1159 memset( ssl->out_buf, 0, out_buf_len );
1160 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1161 ssl->transform_out = NULL;
1162
1163#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1164 mbedtls_ssl_dtls_replay_reset( ssl );
1165#endif
1166
XiaokangQian2b01dc32022-01-21 02:53:13 +00001167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001168 if( ssl->transform )
1169 {
1170 mbedtls_ssl_transform_free( ssl->transform );
1171 mbedtls_free( ssl->transform );
1172 ssl->transform = NULL;
1173 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001174#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1175
XiaokangQian2b01dc32022-01-21 02:53:13 +00001176#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1177 mbedtls_ssl_transform_free( ssl->transform_application );
1178 mbedtls_free( ssl->transform_application );
1179 ssl->transform_application = NULL;
1180
1181 if( ssl->handshake != NULL )
1182 {
1183 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1184 mbedtls_free( ssl->handshake->transform_earlydata );
1185 ssl->handshake->transform_earlydata = NULL;
1186
1187 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1188 mbedtls_free( ssl->handshake->transform_handshake );
1189 ssl->handshake->transform_handshake = NULL;
1190 }
1191
XiaokangQian2b01dc32022-01-21 02:53:13 +00001192#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001193}
1194
1195int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1196{
1197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1198
1199 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1200
XiaokangQian78b1fa72022-01-19 06:56:30 +00001201 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001202
1203 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_RENEGOTIATION)
1205 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001206 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001207
1208 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1210 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001213
Hanno Beckerb0302c42021-08-03 09:39:42 +01001214 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001215 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001216 if( ssl->session )
1217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_ssl_session_free( ssl->session );
1219 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001220 ssl->session = NULL;
1221 }
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001224 ssl->alpn_chosen = NULL;
1225#endif
1226
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001227#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01001228#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001229 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01001230#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001231 {
1232 mbedtls_free( ssl->cli_id );
1233 ssl->cli_id = NULL;
1234 ssl->cli_id_len = 0;
1235 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001236#endif
1237
Paul Bakker48916f92012-09-16 19:57:18 +00001238 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1239 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001240
1241 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001242}
1243
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001244/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001245 * Reset an initialized and used SSL context for re-use while retaining
1246 * all application-set variables, function pointers and data.
1247 */
1248int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1249{
Hanno Becker43aefe22020-02-05 10:44:56 +00001250 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001251}
1252
1253/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 * SSL set accessors
1255 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001256void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001258 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259}
1260
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001261void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001263 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001264}
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001267void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001268{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001269 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001270}
1271#endif
1272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001273void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001275 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001276}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001279
Hanno Becker1841b0a2018-08-24 11:13:57 +01001280void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1281 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001282{
1283 ssl->disable_datagram_packing = !allow_packing;
1284}
1285
1286void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1287 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001289 conf->hs_timeout_min = min;
1290 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001291}
1292#endif
1293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001294void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001296 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001297}
1298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001300void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001301 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001302 void *p_vrfy )
1303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001304 conf->f_vrfy = f_vrfy;
1305 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001308
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001309void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001310 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 void *p_rng )
1312{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001313 conf->f_rng = f_rng;
1314 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001315}
1316
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001317void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001318 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 void *p_dbg )
1320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001321 conf->f_dbg = f_dbg;
1322 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001323}
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001326 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001327 mbedtls_ssl_send_t *f_send,
1328 mbedtls_ssl_recv_t *f_recv,
1329 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001330{
1331 ssl->p_bio = p_bio;
1332 ssl->f_send = f_send;
1333 ssl->f_recv = f_recv;
1334 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001335}
1336
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001337#if defined(MBEDTLS_SSL_PROTO_DTLS)
1338void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1339{
1340 ssl->mtu = mtu;
1341}
1342#endif
1343
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001344void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001345{
1346 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001347}
1348
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001349void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1350 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001351 mbedtls_ssl_set_timer_t *f_set_timer,
1352 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001353{
1354 ssl->p_timer = p_timer;
1355 ssl->f_set_timer = f_set_timer;
1356 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001357
1358 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001359 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001360}
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001363void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001364 void *p_cache,
1365 mbedtls_ssl_cache_get_t *f_get_cache,
1366 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001368 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001369 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001370 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_CLI_C)
1375int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376{
Janos Follath865b3eb2019-12-16 11:46:15 +00001377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001378
1379 if( ssl == NULL ||
1380 session == NULL ||
1381 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001382 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001385 }
1386
Hanno Beckere810bbc2021-05-14 16:01:05 +01001387 if( ssl->handshake->resume == 1 )
1388 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1389
Jerry Yu21092062022-10-10 21:21:31 +08001390#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1391 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001392 {
Jerry Yu21092062022-10-10 21:21:31 +08001393 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1394 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1395
1396 if( mbedtls_ssl_validate_ciphersuite(
1397 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1398 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1399 {
1400 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1401 session->ciphersuite ) );
1402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1403 }
Jerry Yu40afab62022-10-08 10:42:13 +08001404 }
Jerry Yu21092062022-10-10 21:21:31 +08001405#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001406
Hanno Becker52055ae2019-02-06 14:30:46 +00001407 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1408 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001409 return( ret );
1410
Paul Bakker0a597072012-09-25 21:55:46 +00001411 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001412
1413 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001417void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1418 const int *ciphersuites )
1419{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001420 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001421}
1422
Ronald Cron6f135e12021-12-08 16:57:54 +01001423#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001424void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001425 const int kex_modes )
1426{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001427 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001428}
Ronald Cron6f135e12021-12-08 16:57:54 +01001429#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001432void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001433 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001434{
1435 conf->cert_profile = profile;
1436}
1437
Glenn Strauss36872db2022-01-22 05:06:31 -05001438static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1439{
1440 mbedtls_ssl_key_cert *cur = key_cert, *next;
1441
1442 while( cur != NULL )
1443 {
1444 next = cur->next;
1445 mbedtls_free( cur );
1446 cur = next;
1447 }
1448}
1449
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001450/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001451MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001452static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1453 mbedtls_x509_crt *cert,
1454 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001455{
niisato8ee24222018-06-25 19:05:48 +09001456 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001457
Glenn Strauss36872db2022-01-22 05:06:31 -05001458 if( cert == NULL )
1459 {
1460 /* Free list if cert is null */
1461 ssl_key_cert_free( *head );
1462 *head = NULL;
1463 return( 0 );
1464 }
1465
niisato8ee24222018-06-25 19:05:48 +09001466 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1467 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001468 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001469
niisato8ee24222018-06-25 19:05:48 +09001470 new_cert->cert = cert;
1471 new_cert->key = key;
1472 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001473
Glenn Strauss36872db2022-01-22 05:06:31 -05001474 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001475 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001476 {
niisato8ee24222018-06-25 19:05:48 +09001477 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001478 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001479 else
1480 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001481 mbedtls_ssl_key_cert *cur = *head;
1482 while( cur->next != NULL )
1483 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001484 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001485 }
1486
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001487 return( 0 );
1488}
1489
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001490int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001491 mbedtls_x509_crt *own_cert,
1492 mbedtls_pk_context *pk_key )
1493{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001494 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001495}
1496
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001497void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001498 mbedtls_x509_crt *ca_chain,
1499 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001500{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001501 conf->ca_chain = ca_chain;
1502 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001503
1504#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1505 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1506 * cannot be used together. */
1507 conf->f_ca_cb = NULL;
1508 conf->p_ca_cb = NULL;
1509#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001510}
Hanno Becker5adaad92019-03-27 16:54:37 +00001511
1512#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1513void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001514 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001515 void *p_ca_cb )
1516{
1517 conf->f_ca_cb = f_ca_cb;
1518 conf->p_ca_cb = p_ca_cb;
1519
1520 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1521 * cannot be used together. */
1522 conf->ca_chain = NULL;
1523 conf->ca_crl = NULL;
1524}
1525#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001527
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001528#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001529const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1530 size_t *name_len )
1531{
1532 *name_len = ssl->handshake->sni_name_len;
1533 return( ssl->handshake->sni_name );
1534}
1535
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001536int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1537 mbedtls_x509_crt *own_cert,
1538 mbedtls_pk_context *pk_key )
1539{
1540 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1541 own_cert, pk_key ) );
1542}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001543
1544void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1545 mbedtls_x509_crt *ca_chain,
1546 mbedtls_x509_crl *ca_crl )
1547{
1548 ssl->handshake->sni_ca_chain = ca_chain;
1549 ssl->handshake->sni_ca_crl = ca_crl;
1550}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001551
Glenn Strauss999ef702022-03-11 01:37:23 -05001552#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1553void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1554 const mbedtls_x509_crt *crt)
1555{
1556 ssl->handshake->dn_hints = crt;
1557}
1558#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1559
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001560void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1561 int authmode )
1562{
1563 ssl->handshake->sni_authmode = authmode;
1564}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001565#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1566
Hanno Becker8927c832019-04-03 12:52:50 +01001567#if defined(MBEDTLS_X509_CRT_PARSE_C)
1568void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1569 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1570 void *p_vrfy )
1571{
1572 ssl->f_vrfy = f_vrfy;
1573 ssl->p_vrfy = p_vrfy;
1574}
1575#endif
1576
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001577#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001578/*
1579 * Set EC J-PAKE password for current handshake
1580 */
1581int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1582 const unsigned char *pw,
1583 size_t pw_len )
1584{
1585 mbedtls_ecjpake_role role;
1586
Janos Follath8eb64132016-06-03 15:40:57 +01001587 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1589
1590 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1591 role = MBEDTLS_ECJPAKE_SERVER;
1592 else
1593 role = MBEDTLS_ECJPAKE_CLIENT;
1594
1595 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1596 role,
1597 MBEDTLS_MD_SHA256,
1598 MBEDTLS_ECP_DP_SECP256R1,
1599 pw, pw_len ) );
1600}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001601#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001602
Gilles Peskineeccd8882020-03-10 12:19:08 +01001603#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001604
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001605MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001606static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
1607{
1608#if defined(MBEDTLS_USE_PSA_CRYPTO)
1609 if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
1610 return( 1 );
1611#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001612 if( conf->psk != NULL )
1613 return( 1 );
1614
1615 return( 0 );
1616}
1617
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001618static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1619{
1620 /* Remove reference to existing PSK, if any. */
1621#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001623 {
1624 /* The maintenance of the PSK key slot is the
1625 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001626 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001627 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001628#endif /* MBEDTLS_USE_PSA_CRYPTO */
1629 if( conf->psk != NULL )
1630 {
1631 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1632
1633 mbedtls_free( conf->psk );
1634 conf->psk = NULL;
1635 conf->psk_len = 0;
1636 }
1637
1638 /* Remove reference to PSK identity, if any. */
1639 if( conf->psk_identity != NULL )
1640 {
1641 mbedtls_free( conf->psk_identity );
1642 conf->psk_identity = NULL;
1643 conf->psk_identity_len = 0;
1644 }
1645}
1646
Hanno Becker7390c712018-11-15 13:33:04 +00001647/* This function assumes that PSK identity in the SSL config is unset.
1648 * It checks that the provided identity is well-formed and attempts
1649 * to make a copy of it in the SSL config.
1650 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001651MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001652static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1653 unsigned char const *psk_identity,
1654 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001655{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001656 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001657 if( psk_identity == NULL ||
1658 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001659 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001660 {
1661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1662 }
1663
Hanno Becker7390c712018-11-15 13:33:04 +00001664 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1665 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001666 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001667
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001668 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001669 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001670
1671 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001672}
1673
Hanno Becker7390c712018-11-15 13:33:04 +00001674int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1675 const unsigned char *psk, size_t psk_len,
1676 const unsigned char *psk_identity, size_t psk_identity_len )
1677{
Janos Follath865b3eb2019-12-16 11:46:15 +00001678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001679
1680 /* We currently only support one PSK, raw or opaque. */
1681 if( ssl_conf_psk_is_configured( conf ) )
1682 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001683
1684 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001685 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001687 if( psk_len == 0 )
1688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1689 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1691
Hanno Becker7390c712018-11-15 13:33:04 +00001692 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1693 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1694 conf->psk_len = psk_len;
1695 memcpy( conf->psk, psk, conf->psk_len );
1696
1697 /* Check and set PSK Identity */
1698 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1699 if( ret != 0 )
1700 ssl_conf_remove_psk( conf );
1701
1702 return( ret );
1703}
1704
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001705static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1706{
1707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001708 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001709 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001710 /* The maintenance of the external PSK key slot is the
1711 * user's responsibility. */
1712 if( ssl->handshake->psk_opaque_is_internal )
1713 {
1714 psa_destroy_key( ssl->handshake->psk_opaque );
1715 ssl->handshake->psk_opaque_is_internal = 0;
1716 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001717 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001718 }
Neil Armstronge952a302022-05-03 10:22:14 +02001719#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001720 if( ssl->handshake->psk != NULL )
1721 {
1722 mbedtls_platform_zeroize( ssl->handshake->psk,
1723 ssl->handshake->psk_len );
1724 mbedtls_free( ssl->handshake->psk );
1725 ssl->handshake->psk_len = 0;
1726 }
Neil Armstronge952a302022-05-03 10:22:14 +02001727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001728}
1729
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001730int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1731 const unsigned char *psk, size_t psk_len )
1732{
Neil Armstrong501c9322022-05-03 09:35:09 +02001733#if defined(MBEDTLS_USE_PSA_CRYPTO)
1734 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001736 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001738#endif /* MBEDTLS_USE_PSA_CRYPTO */
1739
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001740 if( psk == NULL || ssl->handshake == NULL )
1741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1742
1743 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1745
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001746 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001747
Neil Armstrong501c9322022-05-03 09:35:09 +02001748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001749#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1750 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1751 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001752 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001753 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1754 else
1755 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1756 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1757 }
1758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001759
Jerry Yu568ec252022-07-22 21:27:34 +08001760#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001761 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1762 {
1763 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1764 psa_set_key_usage_flags( &key_attributes,
1765 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1766 }
1767#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1768
Neil Armstrong501c9322022-05-03 09:35:09 +02001769 psa_set_key_algorithm( &key_attributes, alg );
1770 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1771
1772 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1773 if( status != PSA_SUCCESS )
1774 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1775
1776 /* Allow calling psa_destroy_key() on psk remove */
1777 ssl->handshake->psk_opaque_is_internal = 1;
1778 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1779#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001780 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001781 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001782
1783 ssl->handshake->psk_len = psk_len;
1784 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1785
1786 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001787#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001788}
1789
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001790#if defined(MBEDTLS_USE_PSA_CRYPTO)
1791int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001792 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001793 const unsigned char *psk_identity,
1794 size_t psk_identity_len )
1795{
Janos Follath865b3eb2019-12-16 11:46:15 +00001796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001797
1798 /* We currently only support one PSK, raw or opaque. */
1799 if( ssl_conf_psk_is_configured( conf ) )
1800 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001801
Hanno Becker7390c712018-11-15 13:33:04 +00001802 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001803 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001805 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001806
1807 /* Check and set PSK Identity */
1808 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1809 psk_identity_len );
1810 if( ret != 0 )
1811 ssl_conf_remove_psk( conf );
1812
1813 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001814}
1815
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001816int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1817 mbedtls_svc_key_id_t psk )
1818{
1819 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1820 ( ssl->handshake == NULL ) )
1821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1822
1823 ssl_remove_psk( ssl );
1824 ssl->handshake->psk_opaque = psk;
1825 return( 0 );
1826}
1827#endif /* MBEDTLS_USE_PSA_CRYPTO */
1828
Jerry Yu8897c072022-08-12 13:56:53 +08001829#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001830void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1831 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1832 size_t),
1833 void *p_psk )
1834{
1835 conf->f_psk = f_psk;
1836 conf->p_psk = p_psk;
1837}
Jerry Yu8897c072022-08-12 13:56:53 +08001838#endif /* MBEDTLS_SSL_SRV_C */
1839
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001840#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1841
1842#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001843static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1844 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001845{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001846#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001847 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001848 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001849#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001850 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001851 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001852 return( MBEDTLS_SSL_MODE_STREAM );
1853}
1854
1855#else /* MBEDTLS_USE_PSA_CRYPTO */
1856
1857static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1858 mbedtls_cipher_mode_t mode )
1859{
1860#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1861 if( mode == MBEDTLS_MODE_CBC )
1862 return( MBEDTLS_SSL_MODE_CBC );
1863#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1864
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001865#if defined(MBEDTLS_GCM_C) || \
1866 defined(MBEDTLS_CCM_C) || \
1867 defined(MBEDTLS_CHACHAPOLY_C)
1868 if( mode == MBEDTLS_MODE_GCM ||
1869 mode == MBEDTLS_MODE_CCM ||
1870 mode == MBEDTLS_MODE_CHACHAPOLY )
1871 return( MBEDTLS_SSL_MODE_AEAD );
1872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001873
1874 return( MBEDTLS_SSL_MODE_STREAM );
1875}
Gilles Peskine301711e2022-04-26 16:57:05 +02001876#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001877
Gilles Peskinee108d982022-04-26 16:50:40 +02001878static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1879 mbedtls_ssl_mode_t base_mode,
1880 int encrypt_then_mac )
1881{
1882#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1883 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1884 base_mode == MBEDTLS_SSL_MODE_CBC )
1885 {
1886 return( MBEDTLS_SSL_MODE_CBC_ETM );
1887 }
1888#else
1889 (void) encrypt_then_mac;
1890#endif
1891 return( base_mode );
1892}
1893
Neil Armstrongab555e02022-04-04 11:07:59 +02001894mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001895 const mbedtls_ssl_transform *transform )
1896{
Gilles Peskinee108d982022-04-26 16:50:40 +02001897 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02001899 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001900#else
Gilles Peskinee108d982022-04-26 16:50:40 +02001901 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
1902#endif
1903 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001904
Gilles Peskinee108d982022-04-26 16:50:40 +02001905 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001906#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02001907 encrypt_then_mac = transform->encrypt_then_mac;
1908#endif
1909 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001910}
1911
Neil Armstrongab555e02022-04-04 11:07:59 +02001912mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001913#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001914 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02001915#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001916 const mbedtls_ssl_ciphersuite_t *suite )
1917{
Gilles Peskinee108d982022-04-26 16:50:40 +02001918 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
1919
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001920#if defined(MBEDTLS_USE_PSA_CRYPTO)
1921 psa_status_t status;
1922 psa_algorithm_t alg;
1923 psa_key_type_t type;
1924 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001925 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
1926 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02001927 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001928#else
1929 const mbedtls_cipher_info_t *cipher =
1930 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001931 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02001932 {
1933 base_mode =
1934 mbedtls_ssl_get_base_mode(
1935 mbedtls_cipher_info_get_mode( cipher ) );
1936 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001937#endif /* MBEDTLS_USE_PSA_CRYPTO */
1938
Gilles Peskinee108d982022-04-26 16:50:40 +02001939#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1940 int encrypt_then_mac = 0;
1941#endif
1942 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001943}
1944
Neil Armstrong8395d7a2022-05-18 11:44:56 +02001945#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08001946
1947#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00001948/* Serialization of TLS 1.3 sessions:
1949 *
1950 * struct {
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00001951 * opaque hostname<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001952 * uint64 ticket_received;
1953 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08001954 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00001955 * } ClientOnlyData;
1956 *
1957 * struct {
1958 * uint8 endpoint;
1959 * uint8 ciphersuite[2];
1960 * uint32 ticket_age_add;
1961 * uint8 ticket_flags;
1962 * opaque resumption_key<0..255>;
1963 * select ( endpoint ) {
1964 * case client: ClientOnlyData;
1965 * case server: uint64 start_time;
1966 * };
1967 * } serialized_session_tls13;
1968 *
1969 */
1970#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08001971MBEDTLS_CHECK_RETURN_CRITICAL
1972static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
1973 unsigned char *buf,
1974 size_t buf_len,
1975 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00001976{
1977 unsigned char *p = buf;
Jerry Yubc7c1a42022-07-21 22:57:37 +08001978 size_t needed = 1 /* endpoint */
1979 + 2 /* ciphersuite */
1980 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08001981 + 1 /* ticket_flags */
1982 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08001983 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08001984
1985 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
1986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1987 needed += session->resumption_key_len; /* resumption_key */
1988
Jerry Yu438ddd82022-07-07 06:55:50 +00001989#if defined(MBEDTLS_HAVE_TIME)
1990 needed += 8; /* start_time or ticket_received */
1991#endif
1992
1993#if defined(MBEDTLS_SSL_CLI_C)
1994 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
1995 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00001996 size_t hostname_len = session->hostname == NULL?0:strlen( session->hostname );
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00001997#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00001998 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00001999 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002000#endif
2001
Jerry Yu438ddd82022-07-07 06:55:50 +00002002 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002003 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002004
2005 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002006 if( session->ticket_len > SIZE_MAX - needed )
2007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002008
Jerry Yue28d9742022-08-18 15:44:03 +08002009 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002010 }
2011#endif /* MBEDTLS_SSL_CLI_C */
2012
Jerry Yue36fdd62022-08-17 21:31:36 +08002013 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002014 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002015 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002016
2017 p[0] = session->endpoint;
2018 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2019 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2020 p[7] = session->ticket_flags;
2021
2022 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002023 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002024 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002025 memcpy( p, session->resumption_key, session->resumption_key_len );
2026 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002027
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002028#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002029 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002030 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002031 size_t hostname_len = session->hostname == NULL?0:strlen(session->hostname);
2032 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002033 p += 2;
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002034 if ( hostname_len > 0 &&
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002035 session->hostname != NULL )
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002036 {
2037 /* save host name */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002038 memcpy( p, session->hostname, hostname_len );
2039 p += hostname_len;
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002040 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002041 }
2042#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2043
Jerry Yu438ddd82022-07-07 06:55:50 +00002044#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2045 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2046 {
2047 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2048 p += 8;
2049 }
2050#endif /* MBEDTLS_HAVE_TIME */
2051
2052#if defined(MBEDTLS_SSL_CLI_C)
2053 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2054 {
2055#if defined(MBEDTLS_HAVE_TIME)
2056 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2057 p += 8;
2058#endif
2059 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2060 p += 4;
2061
2062 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2063 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002064
2065 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002066 {
2067 memcpy( p, session->ticket, session->ticket_len );
2068 p += session->ticket_len;
2069 }
2070 }
2071#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002072 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002073}
2074
2075MBEDTLS_CHECK_RETURN_CRITICAL
2076static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2077 const unsigned char *buf,
2078 size_t len )
2079{
2080 const unsigned char *p = buf;
2081 const unsigned char *end = buf + len;
2082
2083 if( end - p < 9 )
2084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2085 session->endpoint = p[0];
2086 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2087 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2088 session->ticket_flags = p[7];
2089
2090 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002091 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002092 p += 9;
2093
Jerry Yubc7c1a42022-07-21 22:57:37 +08002094 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2096
Jerry Yubc7c1a42022-07-21 22:57:37 +08002097 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002099 memcpy( session->resumption_key, p, session->resumption_key_len );
2100 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002101
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_SSL_CLI_C)
2103 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2104 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002105 size_t hostname_len;
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002106 /* load host name */
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002107 if( end - p < 2 )
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002109 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0);
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002110 p += 2;
Xiaokang Qian6af2a6d2022-10-08 10:50:19 +00002111
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002112 if( end - p < ( long int )hostname_len )
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002114 if( hostname_len > 0 )
Xiaokang Qianecd75282022-09-28 07:11:02 +00002115 {
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002116 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Xiaokang Qianecd75282022-09-28 07:11:02 +00002117 if( session->hostname == NULL )
2118 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002119 memcpy( session->hostname, p, hostname_len );
2120 p += hostname_len;
Xiaokang Qianecd75282022-09-28 07:11:02 +00002121 }
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002122 }
2123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && MBEDTLS_SSL_CLI_C */
2124
Jerry Yu438ddd82022-07-07 06:55:50 +00002125#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2126 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2127 {
2128 if( end - p < 8 )
2129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2130 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2131 p += 8;
2132 }
2133#endif /* MBEDTLS_HAVE_TIME */
2134
2135#if defined(MBEDTLS_SSL_CLI_C)
2136 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2137 {
2138#if defined(MBEDTLS_HAVE_TIME)
2139 if( end - p < 8 )
2140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2141 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2142 p += 8;
2143#endif
2144 if( end - p < 4 )
2145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2146 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2147 p += 4;
2148
2149 if( end - p < 2 )
2150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2151 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2152 p += 2;
2153
2154 if( end - p < ( long int )session->ticket_len )
2155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2156 if( session->ticket_len > 0 )
2157 {
2158 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2159 if( session->ticket == NULL )
2160 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2161 memcpy( session->ticket, p, session->ticket_len );
2162 p += session->ticket_len;
2163 }
2164 }
2165#endif /* MBEDTLS_SSL_CLI_C */
2166
2167 return( 0 );
2168
2169}
2170#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002171MBEDTLS_CHECK_RETURN_CRITICAL
2172static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2173 unsigned char *buf,
2174 size_t buf_len,
2175 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002176{
2177 ((void) session);
2178 ((void) buf);
2179 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002180 *olen = 0;
2181 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002182}
Jerry Yu438ddd82022-07-07 06:55:50 +00002183
2184static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2185 unsigned char *buf,
2186 size_t buf_len )
2187{
2188 ((void) session);
2189 ((void) buf);
2190 ((void) buf_len);
2191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2192}
2193#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002194#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2195
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002196psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002197 size_t taglen,
2198 psa_algorithm_t *alg,
2199 psa_key_type_t *key_type,
2200 size_t *key_size )
2201{
2202 switch ( mbedtls_cipher_type )
2203 {
2204 case MBEDTLS_CIPHER_AES_128_CBC:
2205 *alg = PSA_ALG_CBC_NO_PADDING;
2206 *key_type = PSA_KEY_TYPE_AES;
2207 *key_size = 128;
2208 break;
2209 case MBEDTLS_CIPHER_AES_128_CCM:
2210 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2211 *key_type = PSA_KEY_TYPE_AES;
2212 *key_size = 128;
2213 break;
2214 case MBEDTLS_CIPHER_AES_128_GCM:
2215 *alg = PSA_ALG_GCM;
2216 *key_type = PSA_KEY_TYPE_AES;
2217 *key_size = 128;
2218 break;
2219 case MBEDTLS_CIPHER_AES_192_CCM:
2220 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2221 *key_type = PSA_KEY_TYPE_AES;
2222 *key_size = 192;
2223 break;
2224 case MBEDTLS_CIPHER_AES_192_GCM:
2225 *alg = PSA_ALG_GCM;
2226 *key_type = PSA_KEY_TYPE_AES;
2227 *key_size = 192;
2228 break;
2229 case MBEDTLS_CIPHER_AES_256_CBC:
2230 *alg = PSA_ALG_CBC_NO_PADDING;
2231 *key_type = PSA_KEY_TYPE_AES;
2232 *key_size = 256;
2233 break;
2234 case MBEDTLS_CIPHER_AES_256_CCM:
2235 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2236 *key_type = PSA_KEY_TYPE_AES;
2237 *key_size = 256;
2238 break;
2239 case MBEDTLS_CIPHER_AES_256_GCM:
2240 *alg = PSA_ALG_GCM;
2241 *key_type = PSA_KEY_TYPE_AES;
2242 *key_size = 256;
2243 break;
2244 case MBEDTLS_CIPHER_ARIA_128_CBC:
2245 *alg = PSA_ALG_CBC_NO_PADDING;
2246 *key_type = PSA_KEY_TYPE_ARIA;
2247 *key_size = 128;
2248 break;
2249 case MBEDTLS_CIPHER_ARIA_128_CCM:
2250 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2251 *key_type = PSA_KEY_TYPE_ARIA;
2252 *key_size = 128;
2253 break;
2254 case MBEDTLS_CIPHER_ARIA_128_GCM:
2255 *alg = PSA_ALG_GCM;
2256 *key_type = PSA_KEY_TYPE_ARIA;
2257 *key_size = 128;
2258 break;
2259 case MBEDTLS_CIPHER_ARIA_192_CCM:
2260 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2261 *key_type = PSA_KEY_TYPE_ARIA;
2262 *key_size = 192;
2263 break;
2264 case MBEDTLS_CIPHER_ARIA_192_GCM:
2265 *alg = PSA_ALG_GCM;
2266 *key_type = PSA_KEY_TYPE_ARIA;
2267 *key_size = 192;
2268 break;
2269 case MBEDTLS_CIPHER_ARIA_256_CBC:
2270 *alg = PSA_ALG_CBC_NO_PADDING;
2271 *key_type = PSA_KEY_TYPE_ARIA;
2272 *key_size = 256;
2273 break;
2274 case MBEDTLS_CIPHER_ARIA_256_CCM:
2275 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2276 *key_type = PSA_KEY_TYPE_ARIA;
2277 *key_size = 256;
2278 break;
2279 case MBEDTLS_CIPHER_ARIA_256_GCM:
2280 *alg = PSA_ALG_GCM;
2281 *key_type = PSA_KEY_TYPE_ARIA;
2282 *key_size = 256;
2283 break;
2284 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2285 *alg = PSA_ALG_CBC_NO_PADDING;
2286 *key_type = PSA_KEY_TYPE_CAMELLIA;
2287 *key_size = 128;
2288 break;
2289 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2290 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2291 *key_type = PSA_KEY_TYPE_CAMELLIA;
2292 *key_size = 128;
2293 break;
2294 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2295 *alg = PSA_ALG_GCM;
2296 *key_type = PSA_KEY_TYPE_CAMELLIA;
2297 *key_size = 128;
2298 break;
2299 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2300 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2301 *key_type = PSA_KEY_TYPE_CAMELLIA;
2302 *key_size = 192;
2303 break;
2304 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2305 *alg = PSA_ALG_GCM;
2306 *key_type = PSA_KEY_TYPE_CAMELLIA;
2307 *key_size = 192;
2308 break;
2309 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2310 *alg = PSA_ALG_CBC_NO_PADDING;
2311 *key_type = PSA_KEY_TYPE_CAMELLIA;
2312 *key_size = 256;
2313 break;
2314 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2315 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2316 *key_type = PSA_KEY_TYPE_CAMELLIA;
2317 *key_size = 256;
2318 break;
2319 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2320 *alg = PSA_ALG_GCM;
2321 *key_type = PSA_KEY_TYPE_CAMELLIA;
2322 *key_size = 256;
2323 break;
2324 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2325 *alg = PSA_ALG_CHACHA20_POLY1305;
2326 *key_type = PSA_KEY_TYPE_CHACHA20;
2327 *key_size = 256;
2328 break;
2329 case MBEDTLS_CIPHER_NULL:
2330 *alg = MBEDTLS_SSL_NULL_CIPHER;
2331 *key_type = 0;
2332 *key_size = 0;
2333 break;
2334 default:
2335 return PSA_ERROR_NOT_SUPPORTED;
2336 }
2337
2338 return PSA_SUCCESS;
2339}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002340#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002341
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002342#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002343int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2344 const unsigned char *dhm_P, size_t P_len,
2345 const unsigned char *dhm_G, size_t G_len )
2346{
Janos Follath865b3eb2019-12-16 11:46:15 +00002347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002348
Glenn Strausscee11292021-12-20 01:43:17 -05002349 mbedtls_mpi_free( &conf->dhm_P );
2350 mbedtls_mpi_free( &conf->dhm_G );
2351
Hanno Beckera90658f2017-10-04 15:29:08 +01002352 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2353 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2354 {
2355 mbedtls_mpi_free( &conf->dhm_P );
2356 mbedtls_mpi_free( &conf->dhm_G );
2357 return( ret );
2358 }
2359
2360 return( 0 );
2361}
Paul Bakker5121ce52009-01-03 21:22:43 +00002362
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002363int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002364{
Janos Follath865b3eb2019-12-16 11:46:15 +00002365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002366
Glenn Strausscee11292021-12-20 01:43:17 -05002367 mbedtls_mpi_free( &conf->dhm_P );
2368 mbedtls_mpi_free( &conf->dhm_G );
2369
Gilles Peskinee5702482021-06-11 21:59:08 +02002370 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2371 &conf->dhm_P ) ) != 0 ||
2372 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2373 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002374 {
2375 mbedtls_mpi_free( &conf->dhm_P );
2376 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002377 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002378 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002379
2380 return( 0 );
2381}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002382#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002383
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002384#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2385/*
2386 * Set the minimum length for Diffie-Hellman parameters
2387 */
2388void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2389 unsigned int bitlen )
2390{
2391 conf->dhm_min_bitlen = bitlen;
2392}
2393#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2394
Gilles Peskineeccd8882020-03-10 12:19:08 +01002395#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002396#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002397/*
2398 * Set allowed/preferred hashes for handshake signatures
2399 */
2400void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2401 const int *hashes )
2402{
2403 conf->sig_hashes = hashes;
2404}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002405#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002406
Jerry Yuf017ee42022-01-12 15:49:48 +08002407/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002408void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2409 const uint16_t* sig_algs )
2410{
Jerry Yuf017ee42022-01-12 15:49:48 +08002411#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2412 conf->sig_hashes = NULL;
2413#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2414 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002415}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002416#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002417
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002418#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002419#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002420/*
2421 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002422 *
2423 * mbedtls_ssl_setup() takes the provided list
2424 * and translates it to a list of IANA TLS group identifiers,
2425 * stored in ssl->handshake->group_list.
2426 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002427 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002428void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002429 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002431 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002432 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002433}
Brett Warrene0edc842021-08-17 09:53:13 +01002434#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002435#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002436
Brett Warrene0edc842021-08-17 09:53:13 +01002437/*
2438 * Set the allowed groups
2439 */
2440void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2441 const uint16_t *group_list )
2442{
2443#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2444 conf->curve_list = NULL;
2445#endif
2446 conf->group_list = group_list;
2447}
2448
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002449#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
Hanno Becker947194e2017-04-07 13:25:49 +01002452 /* Initialize to suppress unnecessary compiler warning */
2453 size_t hostname_len = 0;
2454
2455 /* Check if new hostname is valid before
2456 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002457 if( hostname != NULL )
2458 {
2459 hostname_len = strlen( hostname );
2460
2461 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2463 }
2464
2465 /* Now it's clear that we will overwrite the old hostname,
2466 * so we can free it safely */
2467
2468 if( ssl->hostname != NULL )
2469 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002470 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002471 mbedtls_free( ssl->hostname );
2472 }
2473
2474 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002475
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002477 {
2478 ssl->hostname = NULL;
2479 }
2480 else
2481 {
2482 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002483 if( ssl->hostname == NULL )
2484 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002485
Hanno Becker947194e2017-04-07 13:25:49 +01002486 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002487
Hanno Becker947194e2017-04-07 13:25:49 +01002488 ssl->hostname[hostname_len] = '\0';
2489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002490
2491 return( 0 );
2492}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002493#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002495#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002496void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002498 const unsigned char *, size_t),
2499 void *p_sni )
2500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002501 conf->f_sni = f_sni;
2502 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002507int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002508{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002509 size_t cur_len, tot_len;
2510 const char **p;
2511
2512 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002513 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2514 * MUST NOT be truncated."
2515 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002516 */
2517 tot_len = 0;
2518 for( p = protos; *p != NULL; p++ )
2519 {
2520 cur_len = strlen( *p );
2521 tot_len += cur_len;
2522
Ronald Cron8216dd32020-04-23 16:41:44 +02002523 if( ( cur_len == 0 ) ||
2524 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2525 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002527 }
2528
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002529 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002530
2531 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002532}
2533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002535{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002536 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002539
Johan Pascalb62bb512015-12-03 21:56:45 +01002540#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002541void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2542 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002543{
2544 conf->dtls_srtp_mki_support = support_mki_value;
2545}
2546
Ron Eldoref72faf2018-07-12 11:54:20 +03002547int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2548 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002549 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002550{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002551 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002552 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002554 }
Ron Eldor591f1622018-01-22 12:30:04 +02002555
2556 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002557 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002558 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002559 }
Ron Eldor591f1622018-01-22 12:30:04 +02002560
2561 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2562 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002563 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002564}
2565
Ron Eldoref72faf2018-07-12 11:54:20 +03002566int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002567 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002568{
Johan Pascal253d0262020-09-22 13:04:45 +02002569 const mbedtls_ssl_srtp_profile *p;
2570 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002571
Johan Pascal253d0262020-09-22 13:04:45 +02002572 /* check the profiles list: all entry must be valid,
2573 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002574 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2575 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2576 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002577 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002578 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002579 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002580 list_size++;
2581 }
2582 else
2583 {
2584 /* unsupported value, stop parsing and set the size to an error value */
2585 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002586 }
2587 }
2588
Johan Pascal5ef72d22020-10-28 17:05:47 +01002589 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002590 {
Johan Pascal253d0262020-09-22 13:04:45 +02002591 conf->dtls_srtp_profile_list = NULL;
2592 conf->dtls_srtp_profile_list_len = 0;
2593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2594 }
2595
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002596 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002597 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002598
2599 return( 0 );
2600}
2601
Johan Pascal5ef72d22020-10-28 17:05:47 +01002602void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2603 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002604{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002605 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2606 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002607 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002608 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002609 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002610 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002611 else
2612 {
2613 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002614 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2615 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002616 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002617}
Johan Pascalb62bb512015-12-03 21:56:45 +01002618#endif /* MBEDTLS_SSL_DTLS_SRTP */
2619
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302620#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002621void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002622{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002623 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002624}
2625
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002626void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002627{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002628 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002629}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302630#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002631
Janos Follath088ce432017-04-10 12:42:31 +01002632#if defined(MBEDTLS_SSL_SRV_C)
2633void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2634 char cert_req_ca_list )
2635{
2636 conf->cert_req_ca_list = cert_req_ca_list;
2637}
2638#endif
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002641void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002643 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002644}
2645#endif
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002648void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002650 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002651}
2652#endif
2653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002655int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002658 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002661 }
2662
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002663 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002664
2665 return( 0 );
2666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002668
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002669void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002671 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002672}
2673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002675void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002677 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002678}
2679
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002680void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002682 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002683}
2684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002685void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002686 const unsigned char period[8] )
2687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002688 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002693#if defined(MBEDTLS_SSL_CLI_C)
2694void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002695{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002696 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002697}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002698#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002699
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002700#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002701
Jerry Yud0766ec2022-09-22 10:46:57 +08002702#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002703void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2704 uint16_t num_tickets )
2705{
Jerry Yud0766ec2022-09-22 10:46:57 +08002706 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002707}
2708#endif
2709
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002710void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2711 mbedtls_ssl_ticket_write_t *f_ticket_write,
2712 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2713 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002714{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002715 conf->f_ticket_write = f_ticket_write;
2716 conf->f_ticket_parse = f_ticket_parse;
2717 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002718}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002721
Hanno Becker7e6c1782021-06-08 09:24:55 +01002722void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2723 mbedtls_ssl_export_keys_t *f_export_keys,
2724 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002725{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002726 ssl->f_export_keys = f_export_keys;
2727 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002728}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002729
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002730#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002731void mbedtls_ssl_conf_async_private_cb(
2732 mbedtls_ssl_config *conf,
2733 mbedtls_ssl_async_sign_t *f_async_sign,
2734 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2735 mbedtls_ssl_async_resume_t *f_async_resume,
2736 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002737 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002738{
2739 conf->f_async_sign_start = f_async_sign;
2740 conf->f_async_decrypt_start = f_async_decrypt;
2741 conf->f_async_resume = f_async_resume;
2742 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002743 conf->p_async_config_data = async_config_data;
2744}
2745
Gilles Peskine8f97af72018-04-26 11:46:10 +02002746void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2747{
2748 return( conf->p_async_config_data );
2749}
2750
Gilles Peskine1febfef2018-04-30 11:54:39 +02002751void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002752{
2753 if( ssl->handshake == NULL )
2754 return( NULL );
2755 else
2756 return( ssl->handshake->user_async_ctx );
2757}
2758
Gilles Peskine1febfef2018-04-30 11:54:39 +02002759void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002760 void *ctx )
2761{
2762 if( ssl->handshake != NULL )
2763 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002764}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002765#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002766
Paul Bakker5121ce52009-01-03 21:22:43 +00002767/*
2768 * SSL get accessors
2769 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002770uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002772 if( ssl->session != NULL )
2773 return( ssl->session->verify_result );
2774
2775 if( ssl->session_negotiate != NULL )
2776 return( ssl->session_negotiate->verify_result );
2777
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002778 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779}
2780
Glenn Strauss8f526902022-01-13 00:04:49 -05002781int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2782{
2783 if( ssl == NULL || ssl->session == NULL )
2784 return( 0 );
2785
2786 return( ssl->session->ciphersuite );
2787}
2788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002790{
Paul Bakker926c8e42013-03-06 10:23:34 +01002791 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002792 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002795}
2796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002800 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002801 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002802 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002803 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002804 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002805 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002806 default:
2807 return( "unknown (DTLS)" );
2808 }
2809 }
2810#endif
2811
Glenn Strauss60bfe602022-03-14 19:04:24 -04002812 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002813 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002814 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002816 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002817 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002818 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002819 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002820 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002821}
2822
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002823#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002824size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2825{
David Horstmann95d516f2021-05-04 18:36:56 +01002826 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002827 size_t read_mfl;
2828
2829 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2830 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2831 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2832 {
2833 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2834 }
2835
2836 /* Check if a smaller max length was negotiated */
2837 if( ssl->session_out != NULL )
2838 {
2839 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2840 if( read_mfl < max_len )
2841 {
2842 max_len = read_mfl;
2843 }
2844 }
2845
2846 // During a handshake, use the value being negotiated
2847 if( ssl->session_negotiate != NULL )
2848 {
2849 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2850 if( read_mfl < max_len )
2851 {
2852 max_len = read_mfl;
2853 }
2854 }
2855
2856 return( max_len );
2857}
2858
2859size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002860{
2861 size_t max_len;
2862
2863 /*
2864 * Assume mfl_code is correct since it was checked when set
2865 */
Angus Grattond8213d02016-05-25 20:56:48 +10002866 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002867
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002868 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002869 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002870 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002871 {
Angus Grattond8213d02016-05-25 20:56:48 +10002872 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002873 }
2874
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002875 /* During a handshake, use the value being negotiated */
2876 if( ssl->session_negotiate != NULL &&
2877 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2878 {
2879 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2880 }
2881
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002882 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002883}
2884#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2885
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002887size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002888{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002889 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
2890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2891 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2892 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
2893 return ( 0 );
2894
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002895 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
2896 return( ssl->mtu );
2897
2898 if( ssl->mtu == 0 )
2899 return( ssl->handshake->mtu );
2900
2901 return( ssl->mtu < ssl->handshake->mtu ?
2902 ssl->mtu : ssl->handshake->mtu );
2903}
2904#endif /* MBEDTLS_SSL_PROTO_DTLS */
2905
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002906int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
2907{
2908 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2909
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002910#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2911 !defined(MBEDTLS_SSL_PROTO_DTLS)
2912 (void) ssl;
2913#endif
2914
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002915#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002916 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002917
2918 if( max_len > mfl )
2919 max_len = mfl;
2920#endif
2921
2922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002923 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002924 {
Hanno Becker89490712020-02-05 10:50:12 +00002925 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002926 const int ret = mbedtls_ssl_get_record_expansion( ssl );
2927 const size_t overhead = (size_t) ret;
2928
2929 if( ret < 0 )
2930 return( ret );
2931
2932 if( mtu <= overhead )
2933 {
2934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
2935 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2936 }
2937
2938 if( max_len > mtu - overhead )
2939 max_len = mtu - overhead;
2940 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002942
Hanno Becker0defedb2018-08-10 12:35:02 +01002943#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
2944 !defined(MBEDTLS_SSL_PROTO_DTLS)
2945 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002946#endif
2947
2948 return( (int) max_len );
2949}
2950
Hanno Becker2d8e99b2021-04-21 06:19:50 +01002951int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
2952{
2953 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
2954
2955#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2956 (void) ssl;
2957#endif
2958
2959#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2960 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
2961
2962 if( max_len > mfl )
2963 max_len = mfl;
2964#endif
2965
2966 return( (int) max_len );
2967}
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_X509_CRT_PARSE_C)
2970const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00002971{
2972 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002973 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00002974
Hanno Beckere6824572019-02-07 13:18:46 +00002975#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002976 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00002977#else
2978 return( NULL );
2979#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00002984int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
2985 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002986{
Hanno Beckere810bbc2021-05-14 16:01:05 +01002987 int ret;
2988
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002989 if( ssl == NULL ||
2990 dst == NULL ||
2991 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002992 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02002995 }
2996
Hanno Beckere810bbc2021-05-14 16:01:05 +01002997 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
2998 * idempotent: Each session can only be exported once.
2999 *
3000 * (This is in preparation for TLS 1.3 support where we will
3001 * need the ability to export multiple sessions (aka tickets),
3002 * which will be achieved by calling mbedtls_ssl_get_session()
3003 * multiple times until it fails.)
3004 *
3005 * Check whether we have already exported the current session,
3006 * and fail if so.
3007 */
3008 if( ssl->session->exported == 1 )
3009 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3010
3011 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3012 if( ret != 0 )
3013 return( ret );
3014
3015 /* Remember that we've exported the session. */
3016 ssl->session->exported = 1;
3017 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003020
Paul Bakker5121ce52009-01-03 21:22:43 +00003021/*
Hanno Beckera835da52019-05-16 12:39:07 +01003022 * Define ticket header determining Mbed TLS version
3023 * and structure of the ticket.
3024 */
3025
Hanno Becker94ef3b32019-05-16 12:50:45 +01003026/*
Hanno Becker50b59662019-05-28 14:30:45 +01003027 * Define bitflag determining compile-time settings influencing
3028 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003029 */
3030
Hanno Becker50b59662019-05-28 14:30:45 +01003031#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003032#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003033#else
Hanno Becker3e088662019-05-29 11:10:18 +01003034#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003035#endif /* MBEDTLS_HAVE_TIME */
3036
3037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003038#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003039#else
Hanno Becker3e088662019-05-29 11:10:18 +01003040#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003041#endif /* MBEDTLS_X509_CRT_PARSE_C */
3042
3043#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003044#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003045#else
Hanno Becker3e088662019-05-29 11:10:18 +01003046#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003047#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3048
3049#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003050#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003051#else
Hanno Becker3e088662019-05-29 11:10:18 +01003052#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003053#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3054
Hanno Becker94ef3b32019-05-16 12:50:45 +01003055#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003056#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003057#else
Hanno Becker3e088662019-05-29 11:10:18 +01003058#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003059#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3060
Hanno Becker94ef3b32019-05-16 12:50:45 +01003061#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3062#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3063#else
3064#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3065#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3066
Hanno Becker3e088662019-05-29 11:10:18 +01003067#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3068#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3069#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3070#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003071#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3072#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003073
Hanno Becker50b59662019-05-28 14:30:45 +01003074#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003075 ( (uint16_t) ( \
3076 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3077 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3078 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3079 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003080 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003081 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003082
Hanno Beckerf8787072019-05-16 12:41:07 +01003083static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003084 MBEDTLS_VERSION_MAJOR,
3085 MBEDTLS_VERSION_MINOR,
3086 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003087 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3088 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003089};
Hanno Beckera835da52019-05-16 12:39:07 +01003090
3091/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003092 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003093 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003094 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003095 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003096 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003097 * opaque mbedtls_version[3]; // library version: major, minor, patch
3098 * opaque session_format[2]; // library-version specific 16-bit field
3099 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003100 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003101 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003102 * Note: When updating the format, remember to keep
3103 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003104 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003105 * // In this version, `session_format` determines
3106 * // the setting of those compile-time
3107 * // configuration options which influence
3108 * // the structure of mbedtls_ssl_session.
3109 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003110 * uint8_t minor_ver; // Protocol minor version. Possible values:
3111 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003112 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003113 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003114 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003115 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003116 * serialized_session_tls12 data;
3117 *
3118 * };
3119 *
3120 * } serialized_session;
3121 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003122 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003123
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003124MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003125static int ssl_session_save( const mbedtls_ssl_session *session,
3126 unsigned char omit_header,
3127 unsigned char *buf,
3128 size_t buf_len,
3129 size_t *olen )
3130{
3131 unsigned char *p = buf;
3132 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003133 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003134#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3135 size_t out_len;
3136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3137#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003138 if( session == NULL )
3139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3140
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003141 if( !omit_header )
3142 {
3143 /*
3144 * Add Mbed TLS version identifier
3145 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003146 used += sizeof( ssl_serialized_session_header );
3147
3148 if( used <= buf_len )
3149 {
3150 memcpy( p, ssl_serialized_session_header,
3151 sizeof( ssl_serialized_session_header ) );
3152 p += sizeof( ssl_serialized_session_header );
3153 }
3154 }
3155
3156 /*
3157 * TLS version identifier
3158 */
3159 used += 1;
3160 if( used <= buf_len )
3161 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003162 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003163 }
3164
3165 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003166 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003167 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003168 {
3169#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003170 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003171 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003172 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3174
Jerry Yu251a12e2022-07-13 15:15:48 +08003175#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3176 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003177 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3178 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003179 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003180 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003181 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003182#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3183
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003184 default:
3185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3186 }
3187
3188 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003189 if( used > buf_len )
3190 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003191
3192 return( 0 );
3193}
3194
3195/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003196 * Public wrapper for ssl_session_save()
3197 */
3198int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3199 unsigned char *buf,
3200 size_t buf_len,
3201 size_t *olen )
3202{
3203 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3204}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003205
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003206/*
3207 * Deserialize session, see mbedtls_ssl_session_save() for format.
3208 *
3209 * This internal version is wrapped by a public function that cleans up in
3210 * case of error, and has an extra option omit_header.
3211 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003212MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003213static int ssl_session_load( mbedtls_ssl_session *session,
3214 unsigned char omit_header,
3215 const unsigned char *buf,
3216 size_t len )
3217{
3218 const unsigned char *p = buf;
3219 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003220 size_t remaining_len;
3221
3222
3223 if( session == NULL )
3224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003225
3226 if( !omit_header )
3227 {
3228 /*
3229 * Check Mbed TLS version identifier
3230 */
3231
3232 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3234
3235 if( memcmp( p, ssl_serialized_session_header,
3236 sizeof( ssl_serialized_session_header ) ) != 0 )
3237 {
3238 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3239 }
3240 p += sizeof( ssl_serialized_session_header );
3241 }
3242
3243 /*
3244 * TLS version identifier
3245 */
3246 if( 1 > (size_t)( end - p ) )
3247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003248 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003249
3250 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003251 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003252 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003253 {
3254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003255 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003256 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003257#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3258
Jerry Yu438ddd82022-07-07 06:55:50 +00003259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3260 case MBEDTLS_SSL_VERSION_TLS1_3:
3261 return( ssl_tls13_session_load( session, p, remaining_len ) );
3262#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3263
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003264 default:
3265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3266 }
3267}
3268
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003269/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003270 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003271 */
3272int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3273 const unsigned char *buf,
3274 size_t len )
3275{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003276 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003277
3278 if( ret != 0 )
3279 mbedtls_ssl_session_free( session );
3280
3281 return( ret );
3282}
3283
3284/*
Paul Bakker1961b702013-01-25 14:49:24 +01003285 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003287MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003288static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3289{
3290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3291
Ronald Cron66dbf912022-02-02 15:33:46 +01003292 /*
3293 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003294 * that were written into the output buffer by the previous handshake step,
3295 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003296 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3297 * We proceed to the next handshake step only when all data from the
3298 * previous one have been sent to the peer, thus we make sure that this is
3299 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3300 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3301 * we have to wait before to go ahead.
3302 * In the case of TLS 1.3, handshake step handlers do not send data to the
3303 * peer. Data are only sent here and through
3304 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003305 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003306 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003307 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3308 return( ret );
3309
3310#if defined(MBEDTLS_SSL_PROTO_DTLS)
3311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3312 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3313 {
3314 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3315 return( ret );
3316 }
3317#endif /* MBEDTLS_SSL_PROTO_DTLS */
3318
3319 return( ret );
3320}
3321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323{
Hanno Becker41934dd2021-08-07 19:13:43 +01003324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003325
Hanno Becker41934dd2021-08-07 19:13:43 +01003326 if( ssl == NULL ||
3327 ssl->conf == NULL ||
3328 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003329 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003330 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003332 }
3333
3334 ret = ssl_prepare_handshake_step( ssl );
3335 if( ret != 0 )
3336 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003337
Jerry Yue7047812021-09-13 19:26:39 +08003338 ret = mbedtls_ssl_handle_pending_alert( ssl );
3339 if( ret != 0 )
3340 goto cleanup;
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003343 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003344 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3346 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003347
Ronald Cron9f0fba32022-02-10 16:45:15 +01003348 switch( ssl->state )
3349 {
3350 case MBEDTLS_SSL_HELLO_REQUEST:
3351 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3352 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003353
Ronald Cron9f0fba32022-02-10 16:45:15 +01003354 case MBEDTLS_SSL_CLIENT_HELLO:
3355 ret = mbedtls_ssl_write_client_hello( ssl );
3356 break;
3357
3358 default:
3359#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003360 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003361 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3362 else
3363 ret = mbedtls_ssl_handshake_client_step( ssl );
3364#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3365 ret = mbedtls_ssl_handshake_client_step( ssl );
3366#else
3367 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3368#endif
3369 }
Jerry Yub9930e72021-08-06 17:11:51 +08003370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003373 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003374 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003375#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003376 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003377 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003378#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003379
3380#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3381 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3382 ret = mbedtls_ssl_handshake_server_step( ssl );
3383#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003385#endif
3386
Jerry Yue7047812021-09-13 19:26:39 +08003387 if( ret != 0 )
3388 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003389 /* handshake_step return error. And it is same
3390 * with alert_reason.
3391 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003392 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003393 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003394 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003395 goto cleanup;
3396 }
3397 }
3398
3399cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003400 return( ret );
3401}
3402
3403/*
3404 * Perform the SSL handshake
3405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003407{
3408 int ret = 0;
3409
Hanno Beckera817ea42020-10-20 15:20:23 +01003410 /* Sanity checks */
3411
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003412 if( ssl == NULL || ssl->conf == NULL )
3413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3414
Hanno Beckera817ea42020-10-20 15:20:23 +01003415#if defined(MBEDTLS_SSL_PROTO_DTLS)
3416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3417 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3418 {
3419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3420 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3422 }
3423#endif /* MBEDTLS_SSL_PROTO_DTLS */
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003426
Hanno Beckera817ea42020-10-20 15:20:23 +01003427 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003428 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003431
3432 if( ret != 0 )
3433 break;
3434 }
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
3438 return( ret );
3439}
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#if defined(MBEDTLS_SSL_RENEGOTIATION)
3442#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003443/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003444 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003445 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003446MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003448{
Janos Follath865b3eb2019-12-16 11:46:15 +00003449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003452
3453 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3455 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003456
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003458 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003460 return( ret );
3461 }
3462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003464
3465 return( 0 );
3466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003468
3469/*
3470 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 * - any side: calling mbedtls_ssl_renegotiate(),
3472 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3473 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003474 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003475 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003477 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003478int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003479{
Janos Follath865b3eb2019-12-16 11:46:15 +00003480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003483
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003484 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3485 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003486
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003487 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3488 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003492 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003494 ssl->handshake->out_msg_seq = 1;
3495 else
3496 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003497 }
3498#endif
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3501 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003506 return( ret );
3507 }
3508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003510
3511 return( 0 );
3512}
3513
3514/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003515 * Renegotiate current connection on client,
3516 * or request renegotiation on server
3517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003521
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003522 if( ssl == NULL || ssl->conf == NULL )
3523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003526 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003527 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003528 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003529 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003533
3534 /* Did we already try/start sending HelloRequest? */
3535 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003537
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003538 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003543 /*
3544 * On client, either start the renegotiation process or,
3545 * if already in progress, continue the handshake
3546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003548 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003549 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003551
Hanno Becker40cdaa12020-02-05 10:48:27 +00003552 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003553 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003555 return( ret );
3556 }
3557 }
3558 else
3559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003563 return( ret );
3564 }
3565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003567
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003568 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003571
Gilles Peskine9b562d52018-04-25 20:32:43 +02003572void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003573{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003574 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3575
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003576 if( handshake == NULL )
3577 return;
3578
Brett Warrene0edc842021-08-17 09:53:13 +01003579#if defined(MBEDTLS_ECP_C)
3580#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3581 if ( ssl->handshake->group_list_heap_allocated )
3582 mbedtls_free( (void*) handshake->group_list );
3583 handshake->group_list = NULL;
3584#endif /* MBEDTLS_DEPRECATED_REMOVED */
3585#endif /* MBEDTLS_ECP_C */
3586
Jerry Yuf017ee42022-01-12 15:49:48 +08003587#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
3588#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3589 if ( ssl->handshake->sig_algs_heap_allocated )
3590 mbedtls_free( (void*) handshake->sig_algs );
3591 handshake->sig_algs = NULL;
3592#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003593#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3594 if( ssl->handshake->certificate_request_context )
3595 {
3596 mbedtls_free( (void*) handshake->certificate_request_context );
3597 }
3598#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuf017ee42022-01-12 15:49:48 +08003599#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
3600
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003601#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3602 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3603 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003604 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003605 handshake->async_in_progress = 0;
3606 }
3607#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3608
Andrzej Kurek25f27152022-08-17 16:09:31 -04003609#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003610#if defined(MBEDTLS_USE_PSA_CRYPTO)
3611 psa_hash_abort( &handshake->fin_sha256_psa );
3612#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003613 mbedtls_sha256_free( &handshake->fin_sha256 );
3614#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003615#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003616#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003617#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003618 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003619#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003620 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003621#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003622#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#if defined(MBEDTLS_DHM_C)
3625 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003626#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003627#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003629#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003630#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003631 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003632#if defined(MBEDTLS_SSL_CLI_C)
3633 mbedtls_free( handshake->ecjpake_cache );
3634 handshake->ecjpake_cache = NULL;
3635 handshake->ecjpake_cache_len = 0;
3636#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003637#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003638
Janos Follath4ae5c292016-02-10 11:27:43 +00003639#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3640 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003641 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003643#endif
3644
Gilles Peskineeccd8882020-03-10 12:19:08 +01003645#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003646#if defined(MBEDTLS_USE_PSA_CRYPTO)
3647 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3648 {
3649 /* The maintenance of the external PSK key slot is the
3650 * user's responsibility. */
3651 if( ssl->handshake->psk_opaque_is_internal )
3652 {
3653 psa_destroy_key( ssl->handshake->psk_opaque );
3654 ssl->handshake->psk_opaque_is_internal = 0;
3655 }
3656 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3657 }
Neil Armstronge952a302022-05-03 10:22:14 +02003658#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003659 if( handshake->psk != NULL )
3660 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003661 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003662 mbedtls_free( handshake->psk );
3663 }
Neil Armstronge952a302022-05-03 10:22:14 +02003664#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003665#endif
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3668 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003669 /*
3670 * Free only the linked list wrapper, not the keys themselves
3671 * since the belong to the SNI callback
3672 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003673 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003675
Gilles Peskineeccd8882020-03-10 12:19:08 +01003676#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003677 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003678 if( handshake->ecrs_peer_cert != NULL )
3679 {
3680 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3681 mbedtls_free( handshake->ecrs_peer_cert );
3682 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003683#endif
3684
Hanno Becker75173122019-02-06 16:18:31 +00003685#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3686 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3687 mbedtls_pk_free( &handshake->peer_pubkey );
3688#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3689
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003690#if defined(MBEDTLS_SSL_CLI_C) && \
3691 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3692 mbedtls_free( handshake->cookie );
3693#endif /* MBEDTLS_SSL_CLI_C &&
3694 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003695
3696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003697 mbedtls_ssl_flight_free( handshake->flight );
3698 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003699#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003700
Ronald Cronf12b81d2022-03-15 10:42:41 +01003701#if defined(MBEDTLS_ECDH_C) && \
3702 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003703 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003704 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003705#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3706
Ronald Cron6f135e12021-12-08 16:57:54 +01003707#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003708 mbedtls_ssl_transform_free( handshake->transform_handshake );
3709 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003710 mbedtls_free( handshake->transform_earlydata );
3711 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003712#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003713
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003714
3715#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3716 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3717 * processes datagrams and the fact that a datagram is allowed to have
3718 * several records in it, it is possible that the I/O buffers are not
3719 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003720 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3721 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003722#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003723
Jerry Yuba9c7272021-10-30 11:54:10 +08003724 /* mbedtls_platform_zeroize MUST be last one in this function */
3725 mbedtls_platform_zeroize( handshake,
3726 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003727}
3728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003730{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003731 if( session == NULL )
3732 return;
3733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003735 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003736#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003737
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003738#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003740#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003741
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003742#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3743 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003744 mbedtls_free( session->hostname );
3745#endif
3746
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003747 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003748}
3749
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003750#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003751
3752#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3753#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3754#else
3755#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3756#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3757
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003758#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003759
3760#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3761#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3762#else
3763#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3764#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3765
3766#if defined(MBEDTLS_SSL_ALPN)
3767#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3768#else
3769#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3770#endif /* MBEDTLS_SSL_ALPN */
3771
3772#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3773#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3774#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3775#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3776
3777#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3778 ( (uint32_t) ( \
3779 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3780 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3781 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3782 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3783 0u ) )
3784
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003785static unsigned char ssl_serialized_context_header[] = {
3786 MBEDTLS_VERSION_MAJOR,
3787 MBEDTLS_VERSION_MINOR,
3788 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003789 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3790 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3791 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3792 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3793 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003794};
3795
Paul Bakker5121ce52009-01-03 21:22:43 +00003796/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003797 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003798 *
3799 * The format of the serialized data is:
3800 * (in the presentation language of TLS, RFC 8446 section 3)
3801 *
3802 * // header
3803 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003804 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003805 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003806 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003807 * Note: When updating the format, remember to keep these
3808 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003809 *
3810 * // session sub-structure
3811 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3812 * // transform sub-structure
3813 * uint8 random[64]; // ServerHello.random+ClientHello.random
3814 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3815 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3816 * // fields from ssl_context
3817 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3818 * uint64 in_window_top; // DTLS: last validated record seq_num
3819 * uint64 in_window; // DTLS: bitmask for replay protection
3820 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3821 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3822 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3823 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3824 *
3825 * Note that many fields of the ssl_context or sub-structures are not
3826 * serialized, as they fall in one of the following categories:
3827 *
3828 * 1. forced value (eg in_left must be 0)
3829 * 2. pointer to dynamically-allocated memory (eg session, transform)
3830 * 3. value can be re-derived from other data (eg session keys from MS)
3831 * 4. value was temporary (eg content of input buffer)
3832 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003833 */
3834int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3835 unsigned char *buf,
3836 size_t buf_len,
3837 size_t *olen )
3838{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003839 unsigned char *p = buf;
3840 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003841 size_t session_len;
3842 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003843
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003844 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003845 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3846 * this function's documentation.
3847 *
3848 * These are due to assumptions/limitations in the implementation. Some of
3849 * them are likely to stay (no handshake in progress) some might go away
3850 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003851 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003852 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003853 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003854 {
3855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003857 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003858 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003859 {
3860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003862 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003863 /* Double-check that sub-structures are indeed ready */
3864 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003865 {
3866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003868 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003869 /* There must be no pending incoming or outgoing data */
3870 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003871 {
3872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003873 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003874 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003875 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003876 {
3877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003879 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003880 /* Protocol must be DLTS, not TLS */
3881 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003882 {
3883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003885 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003886 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04003887 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003888 {
3889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003891 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003892 /* We must be using an AEAD ciphersuite */
3893 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003894 {
3895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003897 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003898 /* Renegotiation must not be enabled */
3899#if defined(MBEDTLS_SSL_RENEGOTIATION)
3900 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003904 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003905#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003906
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003907 /*
3908 * Version and format identifier
3909 */
3910 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003911
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003912 if( used <= buf_len )
3913 {
3914 memcpy( p, ssl_serialized_context_header,
3915 sizeof( ssl_serialized_context_header ) );
3916 p += sizeof( ssl_serialized_context_header );
3917 }
3918
3919 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003920 * Session (length + data)
3921 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003922 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003923 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
3924 return( ret );
3925
3926 used += 4 + session_len;
3927 if( used <= buf_len )
3928 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003929 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
3930 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003931
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003932 ret = ssl_session_save( ssl->session, 1,
3933 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003934 if( ret != 0 )
3935 return( ret );
3936
3937 p += session_len;
3938 }
3939
3940 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02003941 * Transform
3942 */
3943 used += sizeof( ssl->transform->randbytes );
3944 if( used <= buf_len )
3945 {
3946 memcpy( p, ssl->transform->randbytes,
3947 sizeof( ssl->transform->randbytes ) );
3948 p += sizeof( ssl->transform->randbytes );
3949 }
3950
3951#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3952 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
3953 if( used <= buf_len )
3954 {
3955 *p++ = ssl->transform->in_cid_len;
3956 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
3957 p += ssl->transform->in_cid_len;
3958
3959 *p++ = ssl->transform->out_cid_len;
3960 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
3961 p += ssl->transform->out_cid_len;
3962 }
3963#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3964
3965 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003966 * Saved fields from top-level ssl_context structure
3967 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003968 used += 4;
3969 if( used <= buf_len )
3970 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003971 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
3972 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003973 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003974
3975#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3976 used += 16;
3977 if( used <= buf_len )
3978 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003979 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
3980 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003981
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01003982 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
3983 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003984 }
3985#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3986
3987#if defined(MBEDTLS_SSL_PROTO_DTLS)
3988 used += 1;
3989 if( used <= buf_len )
3990 {
3991 *p++ = ssl->disable_datagram_packing;
3992 }
3993#endif /* MBEDTLS_SSL_PROTO_DTLS */
3994
Jerry Yuae0b2e22021-10-08 15:21:19 +08003995 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02003996 if( used <= buf_len )
3997 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08003998 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
3999 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004000 }
4001
4002#if defined(MBEDTLS_SSL_PROTO_DTLS)
4003 used += 2;
4004 if( used <= buf_len )
4005 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004006 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4007 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004008 }
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010
4011#if defined(MBEDTLS_SSL_ALPN)
4012 {
4013 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004014 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004015 : 0;
4016
4017 used += 1 + alpn_len;
4018 if( used <= buf_len )
4019 {
4020 *p++ = alpn_len;
4021
4022 if( ssl->alpn_chosen != NULL )
4023 {
4024 memcpy( p, ssl->alpn_chosen, alpn_len );
4025 p += alpn_len;
4026 }
4027 }
4028 }
4029#endif /* MBEDTLS_SSL_ALPN */
4030
4031 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004032 * Done
4033 */
4034 *olen = used;
4035
4036 if( used > buf_len )
4037 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004038
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004039 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4040
Hanno Becker43aefe22020-02-05 10:44:56 +00004041 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004042}
4043
4044/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004045 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004046 *
4047 * This internal version is wrapped by a public function that cleans up in
4048 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004049 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004050MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004051static int ssl_context_load( mbedtls_ssl_context *ssl,
4052 const unsigned char *buf,
4053 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004054{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004055 const unsigned char *p = buf;
4056 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004057 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004059
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004060 /*
4061 * The context should have been freshly setup or reset.
4062 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004063 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004064 * renegotiating, or if the user mistakenly loaded a session first.)
4065 */
4066 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4067 ssl->session != NULL )
4068 {
4069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4070 }
4071
4072 /*
4073 * We can't check that the config matches the initial one, but we can at
4074 * least check it matches the requirements for serializing.
4075 */
4076 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004077 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4078 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004080 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004081#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004082 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004083 {
4084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4085 }
4086
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004087 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4088
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004089 /*
4090 * Check version identifier
4091 */
4092 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4094
4095 if( memcmp( p, ssl_serialized_context_header,
4096 sizeof( ssl_serialized_context_header ) ) != 0 )
4097 {
4098 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4099 }
4100 p += sizeof( ssl_serialized_context_header );
4101
4102 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004103 * Session
4104 */
4105 if( (size_t)( end - p ) < 4 )
4106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4107
4108 session_len = ( (size_t) p[0] << 24 ) |
4109 ( (size_t) p[1] << 16 ) |
4110 ( (size_t) p[2] << 8 ) |
4111 ( (size_t) p[3] );
4112 p += 4;
4113
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004114 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004115 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004116 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004117 ssl->session_in = ssl->session;
4118 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004119 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004120
4121 if( (size_t)( end - p ) < session_len )
4122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4123
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004124 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004125 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004126 {
4127 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004128 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004129 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004130
4131 p += session_len;
4132
4133 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004134 * Transform
4135 */
4136
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004137 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004138 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004139 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004140 ssl->transform_in = ssl->transform;
4141 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004142 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004143
4144 /* Read random bytes and populate structure */
4145 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu840fbb22022-02-17 14:59:29 +08004147#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerbd257552021-03-22 06:59:27 +00004148 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004149 ssl->session->ciphersuite,
4150 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004151#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004152 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004153#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004154 ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
4155 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004156 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004157 ssl->conf->endpoint,
4158 ssl );
4159 if( ret != 0 )
4160 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004162 p += sizeof( ssl->transform->randbytes );
4163
4164#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4165 /* Read connection IDs and store them */
4166 if( (size_t)( end - p ) < 1 )
4167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4168
4169 ssl->transform->in_cid_len = *p++;
4170
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004171 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4173
4174 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4175 p += ssl->transform->in_cid_len;
4176
4177 ssl->transform->out_cid_len = *p++;
4178
4179 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4181
4182 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4183 p += ssl->transform->out_cid_len;
4184#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4185
4186 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004187 * Saved fields from top-level ssl_context structure
4188 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004189 if( (size_t)( end - p ) < 4 )
4190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4191
4192 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4193 ( (uint32_t) p[1] << 16 ) |
4194 ( (uint32_t) p[2] << 8 ) |
4195 ( (uint32_t) p[3] );
4196 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004197
4198#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4199 if( (size_t)( end - p ) < 16 )
4200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4201
4202 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4203 ( (uint64_t) p[1] << 48 ) |
4204 ( (uint64_t) p[2] << 40 ) |
4205 ( (uint64_t) p[3] << 32 ) |
4206 ( (uint64_t) p[4] << 24 ) |
4207 ( (uint64_t) p[5] << 16 ) |
4208 ( (uint64_t) p[6] << 8 ) |
4209 ( (uint64_t) p[7] );
4210 p += 8;
4211
4212 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4213 ( (uint64_t) p[1] << 48 ) |
4214 ( (uint64_t) p[2] << 40 ) |
4215 ( (uint64_t) p[3] << 32 ) |
4216 ( (uint64_t) p[4] << 24 ) |
4217 ( (uint64_t) p[5] << 16 ) |
4218 ( (uint64_t) p[6] << 8 ) |
4219 ( (uint64_t) p[7] );
4220 p += 8;
4221#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4222
4223#if defined(MBEDTLS_SSL_PROTO_DTLS)
4224 if( (size_t)( end - p ) < 1 )
4225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4226
4227 ssl->disable_datagram_packing = *p++;
4228#endif /* MBEDTLS_SSL_PROTO_DTLS */
4229
Jerry Yud9a94fe2021-09-28 18:58:59 +08004230 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004232 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4233 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004234
4235#if defined(MBEDTLS_SSL_PROTO_DTLS)
4236 if( (size_t)( end - p ) < 2 )
4237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4238
4239 ssl->mtu = ( p[0] << 8 ) | p[1];
4240 p += 2;
4241#endif /* MBEDTLS_SSL_PROTO_DTLS */
4242
4243#if defined(MBEDTLS_SSL_ALPN)
4244 {
4245 uint8_t alpn_len;
4246 const char **cur;
4247
4248 if( (size_t)( end - p ) < 1 )
4249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4250
4251 alpn_len = *p++;
4252
4253 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4254 {
4255 /* alpn_chosen should point to an item in the configured list */
4256 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4257 {
4258 if( strlen( *cur ) == alpn_len &&
4259 memcmp( p, cur, alpn_len ) == 0 )
4260 {
4261 ssl->alpn_chosen = *cur;
4262 break;
4263 }
4264 }
4265 }
4266
4267 /* can only happen on conf mismatch */
4268 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4270
4271 p += alpn_len;
4272 }
4273#endif /* MBEDTLS_SSL_ALPN */
4274
4275 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004276 * Forced fields from top-level ssl_context structure
4277 *
4278 * Most of them already set to the correct value by mbedtls_ssl_init() and
4279 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4280 */
4281 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004282 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004283
Hanno Becker361b10d2019-08-30 10:42:49 +01004284 /* Adjust pointers for header fields of outgoing records to
4285 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004286 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004287
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004288#if defined(MBEDTLS_SSL_PROTO_DTLS)
4289 ssl->in_epoch = 1;
4290#endif
4291
4292 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4293 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004294 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4295 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004296 if( ssl->handshake != NULL )
4297 {
4298 mbedtls_ssl_handshake_free( ssl );
4299 mbedtls_free( ssl->handshake );
4300 ssl->handshake = NULL;
4301 }
4302
4303 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004304 * Done - should have consumed entire buffer
4305 */
4306 if( p != end )
4307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004308
4309 return( 0 );
4310}
4311
4312/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004313 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004314 */
4315int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4316 const unsigned char *buf,
4317 size_t len )
4318{
4319 int ret = ssl_context_load( context, buf, len );
4320
4321 if( ret != 0 )
4322 mbedtls_ssl_free( context );
4323
4324 return( ret );
4325}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004326#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004327
4328/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 * Free an SSL context
4330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004332{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004333 if( ssl == NULL )
4334 return;
4335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004337
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004338 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
sander-visserb8aa2072020-05-06 22:05:13 +02004340#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4341 size_t out_buf_len = ssl->out_buf_len;
4342#else
4343 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4344#endif
4345
Darryl Greenb33cc762019-11-28 14:29:44 +00004346 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004348 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 }
4350
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004351 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 {
sander-visserb8aa2072020-05-06 22:05:13 +02004353#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4354 size_t in_buf_len = ssl->in_buf_len;
4355#else
4356 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4357#endif
4358
Darryl Greenb33cc762019-11-28 14:29:44 +00004359 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004361 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 }
4363
Paul Bakker48916f92012-09-16 19:57:18 +00004364 if( ssl->transform )
4365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 mbedtls_ssl_transform_free( ssl->transform );
4367 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004368 }
4369
4370 if( ssl->handshake )
4371 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004372 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4374 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 mbedtls_free( ssl->handshake );
4377 mbedtls_free( ssl->transform_negotiate );
4378 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004379 }
4380
Ronald Cron6f135e12021-12-08 16:57:54 +01004381#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004382 mbedtls_ssl_transform_free( ssl->transform_application );
4383 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004384#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004385
Paul Bakkerc0463502013-02-14 11:19:38 +01004386 if( ssl->session )
4387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 mbedtls_ssl_session_free( ssl->session );
4389 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004390 }
4391
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004393 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004395 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004400#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004402#endif
4403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004405
Paul Bakker86f04f42013-02-14 11:20:09 +01004406 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004407 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004408}
4409
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004410/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004411 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004412 */
4413void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4414{
4415 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4416}
4417
Gilles Peskineae270bf2021-06-02 00:05:29 +02004418/* The selection should be the same as mbedtls_x509_crt_profile_default in
4419 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004420 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004421 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004422 * about this list.
4423 */
Brett Warrene0edc842021-08-17 09:53:13 +01004424static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004425#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004426 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004427#endif
4428#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004429 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004430#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004431#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004432 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004433#endif
4434#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004435 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004436#endif
4437#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004438 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004439#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004440#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004441 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004442#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004443#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004444 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004445#endif
4446#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004447 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004448#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004449 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004450};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004451
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004452static int ssl_preset_suiteb_ciphersuites[] = {
4453 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4454 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4455 0
4456};
4457
Gilles Peskineeccd8882020-03-10 12:19:08 +01004458#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004459
Jerry Yu909df7b2022-01-22 11:56:27 +08004460/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004461 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004462 * rules SHOULD be upheld.
4463 * - No duplicate entries.
4464 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004465 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004466 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004467 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004468static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004469
Andrzej Kurek25f27152022-08-17 16:09:31 -04004470#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 +08004471 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4472 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004473#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004474 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004475
Andrzej Kurek25f27152022-08-17 16:09:31 -04004476#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 +08004477 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4478 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004479#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004480 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4481
Andrzej Kurek25f27152022-08-17 16:09:31 -04004482#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 +08004483 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4484 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004485#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004486 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004487
Andrzej Kurek25f27152022-08-17 16:09:31 -04004488#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 +08004489 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004490#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 +08004491
Andrzej Kurek25f27152022-08-17 16:09:31 -04004492#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 +08004493 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004494#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 +08004495
Andrzej Kurek25f27152022-08-17 16:09:31 -04004496#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004497 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004498#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 +08004499
Andrzej Kurek25f27152022-08-17 16:09:31 -04004500#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 +08004501 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4502#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4503
Andrzej Kurek25f27152022-08-17 16:09:31 -04004504#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 +08004505 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4506#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4507
Andrzej Kurek25f27152022-08-17 16:09:31 -04004508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu693a47a2022-06-23 14:02:28 +08004509 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4510#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4511
Gabor Mezei15b95a62022-05-09 16:37:58 +02004512 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004513};
4514
4515/* NOTICE: see above */
4516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4517static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004518#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004519#if defined(MBEDTLS_ECDSA_C)
4520 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004521#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004522#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4523 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4524#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004525#if defined(MBEDTLS_RSA_C)
4526 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4527#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004528#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004530#if defined(MBEDTLS_ECDSA_C)
4531 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004532#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004533#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4534 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4535#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004536#if defined(MBEDTLS_RSA_C)
4537 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4538#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004539#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004540#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004541#if defined(MBEDTLS_ECDSA_C)
4542 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004543#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004544#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4545 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4546#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004547#if defined(MBEDTLS_RSA_C)
4548 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4549#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004550#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004551 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004552};
4553#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4554/* NOTICE: see above */
4555static uint16_t ssl_preset_suiteb_sig_algs[] = {
4556
Andrzej Kurek25f27152022-08-17 16:09:31 -04004557#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 +08004558 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4559 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004560#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004561 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4562
Andrzej Kurek25f27152022-08-17 16:09:31 -04004563#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 +08004564 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4565 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004566#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004567 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004568
Andrzej Kurek25f27152022-08-17 16:09:31 -04004569#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 +08004570 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004571#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 +08004572
Andrzej Kurek25f27152022-08-17 16:09:31 -04004573#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 +08004574 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004575#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004576
Gabor Mezei15b95a62022-05-09 16:37:58 +02004577 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004578};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004579
Jerry Yu909df7b2022-01-22 11:56:27 +08004580/* NOTICE: see above */
4581#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4582static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004583#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004584#if defined(MBEDTLS_ECDSA_C)
4585 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004586#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004587#if defined(MBEDTLS_RSA_C)
4588 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4589#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004590#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004591#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004592#if defined(MBEDTLS_ECDSA_C)
4593 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004594#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004595#if defined(MBEDTLS_RSA_C)
4596 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4597#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004598#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004599 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004600};
Jerry Yu909df7b2022-01-22 11:56:27 +08004601#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4602
Jerry Yu1a8b4812022-01-20 17:56:50 +08004603#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004604
Brett Warrene0edc842021-08-17 09:53:13 +01004605static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004606#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004607 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004608#endif
4609#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004610 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004611#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004612 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004613};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004614
Jerry Yu1a8b4812022-01-20 17:56:50 +08004615#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004616/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004617 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004618MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004619static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004620{
4621 size_t i, j;
4622 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004623
Gabor Mezei15b95a62022-05-09 16:37:58 +02004624 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004625 {
Jerry Yuf377d642022-01-25 10:43:59 +08004626 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004627 {
Jerry Yuf377d642022-01-25 10:43:59 +08004628 if( sig_algs[i] != sig_algs[j] )
4629 continue;
4630 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4631 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4632 sig_algs[i], j, i );
4633 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004634 }
4635 }
4636 return( ret );
4637}
4638
4639#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4640
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004641/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004642 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004643 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004644int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004645 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004646{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004647#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004649#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004650
Jerry Yu1a8b4812022-01-20 17:56:50 +08004651#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004652 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004653 {
4654 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4655 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4656 }
4657
Jerry Yuf377d642022-01-25 10:43:59 +08004658 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004659 {
4660 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4661 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4662 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004663
4664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004665 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004666 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004667 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004668 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4669 }
4670
Jerry Yuf377d642022-01-25 10:43:59 +08004671 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004672 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004673 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004674 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4675 }
4676#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004677#endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
4678
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004679 /* Use the functions here so that they are covered in tests,
4680 * but otherwise access member directly for efficiency */
4681 mbedtls_ssl_conf_endpoint( conf, endpoint );
4682 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004683
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004684 /*
4685 * Things that are common to all presets
4686 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004687#if defined(MBEDTLS_SSL_CLI_C)
4688 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4689 {
4690 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4691#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4692 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4693#endif
4694 }
4695#endif
4696
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004697#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4698 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4699#endif
4700
4701#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4702 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4703#endif
4704
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004705#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004706 conf->f_cookie_write = ssl_cookie_write_dummy;
4707 conf->f_cookie_check = ssl_cookie_check_dummy;
4708#endif
4709
4710#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4711 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4712#endif
4713
Janos Follath088ce432017-04-10 12:42:31 +01004714#if defined(MBEDTLS_SSL_SRV_C)
4715 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004716 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004717#endif
4718
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004719#if defined(MBEDTLS_SSL_PROTO_DTLS)
4720 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4721 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4722#endif
4723
4724#if defined(MBEDTLS_SSL_RENEGOTIATION)
4725 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004726 memset( conf->renego_period, 0x00, 2 );
4727 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004728#endif
4729
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004730#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004731 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4732 {
4733 const unsigned char dhm_p[] =
4734 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4735 const unsigned char dhm_g[] =
4736 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004737
Hanno Beckere2defad2021-07-24 05:59:17 +01004738 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4739 dhm_p, sizeof( dhm_p ),
4740 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4741 {
4742 return( ret );
4743 }
4744 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004745#endif
4746
Ronald Cron6f135e12021-12-08 16:57:54 +01004747#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004748#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004749 mbedtls_ssl_conf_new_session_tickets(
4750 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4751#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004752 /*
4753 * Allow all TLS 1.3 key exchange modes by default.
4754 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004755 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004756#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004757
XiaokangQian4d3a6042022-04-21 13:46:17 +00004758 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4759 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004760#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004761 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004762 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004763#else
XiaokangQian060d8672022-04-21 09:24:56 +00004764 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004765#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004766 }
4767 else
4768 {
4769#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4770 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4771 {
4772 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4773 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4774 }
4775 else
4776 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4777 {
4778 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4779 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4780 }
4781#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4782 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4783 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4784#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4785 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4786 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4787#else
4788 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4789#endif
4790 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004791
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004792 /*
4793 * Preset-specific defaults
4794 */
4795 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004796 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004797 /*
4798 * NSA Suite B
4799 */
4800 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004801
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004802 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004803
4804#if defined(MBEDTLS_X509_CRT_PARSE_C)
4805 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004806#endif
4807
Gilles Peskineeccd8882020-03-10 12:19:08 +01004808#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004809#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4810 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4811 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4812 else
4813#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4814 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004815#endif
4816
Brett Warrene0edc842021-08-17 09:53:13 +01004817#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4818 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004819#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004820 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004821 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004822
4823 /*
4824 * Default
4825 */
4826 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004827
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004828 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004829
4830#if defined(MBEDTLS_X509_CRT_PARSE_C)
4831 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4832#endif
4833
Gilles Peskineeccd8882020-03-10 12:19:08 +01004834#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004835#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4836 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4837 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4838 else
4839#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4840 conf->sig_algs = ssl_preset_default_sig_algs;
Hanno Beckerdeb68ce2021-08-10 16:04:05 +01004841#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004842
Brett Warrene0edc842021-08-17 09:53:13 +01004843#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4844 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004845#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004846 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004847
4848#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4849 conf->dhm_min_bitlen = 1024;
4850#endif
4851 }
4852
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004853 return( 0 );
4854}
4855
4856/*
4857 * Free mbedtls_ssl_config
4858 */
4859void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4860{
4861#if defined(MBEDTLS_DHM_C)
4862 mbedtls_mpi_free( &conf->dhm_P );
4863 mbedtls_mpi_free( &conf->dhm_G );
4864#endif
4865
Gilles Peskineeccd8882020-03-10 12:19:08 +01004866#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004867#if defined(MBEDTLS_USE_PSA_CRYPTO)
4868 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4869 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004870 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4871 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004872#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004873 if( conf->psk != NULL )
4874 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004875 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004876 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00004877 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004878 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09004879 }
4880
4881 if( conf->psk_identity != NULL )
4882 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004883 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09004884 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00004885 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004886 conf->psk_identity_len = 0;
4887 }
4888#endif
4889
4890#if defined(MBEDTLS_X509_CRT_PARSE_C)
4891 ssl_key_cert_free( conf->key_cert );
4892#endif
4893
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004894 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004895}
4896
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004897#if defined(MBEDTLS_PK_C) && \
4898 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004899/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_RSA_C)
4905 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
4906 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#if defined(MBEDTLS_ECDSA_C)
4909 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
4910 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004913}
4914
Hanno Becker7e5437a2017-04-28 17:15:26 +01004915unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
4916{
4917 switch( type ) {
4918 case MBEDTLS_PK_RSA:
4919 return( MBEDTLS_SSL_SIG_RSA );
4920 case MBEDTLS_PK_ECDSA:
4921 case MBEDTLS_PK_ECKEY:
4922 return( MBEDTLS_SSL_SIG_ECDSA );
4923 default:
4924 return( MBEDTLS_SSL_SIG_ANON );
4925 }
4926}
4927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004929{
4930 switch( sig )
4931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932#if defined(MBEDTLS_RSA_C)
4933 case MBEDTLS_SSL_SIG_RSA:
4934 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#if defined(MBEDTLS_ECDSA_C)
4937 case MBEDTLS_SSL_SIG_ECDSA:
4938 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004939#endif
4940 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004942 }
4943}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02004944#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004945
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004946/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004947 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004950{
4951 switch( hash )
4952 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004953#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 case MBEDTLS_SSL_HASH_MD5:
4955 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004956#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004957#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 case MBEDTLS_SSL_HASH_SHA1:
4959 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004960#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004961#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 case MBEDTLS_SSL_HASH_SHA224:
4963 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02004964#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 case MBEDTLS_SSL_HASH_SHA256:
4967 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 case MBEDTLS_SSL_HASH_SHA384:
4971 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02004972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004973#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 case MBEDTLS_SSL_HASH_SHA512:
4975 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004976#endif
4977 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004979 }
4980}
4981
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004982/*
4983 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
4984 */
4985unsigned char mbedtls_ssl_hash_from_md_alg( int md )
4986{
4987 switch( md )
4988 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004989#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004990 case MBEDTLS_MD_MD5:
4991 return( MBEDTLS_SSL_HASH_MD5 );
4992#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004994 case MBEDTLS_MD_SHA1:
4995 return( MBEDTLS_SSL_HASH_SHA1 );
4996#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02004998 case MBEDTLS_MD_SHA224:
4999 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005000#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005002 case MBEDTLS_MD_SHA256:
5003 return( MBEDTLS_SSL_HASH_SHA256 );
5004#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005006 case MBEDTLS_MD_SHA384:
5007 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005008#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005010 case MBEDTLS_MD_SHA512:
5011 return( MBEDTLS_SSL_HASH_SHA512 );
5012#endif
5013 default:
5014 return( MBEDTLS_SSL_HASH_NONE );
5015 }
5016}
5017
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005018/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005019 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005020 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005021 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005022int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005023{
Brett Warrene0edc842021-08-17 09:53:13 +01005024 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005025
Brett Warrene0edc842021-08-17 09:53:13 +01005026 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005027 return( -1 );
5028
Brett Warrene0edc842021-08-17 09:53:13 +01005029 for( ; *group_list != 0; group_list++ )
5030 {
5031 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005032 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005033 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005034
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005035 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005036}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005037
5038#if defined(MBEDTLS_ECP_C)
5039/*
5040 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5041 */
5042int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5043{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005044 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005045 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005046
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005047 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005048 return -1;
5049
5050 uint16_t tls_id = grp_info->tls_id;
5051
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005052 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5053}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005054#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if defined(MBEDTLS_X509_CRT_PARSE_C)
5057int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5058 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005059 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005060 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005061{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005062 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005063 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005064 const char *ext_oid;
5065 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005068 {
5069 /* Server part of the key exchange */
5070 switch( ciphersuite->key_exchange )
5071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 case MBEDTLS_KEY_EXCHANGE_RSA:
5073 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005074 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005075 break;
5076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5078 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5079 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5080 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005081 break;
5082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5084 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005085 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005086 break;
5087
5088 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 case MBEDTLS_KEY_EXCHANGE_NONE:
5090 case MBEDTLS_KEY_EXCHANGE_PSK:
5091 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5092 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005093 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005094 usage = 0;
5095 }
5096 }
5097 else
5098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5100 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005101 }
5102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005104 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005105 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005106 ret = -1;
5107 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5112 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005113 }
5114 else
5115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5117 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005121 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005122 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005123 ret = -1;
5124 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005125
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005126 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005129
Jerry Yu148165c2021-09-24 23:20:59 +08005130#if defined(MBEDTLS_USE_PSA_CRYPTO)
5131int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5132 const mbedtls_md_type_t md,
5133 unsigned char *dst,
5134 size_t dst_len,
5135 size_t *olen )
5136{
Ronald Cronf6893e12022-01-07 22:09:01 +01005137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5138 psa_hash_operation_t *hash_operation_to_clone;
5139 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5140
Jerry Yu148165c2021-09-24 23:20:59 +08005141 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005142
5143 switch( md )
5144 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005145#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005146 case MBEDTLS_MD_SHA384:
5147 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5148 break;
5149#endif
5150
Andrzej Kurek25f27152022-08-17 16:09:31 -04005151#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005152 case MBEDTLS_MD_SHA256:
5153 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5154 break;
5155#endif
5156
5157 default:
5158 goto exit;
5159 }
5160
5161 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5162 if( status != PSA_SUCCESS )
5163 goto exit;
5164
5165 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5166 if( status != PSA_SUCCESS )
5167 goto exit;
5168
5169exit:
Ronald Cronb788c042022-02-15 09:14:15 +01005170 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005171}
5172#else /* MBEDTLS_USE_PSA_CRYPTO */
5173
Andrzej Kurek25f27152022-08-17 16:09:31 -04005174#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005175MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005176static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5177 unsigned char *dst,
5178 size_t dst_len,
5179 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005180{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005181 int ret;
5182 mbedtls_sha512_context sha512;
5183
5184 if( dst_len < 48 )
5185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5186
5187 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005188 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005189
5190 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5191 {
5192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5193 goto exit;
5194 }
5195
5196 *olen = 48;
5197
5198exit:
5199
5200 mbedtls_sha512_free( &sha512 );
5201 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005202}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005203#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005204
Andrzej Kurek25f27152022-08-17 16:09:31 -04005205#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005206MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005207static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5208 unsigned char *dst,
5209 size_t dst_len,
5210 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005211{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005212 int ret;
5213 mbedtls_sha256_context sha256;
5214
5215 if( dst_len < 32 )
5216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5217
5218 mbedtls_sha256_init( &sha256 );
5219 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005220
Jerry Yu24c0ec32021-09-09 14:21:07 +08005221 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5222 {
5223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5224 goto exit;
5225 }
5226
5227 *olen = 32;
5228
5229exit:
5230
5231 mbedtls_sha256_free( &sha256 );
5232 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005233}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005234#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005235
Jerry Yu000f9762021-09-14 11:12:51 +08005236int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5237 const mbedtls_md_type_t md,
5238 unsigned char *dst,
5239 size_t dst_len,
5240 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005241{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005242 switch( md )
5243 {
5244
Andrzej Kurek25f27152022-08-17 16:09:31 -04005245#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005246 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005247 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005248#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005249
Andrzej Kurek25f27152022-08-17 16:09:31 -04005250#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005251 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005252 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005253#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005254
5255 default:
5256 break;
5257 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005259}
XiaokangQian647719a2021-12-07 09:16:29 +00005260
Jerry Yu148165c2021-09-24 23:20:59 +08005261#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005262
Gabor Mezei078e8032022-04-27 21:17:56 +02005263#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
5264/* mbedtls_ssl_parse_sig_alg_ext()
5265 *
5266 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5267 * value (TLS 1.3 RFC8446):
5268 * enum {
5269 * ....
5270 * ecdsa_secp256r1_sha256( 0x0403 ),
5271 * ecdsa_secp384r1_sha384( 0x0503 ),
5272 * ecdsa_secp521r1_sha512( 0x0603 ),
5273 * ....
5274 * } SignatureScheme;
5275 *
5276 * struct {
5277 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5278 * } SignatureSchemeList;
5279 *
5280 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5281 * value (TLS 1.2 RFC5246):
5282 * enum {
5283 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5284 * sha512(6), (255)
5285 * } HashAlgorithm;
5286 *
5287 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5288 * SignatureAlgorithm;
5289 *
5290 * struct {
5291 * HashAlgorithm hash;
5292 * SignatureAlgorithm signature;
5293 * } SignatureAndHashAlgorithm;
5294 *
5295 * SignatureAndHashAlgorithm
5296 * supported_signature_algorithms<2..2^16-2>;
5297 *
5298 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5299 * generalization of the TLS 1.2 signature algorithm extension.
5300 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5301 * `SignatureScheme` field of TLS 1.3
5302 *
5303 */
5304int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5305 const unsigned char *buf,
5306 const unsigned char *end )
5307{
5308 const unsigned char *p = buf;
5309 size_t supported_sig_algs_len = 0;
5310 const unsigned char *supported_sig_algs_end;
5311 uint16_t sig_alg;
5312 uint32_t common_idx = 0;
5313
5314 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5315 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5316 p += 2;
5317
5318 memset( ssl->handshake->received_sig_algs, 0,
5319 sizeof(ssl->handshake->received_sig_algs) );
5320
5321 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5322 supported_sig_algs_end = p + supported_sig_algs_len;
5323 while( p < supported_sig_algs_end )
5324 {
5325 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5326 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5327 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005328 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5329 sig_alg,
5330 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005331#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005332 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005333 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5334 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5335 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005336 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005337 }
5338#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005339
Jerry Yuf3b46b52022-06-19 16:52:27 +08005340 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5341 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005342
5343 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5344 {
5345 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5346 common_idx += 1;
5347 }
5348 }
5349 /* Check that we consumed all the message. */
5350 if( p != end )
5351 {
5352 MBEDTLS_SSL_DEBUG_MSG( 1,
5353 ( "Signature algorithms extension length misaligned" ) );
5354 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5355 MBEDTLS_ERR_SSL_DECODE_ERROR );
5356 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5357 }
5358
5359 if( common_idx == 0 )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5362 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5363 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5364 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5365 }
5366
Gabor Mezei15b95a62022-05-09 16:37:58 +02005367 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005368 return( 0 );
5369}
5370
5371#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
5372
Jerry Yudc7bd172022-02-17 13:44:15 +08005373#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5374
5375#if defined(MBEDTLS_USE_PSA_CRYPTO)
5376
5377static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5378 mbedtls_svc_key_id_t key,
5379 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005380 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005381 const unsigned char* seed, size_t seed_length,
5382 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005383 const unsigned char* other_secret,
5384 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005385 size_t capacity )
5386{
5387 psa_status_t status;
5388
5389 status = psa_key_derivation_setup( derivation, alg );
5390 if( status != PSA_SUCCESS )
5391 return( status );
5392
5393 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5394 {
5395 status = psa_key_derivation_input_bytes( derivation,
5396 PSA_KEY_DERIVATION_INPUT_SEED,
5397 seed, seed_length );
5398 if( status != PSA_SUCCESS )
5399 return( status );
5400
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005401 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005402 {
5403 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005404 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5405 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005406 if( status != PSA_SUCCESS )
5407 return( status );
5408 }
5409
Jerry Yudc7bd172022-02-17 13:44:15 +08005410 if( mbedtls_svc_key_id_is_null( key ) )
5411 {
5412 status = psa_key_derivation_input_bytes(
5413 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005414 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005415 }
5416 else
5417 {
5418 status = psa_key_derivation_input_key(
5419 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5420 }
5421 if( status != PSA_SUCCESS )
5422 return( status );
5423
5424 status = psa_key_derivation_input_bytes( derivation,
5425 PSA_KEY_DERIVATION_INPUT_LABEL,
5426 label, label_length );
5427 if( status != PSA_SUCCESS )
5428 return( status );
5429 }
5430 else
5431 {
5432 return( PSA_ERROR_NOT_SUPPORTED );
5433 }
5434
5435 status = psa_key_derivation_set_capacity( derivation, capacity );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438
5439 return( PSA_SUCCESS );
5440}
5441
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005443static int tls_prf_generic( mbedtls_md_type_t md_type,
5444 const unsigned char *secret, size_t slen,
5445 const char *label,
5446 const unsigned char *random, size_t rlen,
5447 unsigned char *dstbuf, size_t dlen )
5448{
5449 psa_status_t status;
5450 psa_algorithm_t alg;
5451 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5452 psa_key_derivation_operation_t derivation =
5453 PSA_KEY_DERIVATION_OPERATION_INIT;
5454
5455 if( md_type == MBEDTLS_MD_SHA384 )
5456 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5457 else
5458 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5459
5460 /* Normally a "secret" should be long enough to be impossible to
5461 * find by brute force, and in particular should not be empty. But
5462 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5463 * and for this use case it makes sense to have a 0-length "secret".
5464 * Since the key API doesn't allow importing a key of length 0,
5465 * keep master_key=0, which setup_psa_key_derivation() understands
5466 * to mean a 0-length "secret" input. */
5467 if( slen != 0 )
5468 {
5469 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5470 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5471 psa_set_key_algorithm( &key_attributes, alg );
5472 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5473
5474 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5475 if( status != PSA_SUCCESS )
5476 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5477 }
5478
5479 status = setup_psa_key_derivation( &derivation,
5480 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005481 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005482 random, rlen,
5483 (unsigned char const *) label,
5484 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005485 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005486 dlen );
5487 if( status != PSA_SUCCESS )
5488 {
5489 psa_key_derivation_abort( &derivation );
5490 psa_destroy_key( master_key );
5491 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5492 }
5493
5494 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5495 if( status != PSA_SUCCESS )
5496 {
5497 psa_key_derivation_abort( &derivation );
5498 psa_destroy_key( master_key );
5499 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5500 }
5501
5502 status = psa_key_derivation_abort( &derivation );
5503 if( status != PSA_SUCCESS )
5504 {
5505 psa_destroy_key( master_key );
5506 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5507 }
5508
5509 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5510 status = psa_destroy_key( master_key );
5511 if( status != PSA_SUCCESS )
5512 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5513
5514 return( 0 );
5515}
5516
5517#else /* MBEDTLS_USE_PSA_CRYPTO */
5518
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005519MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005520static int tls_prf_generic( mbedtls_md_type_t md_type,
5521 const unsigned char *secret, size_t slen,
5522 const char *label,
5523 const unsigned char *random, size_t rlen,
5524 unsigned char *dstbuf, size_t dlen )
5525{
5526 size_t nb;
5527 size_t i, j, k, md_len;
5528 unsigned char *tmp;
5529 size_t tmp_len = 0;
5530 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5531 const mbedtls_md_info_t *md_info;
5532 mbedtls_md_context_t md_ctx;
5533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5534
5535 mbedtls_md_init( &md_ctx );
5536
5537 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5539
5540 md_len = mbedtls_md_get_size( md_info );
5541
5542 tmp_len = md_len + strlen( label ) + rlen;
5543 tmp = mbedtls_calloc( 1, tmp_len );
5544 if( tmp == NULL )
5545 {
5546 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5547 goto exit;
5548 }
5549
5550 nb = strlen( label );
5551 memcpy( tmp + md_len, label, nb );
5552 memcpy( tmp + md_len + nb, random, rlen );
5553 nb += rlen;
5554
5555 /*
5556 * Compute P_<hash>(secret, label + random)[0..dlen]
5557 */
5558 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5559 goto exit;
5560
5561 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5562 if( ret != 0 )
5563 goto exit;
5564 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5565 if( ret != 0 )
5566 goto exit;
5567 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5568 if( ret != 0 )
5569 goto exit;
5570
5571 for( i = 0; i < dlen; i += md_len )
5572 {
5573 ret = mbedtls_md_hmac_reset ( &md_ctx );
5574 if( ret != 0 )
5575 goto exit;
5576 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5577 if( ret != 0 )
5578 goto exit;
5579 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5580 if( ret != 0 )
5581 goto exit;
5582
5583 ret = mbedtls_md_hmac_reset ( &md_ctx );
5584 if( ret != 0 )
5585 goto exit;
5586 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5587 if( ret != 0 )
5588 goto exit;
5589 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5590 if( ret != 0 )
5591 goto exit;
5592
5593 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5594
5595 for( j = 0; j < k; j++ )
5596 dstbuf[i + j] = h_i[j];
5597 }
5598
5599exit:
5600 mbedtls_md_free( &md_ctx );
5601
5602 mbedtls_platform_zeroize( tmp, tmp_len );
5603 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5604
5605 mbedtls_free( tmp );
5606
5607 return( ret );
5608}
5609#endif /* MBEDTLS_USE_PSA_CRYPTO */
5610
Andrzej Kurek25f27152022-08-17 16:09:31 -04005611#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005612MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005613static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5614 const char *label,
5615 const unsigned char *random, size_t rlen,
5616 unsigned char *dstbuf, size_t dlen )
5617{
5618 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5619 label, random, rlen, dstbuf, dlen ) );
5620}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005621#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005622
Andrzej Kurek25f27152022-08-17 16:09:31 -04005623#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005624MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005625static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5626 const char *label,
5627 const unsigned char *random, size_t rlen,
5628 unsigned char *dstbuf, size_t dlen )
5629{
5630 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5631 label, random, rlen, dstbuf, dlen ) );
5632}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005633#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005634
Jerry Yuf009d862022-02-17 14:01:37 +08005635/*
5636 * Set appropriate PRF function and other SSL / TLS1.2 functions
5637 *
5638 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005639 * - hash associated with the ciphersuite (only used by TLS 1.2)
5640 *
5641 * Outputs:
5642 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5643 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005644MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005645static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005646 mbedtls_md_type_t hash )
5647{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005648#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005649 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005650 {
5651 handshake->tls_prf = tls_prf_sha384;
5652 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5653 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5654 }
5655 else
5656#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005657#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005658 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005659 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005660 handshake->tls_prf = tls_prf_sha256;
5661 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5662 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5663 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005664#else
Jerry Yuf009d862022-02-17 14:01:37 +08005665 {
Jerry Yuf009d862022-02-17 14:01:37 +08005666 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005667 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005668 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5669 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005670#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005671
5672 return( 0 );
5673}
Jerry Yud6ab2352022-02-17 14:03:43 +08005674
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005675/*
5676 * Compute master secret if needed
5677 *
5678 * Parameters:
5679 * [in/out] handshake
5680 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5681 * (PSA-PSK) ciphersuite_info, psk_opaque
5682 * [out] premaster (cleared)
5683 * [out] master
5684 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5685 * debug: conf->f_dbg, conf->p_dbg
5686 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005687 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005688 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005689MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005690static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5691 unsigned char *master,
5692 const mbedtls_ssl_context *ssl )
5693{
5694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5695
5696 /* cf. RFC 5246, Section 8.1:
5697 * "The master secret is always exactly 48 bytes in length." */
5698 size_t const master_secret_len = 48;
5699
5700#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5701 unsigned char session_hash[48];
5702#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5703
5704 /* The label for the KDF used for key expansion.
5705 * This is either "master secret" or "extended master secret"
5706 * depending on whether the Extended Master Secret extension
5707 * is used. */
5708 char const *lbl = "master secret";
5709
Przemek Stekielae4ed302022-04-05 17:15:55 +02005710 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005711 * - If the Extended Master Secret extension is not used,
5712 * this is ClientHello.Random + ServerHello.Random
5713 * (see Sect. 8.1 in RFC 5246).
5714 * - If the Extended Master Secret extension is used,
5715 * this is the transcript of the handshake so far.
5716 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005717 unsigned char const *seed = handshake->randbytes;
5718 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005719
5720#if !defined(MBEDTLS_DEBUG_C) && \
5721 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5722 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5723 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5724 ssl = NULL; /* make sure we don't use it except for those cases */
5725 (void) ssl;
5726#endif
5727
5728 if( handshake->resume != 0 )
5729 {
5730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5731 return( 0 );
5732 }
5733
5734#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5735 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5736 {
5737 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005738 seed = session_hash;
5739 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005740
5741 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005742 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005743 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005744#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005745
Przemek Stekiel99114f32022-04-22 11:20:09 +02005746#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005747 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005748 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005749 {
5750 /* Perform PSK-to-MS expansion in a single step. */
5751 psa_status_t status;
5752 psa_algorithm_t alg;
5753 mbedtls_svc_key_id_t psk;
5754 psa_key_derivation_operation_t derivation =
5755 PSA_KEY_DERIVATION_OPERATION_INIT;
5756 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5757
5758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5759
5760 psk = mbedtls_ssl_get_opaque_psk( ssl );
5761
5762 if( hash_alg == MBEDTLS_MD_SHA384 )
5763 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5764 else
5765 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5766
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005767 size_t other_secret_len = 0;
5768 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005769
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005770 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005771 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005772 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005773 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005774 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005775 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005776 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005777 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005778 other_secret_len = 48;
5779 other_secret = handshake->premaster + 2;
5780 break;
5781 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005782 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005783 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5784 other_secret = handshake->premaster + 2;
5785 break;
5786 default:
5787 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005788 }
5789
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005790 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005791 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005792 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005793 (unsigned char const *) lbl,
5794 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005795 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005796 master_secret_len );
5797 if( status != PSA_SUCCESS )
5798 {
5799 psa_key_derivation_abort( &derivation );
5800 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5801 }
5802
5803 status = psa_key_derivation_output_bytes( &derivation,
5804 master,
5805 master_secret_len );
5806 if( status != PSA_SUCCESS )
5807 {
5808 psa_key_derivation_abort( &derivation );
5809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5810 }
5811
5812 status = psa_key_derivation_abort( &derivation );
5813 if( status != PSA_SUCCESS )
5814 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5815 }
5816 else
5817#endif
5818 {
5819 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005820 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005821 master,
5822 master_secret_len );
5823 if( ret != 0 )
5824 {
5825 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
5826 return( ret );
5827 }
5828
5829 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
5830 handshake->premaster,
5831 handshake->pmslen );
5832
5833 mbedtls_platform_zeroize( handshake->premaster,
5834 sizeof(handshake->premaster) );
5835 }
5836
5837 return( 0 );
5838}
5839
Jerry Yud62f87e2022-02-17 14:09:02 +08005840int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
5841{
5842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5843 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5844 ssl->handshake->ciphersuite_info;
5845
5846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
5847
5848 /* Set PRF, calc_verify and calc_finished function pointers */
5849 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08005850 ciphersuite_info->mac );
5851 if( ret != 0 )
5852 {
5853 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
5854 return( ret );
5855 }
5856
5857 /* Compute master secret if needed */
5858 ret = ssl_compute_master( ssl->handshake,
5859 ssl->session_negotiate->master,
5860 ssl );
5861 if( ret != 0 )
5862 {
5863 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
5864 return( ret );
5865 }
5866
5867 /* Swap the client and server random values:
5868 * - MS derivation wanted client+server (RFC 5246 8.1)
5869 * - key derivation wants server+client (RFC 5246 6.3) */
5870 {
5871 unsigned char tmp[64];
5872 memcpy( tmp, ssl->handshake->randbytes, 64 );
5873 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
5874 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
5875 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
5876 }
5877
5878 /* Populate transform structure */
5879 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
5880 ssl->session_negotiate->ciphersuite,
5881 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005882#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08005883 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005884#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08005885 ssl->handshake->tls_prf,
5886 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04005887 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08005888 ssl->conf->endpoint,
5889 ssl );
5890 if( ret != 0 )
5891 {
5892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
5893 return( ret );
5894 }
5895
5896 /* We no longer need Server/ClientHello.random values */
5897 mbedtls_platform_zeroize( ssl->handshake->randbytes,
5898 sizeof( ssl->handshake->randbytes ) );
5899
5900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
5901
5902 return( 0 );
5903}
Jerry Yu8392e0d2022-02-17 14:10:24 +08005904
Ronald Cron4dcbca92022-03-07 10:21:40 +01005905int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
5906{
Ronald Cron4dcbca92022-03-07 10:21:40 +01005907 switch( md )
5908 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005909#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005910 case MBEDTLS_SSL_HASH_SHA384:
5911 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
5912 break;
5913#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005914#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01005915 case MBEDTLS_SSL_HASH_SHA256:
5916 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
5917 break;
5918#endif
5919 default:
5920 return( -1 );
5921 }
5922
Ronald Cronc2f13a02022-03-07 10:25:24 +01005923 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01005924}
5925
Andrzej Kurek25f27152022-08-17 16:09:31 -04005926#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08005927void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
5928 unsigned char *hash,
5929 size_t *hlen )
5930{
5931#if defined(MBEDTLS_USE_PSA_CRYPTO)
5932 size_t hash_size;
5933 psa_status_t status;
5934 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
5935
5936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
5937 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
5938 if( status != PSA_SUCCESS )
5939 {
5940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5941 return;
5942 }
5943
5944 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
5945 if( status != PSA_SUCCESS )
5946 {
5947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5948 return;
5949 }
5950
5951 *hlen = 32;
5952 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
5953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
5954#else
5955 mbedtls_sha256_context sha256;
5956
5957 mbedtls_sha256_init( &sha256 );
5958
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
5960
5961 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
5962 mbedtls_sha256_finish( &sha256, hash );
5963
5964 *hlen = 32;
5965
5966 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
5967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
5968
5969 mbedtls_sha256_free( &sha256 );
5970#endif /* MBEDTLS_USE_PSA_CRYPTO */
5971 return;
5972}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005973#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08005974
Andrzej Kurek25f27152022-08-17 16:09:31 -04005975#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08005976void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
5977 unsigned char *hash,
5978 size_t *hlen )
5979{
5980#if defined(MBEDTLS_USE_PSA_CRYPTO)
5981 size_t hash_size;
5982 psa_status_t status;
5983 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
5984
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
5986 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
5987 if( status != PSA_SUCCESS )
5988 {
5989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
5990 return;
5991 }
5992
5993 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
5994 if( status != PSA_SUCCESS )
5995 {
5996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
5997 return;
5998 }
5999
6000 *hlen = 48;
6001 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6003#else
6004 mbedtls_sha512_context sha512;
6005
6006 mbedtls_sha512_init( &sha512 );
6007
6008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6009
Andrzej Kureka242e832022-08-11 10:03:14 -04006010 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006011 mbedtls_sha512_finish( &sha512, hash );
6012
6013 *hlen = 48;
6014
6015 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6017
6018 mbedtls_sha512_free( &sha512 );
6019#endif /* MBEDTLS_USE_PSA_CRYPTO */
6020 return;
6021}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006022#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006023
Neil Armstrong80f6f322022-05-03 17:56:38 +02006024#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6025 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006026int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6027{
6028 unsigned char *p = ssl->handshake->premaster;
6029 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6030 const unsigned char *psk = NULL;
6031 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006032 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006033
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006034 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006035 {
6036 /*
6037 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006038 * checked before calling this function.
6039 *
6040 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006041 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006042 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006043 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6044 {
6045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6047 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006048 }
6049
6050 /*
6051 * PMS = struct {
6052 * opaque other_secret<0..2^16-1>;
6053 * opaque psk<0..2^16-1>;
6054 * };
6055 * with "other_secret" depending on the particular key exchange
6056 */
6057#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6058 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6059 {
6060 if( end - p < 2 )
6061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6062
6063 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6064 p += 2;
6065
6066 if( end < p || (size_t)( end - p ) < psk_len )
6067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6068
6069 memset( p, 0, psk_len );
6070 p += psk_len;
6071 }
6072 else
6073#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6074#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6075 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6076 {
6077 /*
6078 * other_secret already set by the ClientKeyExchange message,
6079 * and is 48 bytes long
6080 */
6081 if( end - p < 2 )
6082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6083
6084 *p++ = 0;
6085 *p++ = 48;
6086 p += 48;
6087 }
6088 else
6089#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6090#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6091 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6092 {
6093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6094 size_t len;
6095
6096 /* Write length only when we know the actual value */
6097 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6098 p + 2, end - ( p + 2 ), &len,
6099 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6100 {
6101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6102 return( ret );
6103 }
6104 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6105 p += 2 + len;
6106
6107 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6108 }
6109 else
6110#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006111#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006112 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6113 {
6114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6115 size_t zlen;
6116
6117 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6118 p + 2, end - ( p + 2 ),
6119 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6120 {
6121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6122 return( ret );
6123 }
6124
6125 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6126 p += 2 + zlen;
6127
6128 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6129 MBEDTLS_DEBUG_ECDH_Z );
6130 }
6131 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006132#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006133 {
6134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6136 }
6137
6138 /* opaque psk<0..2^16-1>; */
6139 if( end - p < 2 )
6140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6141
6142 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6143 p += 2;
6144
6145 if( end < p || (size_t)( end - p ) < psk_len )
6146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6147
6148 memcpy( p, psk, psk_len );
6149 p += psk_len;
6150
6151 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6152
6153 return( 0 );
6154}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006155#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006156
6157#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006158MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006159static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6160
6161#if defined(MBEDTLS_SSL_PROTO_DTLS)
6162int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6163{
6164 /* If renegotiation is not enforced, retransmit until we would reach max
6165 * timeout if we were using the usual handshake doubling scheme */
6166 if( ssl->conf->renego_max_records < 0 )
6167 {
6168 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6169 unsigned char doublings = 1;
6170
6171 while( ratio != 0 )
6172 {
6173 ++doublings;
6174 ratio >>= 1;
6175 }
6176
6177 if( ++ssl->renego_records_seen > doublings )
6178 {
6179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6180 return( 0 );
6181 }
6182 }
6183
6184 return( ssl_write_hello_request( ssl ) );
6185}
6186#endif
6187#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006188
Jerry Yud9526692022-02-17 14:23:47 +08006189/*
6190 * Handshake functions
6191 */
6192#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6193/* No certificate support -> dummy functions */
6194int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6195{
6196 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6197 ssl->handshake->ciphersuite_info;
6198
6199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6200
6201 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6202 {
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6204 ssl->state++;
6205 return( 0 );
6206 }
6207
6208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6210}
6211
6212int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6213{
6214 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6215 ssl->handshake->ciphersuite_info;
6216
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6218
6219 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6220 {
6221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6222 ssl->state++;
6223 return( 0 );
6224 }
6225
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6228}
6229
6230#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6231/* Some certificate support -> implement write and parse */
6232
6233int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6234{
6235 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6236 size_t i, n;
6237 const mbedtls_x509_crt *crt;
6238 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6239 ssl->handshake->ciphersuite_info;
6240
6241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6242
6243 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6244 {
6245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6246 ssl->state++;
6247 return( 0 );
6248 }
6249
6250#if defined(MBEDTLS_SSL_CLI_C)
6251 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6252 {
6253 if( ssl->handshake->client_auth == 0 )
6254 {
6255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6256 ssl->state++;
6257 return( 0 );
6258 }
6259 }
6260#endif /* MBEDTLS_SSL_CLI_C */
6261#if defined(MBEDTLS_SSL_SRV_C)
6262 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6263 {
6264 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6265 {
6266 /* Should never happen because we shouldn't have picked the
6267 * ciphersuite if we don't have a certificate. */
6268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6269 }
6270 }
6271#endif
6272
6273 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6274
6275 /*
6276 * 0 . 0 handshake type
6277 * 1 . 3 handshake length
6278 * 4 . 6 length of all certs
6279 * 7 . 9 length of cert. 1
6280 * 10 . n-1 peer certificate
6281 * n . n+2 length of cert. 2
6282 * n+3 . ... upper level cert, etc.
6283 */
6284 i = 7;
6285 crt = mbedtls_ssl_own_cert( ssl );
6286
6287 while( crt != NULL )
6288 {
6289 n = crt->raw.len;
6290 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6291 {
6292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6293 " > %" MBEDTLS_PRINTF_SIZET,
6294 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6295 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6296 }
6297
6298 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6299 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6300 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6301
6302 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6303 i += n; crt = crt->next;
6304 }
6305
6306 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6307 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6308 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6309
6310 ssl->out_msglen = i;
6311 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6312 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6313
6314 ssl->state++;
6315
6316 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6317 {
6318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6319 return( ret );
6320 }
6321
6322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6323
6324 return( ret );
6325}
6326
6327#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6328
6329#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006330MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006331static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6332 unsigned char *crt_buf,
6333 size_t crt_buf_len )
6334{
6335 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6336
6337 if( peer_crt == NULL )
6338 return( -1 );
6339
6340 if( peer_crt->raw.len != crt_buf_len )
6341 return( -1 );
6342
6343 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6344}
6345#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006346MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006347static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6348 unsigned char *crt_buf,
6349 size_t crt_buf_len )
6350{
6351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6352 unsigned char const * const peer_cert_digest =
6353 ssl->session->peer_cert_digest;
6354 mbedtls_md_type_t const peer_cert_digest_type =
6355 ssl->session->peer_cert_digest_type;
6356 mbedtls_md_info_t const * const digest_info =
6357 mbedtls_md_info_from_type( peer_cert_digest_type );
6358 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6359 size_t digest_len;
6360
6361 if( peer_cert_digest == NULL || digest_info == NULL )
6362 return( -1 );
6363
6364 digest_len = mbedtls_md_get_size( digest_info );
6365 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6366 return( -1 );
6367
6368 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6369 if( ret != 0 )
6370 return( -1 );
6371
6372 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6373}
6374#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6375#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6376
6377/*
6378 * Once the certificate message is read, parse it into a cert chain and
6379 * perform basic checks, but leave actual verification to the caller
6380 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006381MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006382static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6383 mbedtls_x509_crt *chain )
6384{
6385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6386#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6387 int crt_cnt=0;
6388#endif
6389 size_t i, n;
6390 uint8_t alert;
6391
6392 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6393 {
6394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6395 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6396 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6397 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6398 }
6399
6400 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6401 {
6402 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6403 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6404 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6405 }
6406
6407 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6408 {
6409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6410 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6411 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6412 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6413 }
6414
6415 i = mbedtls_ssl_hs_hdr_len( ssl );
6416
6417 /*
6418 * Same message structure as in mbedtls_ssl_write_certificate()
6419 */
6420 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6421
6422 if( ssl->in_msg[i] != 0 ||
6423 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6424 {
6425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6426 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6427 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6428 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6429 }
6430
6431 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6432 i += 3;
6433
6434 /* Iterate through and parse the CRTs in the provided chain. */
6435 while( i < ssl->in_hslen )
6436 {
6437 /* Check that there's room for the next CRT's length fields. */
6438 if ( i + 3 > ssl->in_hslen ) {
6439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6440 mbedtls_ssl_send_alert_message( ssl,
6441 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6442 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6443 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6444 }
6445 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6446 * anything beyond 2**16 ~ 64K. */
6447 if( ssl->in_msg[i] != 0 )
6448 {
6449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6450 mbedtls_ssl_send_alert_message( ssl,
6451 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6452 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6453 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6454 }
6455
6456 /* Read length of the next CRT in the chain. */
6457 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6458 | (unsigned int) ssl->in_msg[i + 2];
6459 i += 3;
6460
6461 if( n < 128 || i + n > ssl->in_hslen )
6462 {
6463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6464 mbedtls_ssl_send_alert_message( ssl,
6465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6466 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6467 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6468 }
6469
6470 /* Check if we're handling the first CRT in the chain. */
6471#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6472 if( crt_cnt++ == 0 &&
6473 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6474 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6475 {
6476 /* During client-side renegotiation, check that the server's
6477 * end-CRTs hasn't changed compared to the initial handshake,
6478 * mitigating the triple handshake attack. On success, reuse
6479 * the original end-CRT instead of parsing it again. */
6480 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6481 if( ssl_check_peer_crt_unchanged( ssl,
6482 &ssl->in_msg[i],
6483 n ) != 0 )
6484 {
6485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6486 mbedtls_ssl_send_alert_message( ssl,
6487 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6488 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6489 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6490 }
6491
6492 /* Now we can safely free the original chain. */
6493 ssl_clear_peer_cert( ssl->session );
6494 }
6495#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6496
6497 /* Parse the next certificate in the chain. */
6498#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6499 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6500#else
6501 /* If we don't need to store the CRT chain permanently, parse
6502 * it in-place from the input buffer instead of making a copy. */
6503 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6504#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6505 switch( ret )
6506 {
6507 case 0: /*ok*/
6508 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6509 /* Ignore certificate with an unknown algorithm: maybe a
6510 prior certificate was already trusted. */
6511 break;
6512
6513 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6514 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6515 goto crt_parse_der_failed;
6516
6517 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6519 goto crt_parse_der_failed;
6520
6521 default:
6522 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6523 crt_parse_der_failed:
6524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6525 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6526 return( ret );
6527 }
6528
6529 i += n;
6530 }
6531
6532 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6533 return( 0 );
6534}
6535
6536#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006537MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006538static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6539{
6540 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6541 return( -1 );
6542
6543 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6544 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6545 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6546 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6547 {
Ronald Cron19385882022-06-15 16:26:13 +02006548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006549 return( 0 );
6550 }
6551 return( -1 );
6552}
6553#endif /* MBEDTLS_SSL_SRV_C */
6554
6555/* Check if a certificate message is expected.
6556 * Return either
6557 * - SSL_CERTIFICATE_EXPECTED, or
6558 * - SSL_CERTIFICATE_SKIP
6559 * indicating whether a Certificate message is expected or not.
6560 */
6561#define SSL_CERTIFICATE_EXPECTED 0
6562#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006563MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006564static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6565 int authmode )
6566{
6567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6568 ssl->handshake->ciphersuite_info;
6569
6570 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6571 return( SSL_CERTIFICATE_SKIP );
6572
6573#if defined(MBEDTLS_SSL_SRV_C)
6574 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6575 {
6576 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6577 return( SSL_CERTIFICATE_SKIP );
6578
6579 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6580 {
6581 ssl->session_negotiate->verify_result =
6582 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6583 return( SSL_CERTIFICATE_SKIP );
6584 }
6585 }
6586#else
6587 ((void) authmode);
6588#endif /* MBEDTLS_SSL_SRV_C */
6589
6590 return( SSL_CERTIFICATE_EXPECTED );
6591}
6592
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006593MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006594static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6595 int authmode,
6596 mbedtls_x509_crt *chain,
6597 void *rs_ctx )
6598{
6599 int ret = 0;
6600 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6601 ssl->handshake->ciphersuite_info;
6602 int have_ca_chain = 0;
6603
6604 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6605 void *p_vrfy;
6606
6607 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6608 return( 0 );
6609
6610 if( ssl->f_vrfy != NULL )
6611 {
6612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6613 f_vrfy = ssl->f_vrfy;
6614 p_vrfy = ssl->p_vrfy;
6615 }
6616 else
6617 {
6618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6619 f_vrfy = ssl->conf->f_vrfy;
6620 p_vrfy = ssl->conf->p_vrfy;
6621 }
6622
6623 /*
6624 * Main check: verify certificate
6625 */
6626#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6627 if( ssl->conf->f_ca_cb != NULL )
6628 {
6629 ((void) rs_ctx);
6630 have_ca_chain = 1;
6631
6632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6633 ret = mbedtls_x509_crt_verify_with_ca_cb(
6634 chain,
6635 ssl->conf->f_ca_cb,
6636 ssl->conf->p_ca_cb,
6637 ssl->conf->cert_profile,
6638 ssl->hostname,
6639 &ssl->session_negotiate->verify_result,
6640 f_vrfy, p_vrfy );
6641 }
6642 else
6643#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6644 {
6645 mbedtls_x509_crt *ca_chain;
6646 mbedtls_x509_crl *ca_crl;
6647
6648#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6649 if( ssl->handshake->sni_ca_chain != NULL )
6650 {
6651 ca_chain = ssl->handshake->sni_ca_chain;
6652 ca_crl = ssl->handshake->sni_ca_crl;
6653 }
6654 else
6655#endif
6656 {
6657 ca_chain = ssl->conf->ca_chain;
6658 ca_crl = ssl->conf->ca_crl;
6659 }
6660
6661 if( ca_chain != NULL )
6662 have_ca_chain = 1;
6663
6664 ret = mbedtls_x509_crt_verify_restartable(
6665 chain,
6666 ca_chain, ca_crl,
6667 ssl->conf->cert_profile,
6668 ssl->hostname,
6669 &ssl->session_negotiate->verify_result,
6670 f_vrfy, p_vrfy, rs_ctx );
6671 }
6672
6673 if( ret != 0 )
6674 {
6675 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6676 }
6677
6678#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6679 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6680 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6681#endif
6682
6683 /*
6684 * Secondary checks: always done, but change 'ret' only if it was 0
6685 */
6686
6687#if defined(MBEDTLS_ECP_C)
6688 {
6689 const mbedtls_pk_context *pk = &chain->pk;
6690
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006691 /* If certificate uses an EC key, make sure the curve is OK.
6692 * This is a public key, so it can't be opaque, so can_do() is a good
6693 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006694 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006695 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006696 /* and in the unlikely case the above assumption no longer holds
6697 * we are making sure that pk_ec() here does not return a NULL
6698 */
6699 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6700 if( ec == NULL )
6701 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6704 }
Jerry Yud9526692022-02-17 14:23:47 +08006705
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006706 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6707 {
6708 ssl->session_negotiate->verify_result |=
6709 MBEDTLS_X509_BADCERT_BAD_KEY;
6710
6711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6712 if( ret == 0 )
6713 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6714 }
Jerry Yud9526692022-02-17 14:23:47 +08006715 }
6716 }
6717#endif /* MBEDTLS_ECP_C */
6718
6719 if( mbedtls_ssl_check_cert_usage( chain,
6720 ciphersuite_info,
6721 ! ssl->conf->endpoint,
6722 &ssl->session_negotiate->verify_result ) != 0 )
6723 {
6724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6725 if( ret == 0 )
6726 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6727 }
6728
6729 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6730 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6731 * with details encoded in the verification flags. All other kinds
6732 * of error codes, including those from the user provided f_vrfy
6733 * functions, are treated as fatal and lead to a failure of
6734 * ssl_parse_certificate even if verification was optional. */
6735 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6736 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6737 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6738 {
6739 ret = 0;
6740 }
6741
6742 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6745 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6746 }
6747
6748 if( ret != 0 )
6749 {
6750 uint8_t alert;
6751
6752 /* The certificate may have been rejected for several reasons.
6753 Pick one and send the corresponding alert. Which alert to send
6754 may be a subject of debate in some cases. */
6755 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6756 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6757 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6758 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6759 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6760 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6761 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6762 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6763 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6764 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6765 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6766 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6767 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6768 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6769 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6770 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6771 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6772 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6773 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6774 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6775 else
6776 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6777 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6778 alert );
6779 }
6780
6781#if defined(MBEDTLS_DEBUG_C)
6782 if( ssl->session_negotiate->verify_result != 0 )
6783 {
6784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6785 (unsigned int) ssl->session_negotiate->verify_result ) );
6786 }
6787 else
6788 {
6789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6790 }
6791#endif /* MBEDTLS_DEBUG_C */
6792
6793 return( ret );
6794}
6795
6796#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006797MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006798static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6799 unsigned char *start, size_t len )
6800{
6801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6802 /* Remember digest of the peer's end-CRT. */
6803 ssl->session_negotiate->peer_cert_digest =
6804 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6805 if( ssl->session_negotiate->peer_cert_digest == NULL )
6806 {
6807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6808 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
6809 mbedtls_ssl_send_alert_message( ssl,
6810 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6811 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6812
6813 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6814 }
6815
6816 ret = mbedtls_md( mbedtls_md_info_from_type(
6817 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6818 start, len,
6819 ssl->session_negotiate->peer_cert_digest );
6820
6821 ssl->session_negotiate->peer_cert_digest_type =
6822 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6823 ssl->session_negotiate->peer_cert_digest_len =
6824 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6825
6826 return( ret );
6827}
6828
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006829MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006830static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6831 unsigned char *start, size_t len )
6832{
6833 unsigned char *end = start + len;
6834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6835
6836 /* Make a copy of the peer's raw public key. */
6837 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6838 ret = mbedtls_pk_parse_subpubkey( &start, end,
6839 &ssl->handshake->peer_pubkey );
6840 if( ret != 0 )
6841 {
6842 /* We should have parsed the public key before. */
6843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6844 }
6845
6846 return( 0 );
6847}
6848#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6849
6850int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6851{
6852 int ret = 0;
6853 int crt_expected;
6854#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6855 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6856 ? ssl->handshake->sni_authmode
6857 : ssl->conf->authmode;
6858#else
6859 const int authmode = ssl->conf->authmode;
6860#endif
6861 void *rs_ctx = NULL;
6862 mbedtls_x509_crt *chain = NULL;
6863
6864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6865
6866 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6867 if( crt_expected == SSL_CERTIFICATE_SKIP )
6868 {
6869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6870 goto exit;
6871 }
6872
6873#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6874 if( ssl->handshake->ecrs_enabled &&
6875 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
6876 {
6877 chain = ssl->handshake->ecrs_peer_cert;
6878 ssl->handshake->ecrs_peer_cert = NULL;
6879 goto crt_verify;
6880 }
6881#endif
6882
6883 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
6884 {
6885 /* mbedtls_ssl_read_record may have sent an alert already. We
6886 let it decide whether to alert. */
6887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6888 goto exit;
6889 }
6890
6891#if defined(MBEDTLS_SSL_SRV_C)
6892 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6893 {
6894 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6895
6896 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
6897 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
6898
6899 goto exit;
6900 }
6901#endif /* MBEDTLS_SSL_SRV_C */
6902
6903 /* Clear existing peer CRT structure in case we tried to
6904 * reuse a session but it failed, and allocate a new one. */
6905 ssl_clear_peer_cert( ssl->session_negotiate );
6906
6907 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6908 if( chain == NULL )
6909 {
6910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
6911 sizeof( mbedtls_x509_crt ) ) );
6912 mbedtls_ssl_send_alert_message( ssl,
6913 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6914 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6915
6916 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6917 goto exit;
6918 }
6919 mbedtls_x509_crt_init( chain );
6920
6921 ret = ssl_parse_certificate_chain( ssl, chain );
6922 if( ret != 0 )
6923 goto exit;
6924
6925#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6926 if( ssl->handshake->ecrs_enabled)
6927 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
6928
6929crt_verify:
6930 if( ssl->handshake->ecrs_enabled)
6931 rs_ctx = &ssl->handshake->ecrs_ctx;
6932#endif
6933
6934 ret = ssl_parse_certificate_verify( ssl, authmode,
6935 chain, rs_ctx );
6936 if( ret != 0 )
6937 goto exit;
6938
6939#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6940 {
6941 unsigned char *crt_start, *pk_start;
6942 size_t crt_len, pk_len;
6943
6944 /* We parse the CRT chain without copying, so
6945 * these pointers point into the input buffer,
6946 * and are hence still valid after freeing the
6947 * CRT chain. */
6948
6949 crt_start = chain->raw.p;
6950 crt_len = chain->raw.len;
6951
6952 pk_start = chain->pk_raw.p;
6953 pk_len = chain->pk_raw.len;
6954
6955 /* Free the CRT structures before computing
6956 * digest and copying the peer's public key. */
6957 mbedtls_x509_crt_free( chain );
6958 mbedtls_free( chain );
6959 chain = NULL;
6960
6961 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
6962 if( ret != 0 )
6963 goto exit;
6964
6965 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6966 if( ret != 0 )
6967 goto exit;
6968 }
6969#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6970 /* Pass ownership to session structure. */
6971 ssl->session_negotiate->peer_cert = chain;
6972 chain = NULL;
6973#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6974
6975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
6976
6977exit:
6978
6979 if( ret == 0 )
6980 ssl->state++;
6981
6982#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6983 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6984 {
6985 ssl->handshake->ecrs_peer_cert = chain;
6986 chain = NULL;
6987 }
6988#endif
6989
6990 if( chain != NULL )
6991 {
6992 mbedtls_x509_crt_free( chain );
6993 mbedtls_free( chain );
6994 }
6995
6996 return( ret );
6997}
6998#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6999
Andrzej Kurek25f27152022-08-17 16:09:31 -04007000#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007001static void ssl_calc_finished_tls_sha256(
7002 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7003{
7004 int len = 12;
7005 const char *sender;
7006 unsigned char padbuf[32];
7007#if defined(MBEDTLS_USE_PSA_CRYPTO)
7008 size_t hash_size;
7009 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7010 psa_status_t status;
7011#else
7012 mbedtls_sha256_context sha256;
7013#endif
7014
7015 mbedtls_ssl_session *session = ssl->session_negotiate;
7016 if( !session )
7017 session = ssl->session;
7018
7019 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7020 ? "client finished"
7021 : "server finished";
7022
7023#if defined(MBEDTLS_USE_PSA_CRYPTO)
7024 sha256_psa = psa_hash_operation_init();
7025
7026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7027
7028 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7029 if( status != PSA_SUCCESS )
7030 {
7031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7032 return;
7033 }
7034
7035 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7036 if( status != PSA_SUCCESS )
7037 {
7038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7039 return;
7040 }
7041 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7042#else
7043
7044 mbedtls_sha256_init( &sha256 );
7045
7046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7047
7048 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7049
7050 /*
7051 * TLSv1.2:
7052 * hash = PRF( master, finished_label,
7053 * Hash( handshake ) )[0.11]
7054 */
7055
7056#if !defined(MBEDTLS_SHA256_ALT)
7057 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7058 sha256.state, sizeof( sha256.state ) );
7059#endif
7060
7061 mbedtls_sha256_finish( &sha256, padbuf );
7062 mbedtls_sha256_free( &sha256 );
7063#endif /* MBEDTLS_USE_PSA_CRYPTO */
7064
7065 ssl->handshake->tls_prf( session->master, 48, sender,
7066 padbuf, 32, buf, len );
7067
7068 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7069
7070 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7071
7072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7073}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007074#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007075
Jerry Yub7ba49e2022-02-17 14:25:53 +08007076
Andrzej Kurek25f27152022-08-17 16:09:31 -04007077#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007078static void ssl_calc_finished_tls_sha384(
7079 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7080{
7081 int len = 12;
7082 const char *sender;
7083 unsigned char padbuf[48];
7084#if defined(MBEDTLS_USE_PSA_CRYPTO)
7085 size_t hash_size;
7086 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7087 psa_status_t status;
7088#else
7089 mbedtls_sha512_context sha512;
7090#endif
7091
7092 mbedtls_ssl_session *session = ssl->session_negotiate;
7093 if( !session )
7094 session = ssl->session;
7095
7096 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7097 ? "client finished"
7098 : "server finished";
7099
7100#if defined(MBEDTLS_USE_PSA_CRYPTO)
7101 sha384_psa = psa_hash_operation_init();
7102
7103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7104
7105 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7106 if( status != PSA_SUCCESS )
7107 {
7108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7109 return;
7110 }
7111
7112 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7113 if( status != PSA_SUCCESS )
7114 {
7115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7116 return;
7117 }
7118 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7119#else
7120 mbedtls_sha512_init( &sha512 );
7121
7122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7123
Andrzej Kureka242e832022-08-11 10:03:14 -04007124 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007125
7126 /*
7127 * TLSv1.2:
7128 * hash = PRF( master, finished_label,
7129 * Hash( handshake ) )[0.11]
7130 */
7131
7132#if !defined(MBEDTLS_SHA512_ALT)
7133 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7134 sha512.state, sizeof( sha512.state ) );
7135#endif
7136 mbedtls_sha512_finish( &sha512, padbuf );
7137
7138 mbedtls_sha512_free( &sha512 );
7139#endif
7140
7141 ssl->handshake->tls_prf( session->master, 48, sender,
7142 padbuf, 48, buf, len );
7143
7144 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7145
7146 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7147
7148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7149}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007150#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007151
Jerry Yuaef00152022-02-17 14:27:31 +08007152void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7153{
7154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7155
7156 /*
7157 * Free our handshake params
7158 */
7159 mbedtls_ssl_handshake_free( ssl );
7160 mbedtls_free( ssl->handshake );
7161 ssl->handshake = NULL;
7162
7163 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007164 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007165 */
7166 if( ssl->transform )
7167 {
7168 mbedtls_ssl_transform_free( ssl->transform );
7169 mbedtls_free( ssl->transform );
7170 }
7171 ssl->transform = ssl->transform_negotiate;
7172 ssl->transform_negotiate = NULL;
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7175}
7176
Jerry Yu2a9fff52022-02-17 14:28:51 +08007177void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7178{
7179 int resume = ssl->handshake->resume;
7180
7181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7182
7183#if defined(MBEDTLS_SSL_RENEGOTIATION)
7184 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7185 {
7186 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7187 ssl->renego_records_seen = 0;
7188 }
7189#endif
7190
7191 /*
7192 * Free the previous session and switch in the current one
7193 */
7194 if( ssl->session )
7195 {
7196#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7197 /* RFC 7366 3.1: keep the EtM state */
7198 ssl->session_negotiate->encrypt_then_mac =
7199 ssl->session->encrypt_then_mac;
7200#endif
7201
7202 mbedtls_ssl_session_free( ssl->session );
7203 mbedtls_free( ssl->session );
7204 }
7205 ssl->session = ssl->session_negotiate;
7206 ssl->session_negotiate = NULL;
7207
7208 /*
7209 * Add cache entry
7210 */
7211 if( ssl->conf->f_set_cache != NULL &&
7212 ssl->session->id_len != 0 &&
7213 resume == 0 )
7214 {
7215 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7216 ssl->session->id,
7217 ssl->session->id_len,
7218 ssl->session ) != 0 )
7219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7220 }
7221
7222#if defined(MBEDTLS_SSL_PROTO_DTLS)
7223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7224 ssl->handshake->flight != NULL )
7225 {
7226 /* Cancel handshake timer */
7227 mbedtls_ssl_set_timer( ssl, 0 );
7228
7229 /* Keep last flight around in case we need to resend it:
7230 * we need the handshake and transform structures for that */
7231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7232 }
7233 else
7234#endif
7235 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7236
7237 ssl->state++;
7238
7239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7240}
7241
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007242int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7243{
7244 int ret, hash_len;
7245
7246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7247
7248 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7249
7250 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7251
7252 /*
7253 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7254 * may define some other value. Currently (early 2016), no defined
7255 * ciphersuite does this (and this is unlikely to change as activity has
7256 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7257 */
7258 hash_len = 12;
7259
7260#if defined(MBEDTLS_SSL_RENEGOTIATION)
7261 ssl->verify_data_len = hash_len;
7262 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7263#endif
7264
7265 ssl->out_msglen = 4 + hash_len;
7266 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7267 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7268
7269 /*
7270 * In case of session resuming, invert the client and server
7271 * ChangeCipherSpec messages order.
7272 */
7273 if( ssl->handshake->resume != 0 )
7274 {
7275#if defined(MBEDTLS_SSL_CLI_C)
7276 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7277 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7278#endif
7279#if defined(MBEDTLS_SSL_SRV_C)
7280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7281 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7282#endif
7283 }
7284 else
7285 ssl->state++;
7286
7287 /*
7288 * Switch to our negotiated transform and session parameters for outbound
7289 * data.
7290 */
7291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7292
7293#if defined(MBEDTLS_SSL_PROTO_DTLS)
7294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7295 {
7296 unsigned char i;
7297
7298 /* Remember current epoch settings for resending */
7299 ssl->handshake->alt_transform_out = ssl->transform_out;
7300 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7301 sizeof( ssl->handshake->alt_out_ctr ) );
7302
7303 /* Set sequence_number to zero */
7304 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7305
7306
7307 /* Increment epoch */
7308 for( i = 2; i > 0; i-- )
7309 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7310 break;
7311
7312 /* The loop goes to its end iff the counter is wrapping */
7313 if( i == 0 )
7314 {
7315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7316 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7317 }
7318 }
7319 else
7320#endif /* MBEDTLS_SSL_PROTO_DTLS */
7321 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7322
7323 ssl->transform_out = ssl->transform_negotiate;
7324 ssl->session_out = ssl->session_negotiate;
7325
7326#if defined(MBEDTLS_SSL_PROTO_DTLS)
7327 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7328 mbedtls_ssl_send_flight_completed( ssl );
7329#endif
7330
7331 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7332 {
7333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7334 return( ret );
7335 }
7336
7337#if defined(MBEDTLS_SSL_PROTO_DTLS)
7338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7339 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7340 {
7341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7342 return( ret );
7343 }
7344#endif
7345
7346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7347
7348 return( 0 );
7349}
7350
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007351#define SSL_MAX_HASH_LEN 12
7352
7353int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7354{
7355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7356 unsigned int hash_len = 12;
7357 unsigned char buf[SSL_MAX_HASH_LEN];
7358
7359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7360
7361 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7362
7363 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7364 {
7365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7366 goto exit;
7367 }
7368
7369 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7370 {
7371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7372 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7373 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7374 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7375 goto exit;
7376 }
7377
7378 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7379 {
7380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7381 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7382 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7383 goto exit;
7384 }
7385
7386 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7387 {
7388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7389 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7390 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7391 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7392 goto exit;
7393 }
7394
7395 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7396 buf, hash_len ) != 0 )
7397 {
7398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7400 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7401 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7402 goto exit;
7403 }
7404
7405#if defined(MBEDTLS_SSL_RENEGOTIATION)
7406 ssl->verify_data_len = hash_len;
7407 memcpy( ssl->peer_verify_data, buf, hash_len );
7408#endif
7409
7410 if( ssl->handshake->resume != 0 )
7411 {
7412#if defined(MBEDTLS_SSL_CLI_C)
7413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7414 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7415#endif
7416#if defined(MBEDTLS_SSL_SRV_C)
7417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7418 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7419#endif
7420 }
7421 else
7422 ssl->state++;
7423
7424#if defined(MBEDTLS_SSL_PROTO_DTLS)
7425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7426 mbedtls_ssl_recv_flight_completed( ssl );
7427#endif
7428
7429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7430
7431exit:
7432 mbedtls_platform_zeroize( buf, hash_len );
7433 return( ret );
7434}
7435
Jerry Yu392112c2022-02-17 14:34:10 +08007436#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7437/*
7438 * Helper to get TLS 1.2 PRF from ciphersuite
7439 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7440 */
7441static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7442{
Andrzej Kurek25f27152022-08-17 16:09:31 -04007443#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu392112c2022-02-17 14:34:10 +08007444 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7445 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7446
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007447 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007448 return( tls_prf_sha384 );
7449#else
7450 (void) ciphersuite_id;
7451#endif
7452 return( tls_prf_sha256 );
7453}
7454#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007455
7456static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7457{
7458 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007459#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007460 if( tls_prf == tls_prf_sha384 )
7461 {
7462 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7463 }
7464 else
7465#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007466#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007467 if( tls_prf == tls_prf_sha256 )
7468 {
7469 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7470 }
7471 else
7472#endif
7473 return( MBEDTLS_SSL_TLS_PRF_NONE );
7474}
7475
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007476/*
7477 * Populate a transform structure with session keys and all the other
7478 * necessary information.
7479 *
7480 * Parameters:
7481 * - [in/out]: transform: structure to populate
7482 * [in] must be just initialised with mbedtls_ssl_transform_init()
7483 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7484 * - [in] ciphersuite
7485 * - [in] master
7486 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007487 * - [in] tls_prf: pointer to PRF to use for key derivation
7488 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007489 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007490 * - [in] endpoint: client or server
7491 * - [in] ssl: used for:
7492 * - ssl->conf->{f,p}_export_keys
7493 * [in] optionally used for:
7494 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7495 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007496MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007497static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7498 int ciphersuite,
7499 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007500#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007501 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007502#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007503 ssl_tls_prf_t tls_prf,
7504 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007505 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007506 unsigned endpoint,
7507 const mbedtls_ssl_context *ssl )
7508{
7509 int ret = 0;
7510 unsigned char keyblk[256];
7511 unsigned char *key1;
7512 unsigned char *key2;
7513 unsigned char *mac_enc;
7514 unsigned char *mac_dec;
7515 size_t mac_key_len = 0;
7516 size_t iv_copy_len;
7517 size_t keylen;
7518 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007519 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007520#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007521 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007522 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007523#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007524
7525#if defined(MBEDTLS_USE_PSA_CRYPTO)
7526 psa_key_type_t key_type;
7527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7528 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007529 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007530 size_t key_bits;
7531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7532#endif
7533
7534#if !defined(MBEDTLS_DEBUG_C) && \
7535 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7536 if( ssl->f_export_keys == NULL )
7537 {
7538 ssl = NULL; /* make sure we don't use it except for these cases */
7539 (void) ssl;
7540 }
7541#endif
7542
7543 /*
7544 * Some data just needs copying into the structure
7545 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007546#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007547 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007548#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007549 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007550
7551#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7552 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7553#endif
7554
7555#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007556 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007557 {
7558 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7559 * generation separate. This should never happen. */
7560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7561 }
7562#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7563
7564 /*
7565 * Get various info structures
7566 */
7567 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7568 if( ciphersuite_info == NULL )
7569 {
7570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7571 ciphersuite ) );
7572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7573 }
7574
Neil Armstrongab555e02022-04-04 11:07:59 +02007575 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007576#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007577 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007578#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007579 ciphersuite_info );
7580
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007581 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7582 transform->taglen =
7583 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7584
7585#if defined(MBEDTLS_USE_PSA_CRYPTO)
7586 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7587 transform->taglen,
7588 &alg,
7589 &key_type,
7590 &key_bits ) ) != PSA_SUCCESS )
7591 {
7592 ret = psa_ssl_status_to_mbedtls( status );
7593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7594 goto end;
7595 }
7596#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007597 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7598 if( cipher_info == NULL )
7599 {
7600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7601 ciphersuite_info->cipher ) );
7602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7603 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007604#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007605
Neil Armstronge4512952022-03-08 09:08:22 +01007606#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007607 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007608 if( mac_alg == 0 )
7609 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007611 (unsigned) ciphersuite_info->mac ) );
7612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7613 }
7614#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007615 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7616 if( md_info == NULL )
7617 {
7618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7619 (unsigned) ciphersuite_info->mac ) );
7620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7621 }
Neil Armstronge4512952022-03-08 09:08:22 +01007622#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007623
7624#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7625 /* Copy own and peer's CID if the use of the CID
7626 * extension has been negotiated. */
7627 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7628 {
7629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7630
7631 transform->in_cid_len = ssl->own_cid_len;
7632 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7633 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7634 transform->in_cid_len );
7635
7636 transform->out_cid_len = ssl->handshake->peer_cid_len;
7637 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7638 ssl->handshake->peer_cid_len );
7639 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7640 transform->out_cid_len );
7641 }
7642#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7643
7644 /*
7645 * Compute key block using the PRF
7646 */
7647 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7648 if( ret != 0 )
7649 {
7650 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7651 return( ret );
7652 }
7653
7654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7655 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7656 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7657 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7658 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7659
7660 /*
7661 * Determine the appropriate key, IV and MAC length.
7662 */
7663
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007664#if defined(MBEDTLS_USE_PSA_CRYPTO)
7665 keylen = PSA_BITS_TO_BYTES(key_bits);
7666#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007667 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007668#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007669
7670#if defined(MBEDTLS_GCM_C) || \
7671 defined(MBEDTLS_CCM_C) || \
7672 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007673 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007674 {
7675 size_t explicit_ivlen;
7676
7677 transform->maclen = 0;
7678 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007679
7680 /* All modes haves 96-bit IVs, but the length of the static parts vary
7681 * with mode and version:
7682 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7683 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7684 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7685 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7686 * sequence number).
7687 */
7688 transform->ivlen = 12;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007689#if defined(MBEDTLS_USE_PSA_CRYPTO)
7690 if( key_type == PSA_KEY_TYPE_CHACHA20 )
7691#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007692 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY )
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007693#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007694 transform->fixed_ivlen = 12;
7695 else
7696 transform->fixed_ivlen = 4;
7697
7698 /* Minimum length of encrypted record */
7699 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7700 transform->minlen = explicit_ivlen + transform->taglen;
7701 }
7702 else
7703#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007705 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7706 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7707 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007708 {
Neil Armstronge4512952022-03-08 09:08:22 +01007709#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007710 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007711#else
7712 size_t block_size = cipher_info->block_size;
7713#endif /* MBEDTLS_USE_PSA_CRYPTO */
7714
7715#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007716 /* Get MAC length */
7717 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7718#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007719 /* Initialize HMAC contexts */
7720 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7721 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7722 {
7723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7724 goto end;
7725 }
7726
7727 /* Get MAC length */
7728 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007729#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007730 transform->maclen = mac_key_len;
7731
7732 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007733#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007734 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007735#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007736 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007737#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007738
7739 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007740 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007741 transform->minlen = transform->maclen;
7742 else
7743 {
7744 /*
7745 * GenericBlockCipher:
7746 * 1. if EtM is in use: one block plus MAC
7747 * otherwise: * first multiple of blocklen greater than maclen
7748 * 2. IV
7749 */
7750#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007751 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007752 {
7753 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007754 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007755 }
7756 else
7757#endif
7758 {
7759 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007760 + block_size
7761 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007762 }
7763
Glenn Strauss07c64162022-03-14 12:34:51 -04007764 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007765 {
7766 transform->minlen += transform->ivlen;
7767 }
7768 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007769 {
7770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7771 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7772 goto end;
7773 }
7774 }
7775 }
7776 else
7777#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7778 {
7779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7781 }
7782
7783 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7784 (unsigned) keylen,
7785 (unsigned) transform->minlen,
7786 (unsigned) transform->ivlen,
7787 (unsigned) transform->maclen ) );
7788
7789 /*
7790 * Finally setup the cipher contexts, IVs and MAC secrets.
7791 */
7792#if defined(MBEDTLS_SSL_CLI_C)
7793 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7794 {
7795 key1 = keyblk + mac_key_len * 2;
7796 key2 = keyblk + mac_key_len * 2 + keylen;
7797
7798 mac_enc = keyblk;
7799 mac_dec = keyblk + mac_key_len;
7800
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007801 iv_copy_len = ( transform->fixed_ivlen ) ?
7802 transform->fixed_ivlen : transform->ivlen;
7803 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
7804 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
7805 iv_copy_len );
7806 }
7807 else
7808#endif /* MBEDTLS_SSL_CLI_C */
7809#if defined(MBEDTLS_SSL_SRV_C)
7810 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7811 {
7812 key1 = keyblk + mac_key_len * 2 + keylen;
7813 key2 = keyblk + mac_key_len * 2;
7814
7815 mac_enc = keyblk + mac_key_len;
7816 mac_dec = keyblk;
7817
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007818 iv_copy_len = ( transform->fixed_ivlen ) ?
7819 transform->fixed_ivlen : transform->ivlen;
7820 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
7821 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
7822 iv_copy_len );
7823 }
7824 else
7825#endif /* MBEDTLS_SSL_SRV_C */
7826 {
7827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7828 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7829 goto end;
7830 }
7831
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007832 if( ssl != NULL && ssl->f_export_keys != NULL )
7833 {
7834 ssl->f_export_keys( ssl->p_export_keys,
7835 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
7836 master, 48,
7837 randbytes + 32,
7838 randbytes,
7839 tls_prf_get_type( tls_prf ) );
7840 }
7841
7842#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007843 transform->psa_alg = alg;
7844
7845 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
7846 {
7847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7848 psa_set_key_algorithm( &attributes, alg );
7849 psa_set_key_type( &attributes, key_type );
7850
7851 if( ( status = psa_import_key( &attributes,
7852 key1,
7853 PSA_BITS_TO_BYTES( key_bits ),
7854 &transform->psa_key_enc ) ) != PSA_SUCCESS )
7855 {
7856 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
7857 ret = psa_ssl_status_to_mbedtls( status );
7858 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7859 goto end;
7860 }
7861
7862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7863
7864 if( ( status = psa_import_key( &attributes,
7865 key2,
7866 PSA_BITS_TO_BYTES( key_bits ),
7867 &transform->psa_key_dec ) ) != PSA_SUCCESS )
7868 {
7869 ret = psa_ssl_status_to_mbedtls( status );
7870 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
7871 goto end;
7872 }
7873 }
7874#else
7875 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
7876 cipher_info ) ) != 0 )
7877 {
7878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7879 goto end;
7880 }
7881
7882 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
7883 cipher_info ) ) != 0 )
7884 {
7885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
7886 goto end;
7887 }
7888
7889 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
7890 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7891 MBEDTLS_ENCRYPT ) ) != 0 )
7892 {
7893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7894 goto end;
7895 }
7896
7897 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
7898 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
7899 MBEDTLS_DECRYPT ) ) != 0 )
7900 {
7901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
7902 goto end;
7903 }
7904
7905#if defined(MBEDTLS_CIPHER_MODE_CBC)
7906 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
7907 {
7908 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
7909 MBEDTLS_PADDING_NONE ) ) != 0 )
7910 {
7911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7912 goto end;
7913 }
7914
7915 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
7916 MBEDTLS_PADDING_NONE ) ) != 0 )
7917 {
7918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
7919 goto end;
7920 }
7921 }
7922#endif /* MBEDTLS_CIPHER_MODE_CBC */
7923#endif /* MBEDTLS_USE_PSA_CRYPTO */
7924
Neil Armstrong29c0c042022-03-17 17:47:28 +01007925#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
7926 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
7927 For AEAD-based ciphersuites, there is nothing to do here. */
7928 if( mac_key_len != 0 )
7929 {
7930#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007931 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007932
7933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01007934 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01007935 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
7936
7937 if( ( status = psa_import_key( &attributes,
7938 mac_enc, mac_key_len,
7939 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
7940 {
7941 ret = psa_ssl_status_to_mbedtls( status );
7942 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
7943 goto end;
7944 }
7945
Ronald Cronfb39f152022-03-25 14:36:28 +01007946 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04007947 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
7948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
7949 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
7950#endif
7951 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01007952 /* mbedtls_ct_hmac() requires the key to be exportable */
7953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
7954 PSA_KEY_USAGE_VERIFY_HASH );
7955 else
7956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
7957
7958 if( ( status = psa_import_key( &attributes,
7959 mac_dec, mac_key_len,
7960 &transform->psa_mac_dec ) ) != 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#else
7967 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
7968 if( ret != 0 )
7969 goto end;
7970 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
7971 if( ret != 0 )
7972 goto end;
7973#endif /* MBEDTLS_USE_PSA_CRYPTO */
7974 }
7975#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7976
7977 ((void) mac_dec);
7978 ((void) mac_enc);
7979
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007980end:
7981 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
7982 return( ret );
7983}
7984
Jerry Yuee40f9d2022-02-17 14:55:16 +08007985#if defined(MBEDTLS_USE_PSA_CRYPTO)
7986int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
7987 unsigned char *hash, size_t *hashlen,
7988 unsigned char *data, size_t data_len,
7989 mbedtls_md_type_t md_alg )
7990{
7991 psa_status_t status;
7992 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007993 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08007994
7995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
7996
7997 if( ( status = psa_hash_setup( &hash_operation,
7998 hash_alg ) ) != PSA_SUCCESS )
7999 {
8000 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8001 goto exit;
8002 }
8003
8004 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8005 64 ) ) != PSA_SUCCESS )
8006 {
8007 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8008 goto exit;
8009 }
8010
8011 if( ( status = psa_hash_update( &hash_operation,
8012 data, data_len ) ) != PSA_SUCCESS )
8013 {
8014 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8015 goto exit;
8016 }
8017
8018 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8019 hashlen ) ) != PSA_SUCCESS )
8020 {
8021 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8022 goto exit;
8023 }
8024
8025exit:
8026 if( status != PSA_SUCCESS )
8027 {
8028 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8029 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8030 switch( status )
8031 {
8032 case PSA_ERROR_NOT_SUPPORTED:
8033 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8034 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8035 case PSA_ERROR_BUFFER_TOO_SMALL:
8036 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8037 case PSA_ERROR_INSUFFICIENT_MEMORY:
8038 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8039 default:
8040 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8041 }
8042 }
8043 return( 0 );
8044}
8045
8046#else
8047
8048int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8049 unsigned char *hash, size_t *hashlen,
8050 unsigned char *data, size_t data_len,
8051 mbedtls_md_type_t md_alg )
8052{
8053 int ret = 0;
8054 mbedtls_md_context_t ctx;
8055 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8056 *hashlen = mbedtls_md_get_size( md_info );
8057
8058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8059
8060 mbedtls_md_init( &ctx );
8061
8062 /*
8063 * digitally-signed struct {
8064 * opaque client_random[32];
8065 * opaque server_random[32];
8066 * ServerDHParams params;
8067 * };
8068 */
8069 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8070 {
8071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8072 goto exit;
8073 }
8074 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8075 {
8076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8077 goto exit;
8078 }
8079 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8080 {
8081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8082 goto exit;
8083 }
8084 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8085 {
8086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8087 goto exit;
8088 }
8089 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8090 {
8091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8092 goto exit;
8093 }
8094
8095exit:
8096 mbedtls_md_free( &ctx );
8097
8098 if( ret != 0 )
8099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8100 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8101
8102 return( ret );
8103}
8104#endif /* MBEDTLS_USE_PSA_CRYPTO */
8105
Jerry Yud9d91da2022-02-17 14:57:06 +08008106#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8107
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008108/* Find the preferred hash for a given signature algorithm. */
8109unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8110 mbedtls_ssl_context *ssl,
8111 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008112{
Gabor Mezei078e8032022-04-27 21:17:56 +02008113 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008114 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008115
8116 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008117 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008118
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008119 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008120 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008121 unsigned int hash_alg_received =
8122 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8123 received_sig_algs[i] );
8124 unsigned int sig_alg_received =
8125 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8126 received_sig_algs[i] );
8127
8128 if( sig_alg == sig_alg_received )
8129 {
8130#if defined(MBEDTLS_USE_PSA_CRYPTO)
8131 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8132 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008133 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008134 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008135
Neil Armstrong96eceb82022-06-30 18:05:05 +02008136 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8137 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8138 PSA_ALG_ECDSA( psa_hash_alg ),
8139 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008140 continue;
8141
Neil Armstrong96eceb82022-06-30 18:05:05 +02008142 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008143 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8144 PSA_ALG_RSA_PKCS1V15_SIGN(
8145 psa_hash_alg ),
8146 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008147 continue;
8148 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008149#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008150
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008151 return( hash_alg_received );
8152 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008153 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008154
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008155 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008156}
8157
8158#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8159
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008160/* Serialization of TLS 1.2 sessions:
8161 *
8162 * struct {
8163 * uint64 start_time;
8164 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008165 * uint8 session_id_len; // at most 32
8166 * opaque session_id[32];
8167 * opaque master[48]; // fixed length in the standard
8168 * uint32 verify_result;
8169 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8170 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8171 * uint32 ticket_lifetime;
8172 * uint8 mfl_code; // up to 255 according to standard
8173 * uint8 encrypt_then_mac; // 0 or 1
8174 * } serialized_session_tls12;
8175 *
8176 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008177static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008178 unsigned char *buf,
8179 size_t buf_len )
8180{
8181 unsigned char *p = buf;
8182 size_t used = 0;
8183
8184#if defined(MBEDTLS_HAVE_TIME)
8185 uint64_t start;
8186#endif
8187#if defined(MBEDTLS_X509_CRT_PARSE_C)
8188#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8189 size_t cert_len;
8190#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8191#endif /* MBEDTLS_X509_CRT_PARSE_C */
8192
8193 /*
8194 * Time
8195 */
8196#if defined(MBEDTLS_HAVE_TIME)
8197 used += 8;
8198
8199 if( used <= buf_len )
8200 {
8201 start = (uint64_t) session->start;
8202
8203 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8204 p += 8;
8205 }
8206#endif /* MBEDTLS_HAVE_TIME */
8207
8208 /*
8209 * Basic mandatory fields
8210 */
8211 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008212 + 1 /* id_len */
8213 + sizeof( session->id )
8214 + sizeof( session->master )
8215 + 4; /* verify_result */
8216
8217 if( used <= buf_len )
8218 {
8219 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8220 p += 2;
8221
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008222 *p++ = MBEDTLS_BYTE_0( session->id_len );
8223 memcpy( p, session->id, 32 );
8224 p += 32;
8225
8226 memcpy( p, session->master, 48 );
8227 p += 48;
8228
8229 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8230 p += 4;
8231 }
8232
8233 /*
8234 * Peer's end-entity certificate
8235 */
8236#if defined(MBEDTLS_X509_CRT_PARSE_C)
8237#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8238 if( session->peer_cert == NULL )
8239 cert_len = 0;
8240 else
8241 cert_len = session->peer_cert->raw.len;
8242
8243 used += 3 + cert_len;
8244
8245 if( used <= buf_len )
8246 {
8247 *p++ = MBEDTLS_BYTE_2( cert_len );
8248 *p++ = MBEDTLS_BYTE_1( cert_len );
8249 *p++ = MBEDTLS_BYTE_0( cert_len );
8250
8251 if( session->peer_cert != NULL )
8252 {
8253 memcpy( p, session->peer_cert->raw.p, cert_len );
8254 p += cert_len;
8255 }
8256 }
8257#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8258 if( session->peer_cert_digest != NULL )
8259 {
8260 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8261 if( used <= buf_len )
8262 {
8263 *p++ = (unsigned char) session->peer_cert_digest_type;
8264 *p++ = (unsigned char) session->peer_cert_digest_len;
8265 memcpy( p, session->peer_cert_digest,
8266 session->peer_cert_digest_len );
8267 p += session->peer_cert_digest_len;
8268 }
8269 }
8270 else
8271 {
8272 used += 2;
8273 if( used <= buf_len )
8274 {
8275 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8276 *p++ = 0;
8277 }
8278 }
8279#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8280#endif /* MBEDTLS_X509_CRT_PARSE_C */
8281
8282 /*
8283 * Session ticket if any, plus associated data
8284 */
8285#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8286 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8287
8288 if( used <= buf_len )
8289 {
8290 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8291 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8292 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8293
8294 if( session->ticket != NULL )
8295 {
8296 memcpy( p, session->ticket, session->ticket_len );
8297 p += session->ticket_len;
8298 }
8299
8300 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8301 p += 4;
8302 }
8303#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8304
8305 /*
8306 * Misc extension-related info
8307 */
8308#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8309 used += 1;
8310
8311 if( used <= buf_len )
8312 *p++ = session->mfl_code;
8313#endif
8314
8315#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8316 used += 1;
8317
8318 if( used <= buf_len )
8319 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8320#endif
8321
8322 return( used );
8323}
8324
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008325MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008326static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008327 const unsigned char *buf,
8328 size_t len )
8329{
8330#if defined(MBEDTLS_HAVE_TIME)
8331 uint64_t start;
8332#endif
8333#if defined(MBEDTLS_X509_CRT_PARSE_C)
8334#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8335 size_t cert_len;
8336#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8337#endif /* MBEDTLS_X509_CRT_PARSE_C */
8338
8339 const unsigned char *p = buf;
8340 const unsigned char * const end = buf + len;
8341
8342 /*
8343 * Time
8344 */
8345#if defined(MBEDTLS_HAVE_TIME)
8346 if( 8 > (size_t)( end - p ) )
8347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8348
8349 start = ( (uint64_t) p[0] << 56 ) |
8350 ( (uint64_t) p[1] << 48 ) |
8351 ( (uint64_t) p[2] << 40 ) |
8352 ( (uint64_t) p[3] << 32 ) |
8353 ( (uint64_t) p[4] << 24 ) |
8354 ( (uint64_t) p[5] << 16 ) |
8355 ( (uint64_t) p[6] << 8 ) |
8356 ( (uint64_t) p[7] );
8357 p += 8;
8358
8359 session->start = (time_t) start;
8360#endif /* MBEDTLS_HAVE_TIME */
8361
8362 /*
8363 * Basic mandatory fields
8364 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008365 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8367
8368 session->ciphersuite = ( p[0] << 8 ) | p[1];
8369 p += 2;
8370
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008371 session->id_len = *p++;
8372 memcpy( session->id, p, 32 );
8373 p += 32;
8374
8375 memcpy( session->master, p, 48 );
8376 p += 48;
8377
8378 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8379 ( (uint32_t) p[1] << 16 ) |
8380 ( (uint32_t) p[2] << 8 ) |
8381 ( (uint32_t) p[3] );
8382 p += 4;
8383
8384 /* Immediately clear invalid pointer values that have been read, in case
8385 * we exit early before we replaced them with valid ones. */
8386#if defined(MBEDTLS_X509_CRT_PARSE_C)
8387#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8388 session->peer_cert = NULL;
8389#else
8390 session->peer_cert_digest = NULL;
8391#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8392#endif /* MBEDTLS_X509_CRT_PARSE_C */
8393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8394 session->ticket = NULL;
8395#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8396
8397 /*
8398 * Peer certificate
8399 */
8400#if defined(MBEDTLS_X509_CRT_PARSE_C)
8401#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8402 /* Deserialize CRT from the end of the ticket. */
8403 if( 3 > (size_t)( end - p ) )
8404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8405
8406 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8407 p += 3;
8408
8409 if( cert_len != 0 )
8410 {
8411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8412
8413 if( cert_len > (size_t)( end - p ) )
8414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8415
8416 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8417
8418 if( session->peer_cert == NULL )
8419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8420
8421 mbedtls_x509_crt_init( session->peer_cert );
8422
8423 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8424 p, cert_len ) ) != 0 )
8425 {
8426 mbedtls_x509_crt_free( session->peer_cert );
8427 mbedtls_free( session->peer_cert );
8428 session->peer_cert = NULL;
8429 return( ret );
8430 }
8431
8432 p += cert_len;
8433 }
8434#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8435 /* Deserialize CRT digest from the end of the ticket. */
8436 if( 2 > (size_t)( end - p ) )
8437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8438
8439 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8440 session->peer_cert_digest_len = (size_t) *p++;
8441
8442 if( session->peer_cert_digest_len != 0 )
8443 {
8444 const mbedtls_md_info_t *md_info =
8445 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8446 if( md_info == NULL )
8447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8448 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8450
8451 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8453
8454 session->peer_cert_digest =
8455 mbedtls_calloc( 1, session->peer_cert_digest_len );
8456 if( session->peer_cert_digest == NULL )
8457 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8458
8459 memcpy( session->peer_cert_digest, p,
8460 session->peer_cert_digest_len );
8461 p += session->peer_cert_digest_len;
8462 }
8463#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8464#endif /* MBEDTLS_X509_CRT_PARSE_C */
8465
8466 /*
8467 * Session ticket and associated data
8468 */
8469#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8470 if( 3 > (size_t)( end - p ) )
8471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8472
8473 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8474 p += 3;
8475
8476 if( session->ticket_len != 0 )
8477 {
8478 if( session->ticket_len > (size_t)( end - p ) )
8479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8480
8481 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8482 if( session->ticket == NULL )
8483 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8484
8485 memcpy( session->ticket, p, session->ticket_len );
8486 p += session->ticket_len;
8487 }
8488
8489 if( 4 > (size_t)( end - p ) )
8490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8491
8492 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8493 ( (uint32_t) p[1] << 16 ) |
8494 ( (uint32_t) p[2] << 8 ) |
8495 ( (uint32_t) p[3] );
8496 p += 4;
8497#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8498
8499 /*
8500 * Misc extension-related info
8501 */
8502#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8503 if( 1 > (size_t)( end - p ) )
8504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8505
8506 session->mfl_code = *p++;
8507#endif
8508
8509#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8510 if( 1 > (size_t)( end - p ) )
8511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8512
8513 session->encrypt_then_mac = *p++;
8514#endif
8515
8516 /* Done, should have consumed entire buffer */
8517 if( p != end )
8518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8519
8520 return( 0 );
8521}
Jerry Yudc7bd172022-02-17 13:44:15 +08008522#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8523
XiaokangQian75d40ef2022-04-20 11:05:24 +00008524int mbedtls_ssl_validate_ciphersuite(
8525 const mbedtls_ssl_context *ssl,
8526 const mbedtls_ssl_ciphersuite_t *suite_info,
8527 mbedtls_ssl_protocol_version min_tls_version,
8528 mbedtls_ssl_protocol_version max_tls_version )
8529{
8530 (void) ssl;
8531
8532 if( suite_info == NULL )
8533 return( -1 );
8534
8535 if( ( suite_info->min_tls_version > max_tls_version ) ||
8536 ( suite_info->max_tls_version < min_tls_version ) )
8537 {
8538 return( -1 );
8539 }
8540
XiaokangQian060d8672022-04-21 09:24:56 +00008541#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008542#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8543 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8544 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8545 {
8546 return( -1 );
8547 }
8548#endif
8549
8550 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8551#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8552 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8553 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8554 {
8555 return( -1 );
8556 }
8557#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8558#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8559
8560 return( 0 );
8561}
8562
XiaokangQianeaf36512022-04-24 09:07:44 +00008563#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8564/*
8565 * Function for writing a signature algorithm extension.
8566 *
8567 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8568 * value (TLS 1.3 RFC8446):
8569 * enum {
8570 * ....
8571 * ecdsa_secp256r1_sha256( 0x0403 ),
8572 * ecdsa_secp384r1_sha384( 0x0503 ),
8573 * ecdsa_secp521r1_sha512( 0x0603 ),
8574 * ....
8575 * } SignatureScheme;
8576 *
8577 * struct {
8578 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8579 * } SignatureSchemeList;
8580 *
8581 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8582 * value (TLS 1.2 RFC5246):
8583 * enum {
8584 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8585 * sha512(6), (255)
8586 * } HashAlgorithm;
8587 *
8588 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8589 * SignatureAlgorithm;
8590 *
8591 * struct {
8592 * HashAlgorithm hash;
8593 * SignatureAlgorithm signature;
8594 * } SignatureAndHashAlgorithm;
8595 *
8596 * SignatureAndHashAlgorithm
8597 * supported_signature_algorithms<2..2^16-2>;
8598 *
8599 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8600 * generalization of the TLS 1.2 signature algorithm extension.
8601 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8602 * `SignatureScheme` field of TLS 1.3
8603 *
8604 */
8605int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8606 const unsigned char *end, size_t *out_len )
8607{
8608 unsigned char *p = buf;
8609 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8610 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8611
8612 *out_len = 0;
8613
8614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8615
8616 /* Check if we have space for header and length field:
8617 * - extension_type (2 bytes)
8618 * - extension_data_length (2 bytes)
8619 * - supported_signature_algorithms_length (2 bytes)
8620 */
8621 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8622 p += 6;
8623
8624 /*
8625 * Write supported_signature_algorithms
8626 */
8627 supported_sig_alg = p;
8628 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8629 if( sig_alg == NULL )
8630 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8631
8632 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8633 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8635 *sig_alg,
8636 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008637 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8638 continue;
8639 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8640 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8641 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008643 *sig_alg,
8644 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008645 }
8646
8647 /* Length of supported_signature_algorithms */
8648 supported_sig_alg_len = p - supported_sig_alg;
8649 if( supported_sig_alg_len == 0 )
8650 {
8651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8653 }
8654
XiaokangQianeaf36512022-04-24 09:07:44 +00008655 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008656 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008657 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8658
XiaokangQianeaf36512022-04-24 09:07:44 +00008659 *out_len = p - buf;
8660
8661#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8662 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8663#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8664 return( 0 );
8665}
8666#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8667
XiaokangQian40a35232022-05-07 09:02:40 +00008668#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008669/*
8670 * mbedtls_ssl_parse_server_name_ext
8671 *
8672 * Structure of server_name extension:
8673 *
8674 * enum {
8675 * host_name(0), (255)
8676 * } NameType;
8677 * opaque HostName<1..2^16-1>;
8678 *
8679 * struct {
8680 * NameType name_type;
8681 * select (name_type) {
8682 * case host_name: HostName;
8683 * } name;
8684 * } ServerName;
8685 * struct {
8686 * ServerName server_name_list<1..2^16-1>
8687 * } ServerNameList;
8688 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008689MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00008690int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
8691 const unsigned char *buf,
8692 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00008693{
8694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8695 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008696 size_t server_name_list_len, hostname_len;
8697 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008698
XiaokangQianf2a94202022-05-20 06:44:24 +00008699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00008700
8701 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008702 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008703 p += 2;
8704
XiaokangQian9b2b7712022-05-17 02:57:00 +00008705 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
8706 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00008707 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00008708 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00008709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008710 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00008711 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
8712 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00008713
8714 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
8715 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008716 /* sni_name is intended to be used only during the parsing of the
8717 * ClientHello message (it is reset to NULL before the end of
8718 * the message parsing). Thus it is ok to just point to the
8719 * reception buffer and not make a copy of it.
8720 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008721 ssl->handshake->sni_name = p + 3;
8722 ssl->handshake->sni_name_len = hostname_len;
8723 if( ssl->conf->f_sni == NULL )
8724 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00008725 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00008726 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00008727 if( ret != 0 )
8728 {
XiaokangQianf2a94202022-05-20 06:44:24 +00008729 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00008730 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8731 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00008732 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
8733 }
8734 return( 0 );
8735 }
8736
8737 p += hostname_len + 3;
8738 }
8739
8740 return( 0 );
8741}
8742#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8743
XiaokangQianacb39922022-06-17 10:18:48 +00008744#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008745MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00008746int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
8747 const unsigned char *buf,
8748 const unsigned char *end )
8749{
8750 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008751 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008752 const unsigned char *protocol_name_list;
8753 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008754 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008755
8756 /* If ALPN not configured, just ignore the extension */
8757 if( ssl->conf->alpn_list == NULL )
8758 return( 0 );
8759
8760 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008761 * RFC7301, section 3.1
8762 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008763 *
XiaokangQianc7403452022-06-23 03:24:12 +00008764 * struct {
8765 * ProtocolName protocol_name_list<2..2^16-1>
8766 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008767 */
8768
XiaokangQianc7403452022-06-23 03:24:12 +00008769 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008770 * protocol_name_list_len 2 bytes
8771 * protocol_name_len 1 bytes
8772 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008773 */
XiaokangQianacb39922022-06-17 10:18:48 +00008774 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
8775
XiaokangQianc7403452022-06-23 03:24:12 +00008776 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00008777 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00008778 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00008779 protocol_name_list = p;
8780 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008781
8782 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00008783 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008784 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008785 protocol_name_len = *p++;
8786 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
8787 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00008788 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00008789 {
8790 MBEDTLS_SSL_PEND_FATAL_ALERT(
8791 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
8792 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00008793 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00008794 }
8795
8796 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008797 }
8798
8799 /* Use our order of preference */
8800 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
8801 {
8802 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00008803 p = protocol_name_list;
8804 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00008805 {
XiaokangQian95d5f542022-06-24 02:29:26 +00008806 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00008807 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00008808 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00008809 {
8810 ssl->alpn_chosen = *alpn;
8811 return( 0 );
8812 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008813
8814 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008815 }
8816 }
8817
XiaokangQian95d5f542022-06-24 02:29:26 +00008818 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008819 MBEDTLS_SSL_PEND_FATAL_ALERT(
8820 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8821 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8822 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
8823}
8824
8825int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
8826 unsigned char *buf,
8827 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00008828 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00008829{
8830 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008831 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008832 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008833
8834 if( ssl->alpn_chosen == NULL )
8835 {
8836 return( 0 );
8837 }
8838
XiaokangQian95d5f542022-06-24 02:29:26 +00008839 protocol_name_len = strlen( ssl->alpn_chosen );
8840 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008841
8842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
8843 /*
8844 * 0 . 1 ext identifier
8845 * 2 . 3 ext length
8846 * 4 . 5 protocol list length
8847 * 6 . 6 protocol name length
8848 * 7 . 7+n protocol name
8849 */
8850 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
8851
XiaokangQian95d5f542022-06-24 02:29:26 +00008852 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008853
XiaokangQian95d5f542022-06-24 02:29:26 +00008854 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
8855 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00008856 /* Note: the length of the chosen protocol has been checked to be less
8857 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8858 */
XiaokangQian95d5f542022-06-24 02:29:26 +00008859 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008860
XiaokangQian95d5f542022-06-24 02:29:26 +00008861 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00008862 return ( 0 );
8863}
8864#endif /* MBEDTLS_SSL_ALPN */
8865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#endif /* MBEDTLS_SSL_TLS_C */