blob: 7b51040c460ab488999d967b26b3a821afd27a9c [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#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
524void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
526{
527 ((void) ciphersuite_info);
528
Andrzej Kurek25f27152022-08-17 16:09:31 -0400529#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800530 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
531 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
532 else
533#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400534#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800535 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
536 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
537 else
538#endif
539 {
540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
541 return;
542 }
543}
544
XiaokangQianadab9a62022-07-18 07:41:26 +0000545void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
546 unsigned hs_type,
547 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100548{
549 unsigned char hs_hdr[4];
550
551 /* Build HS header for checksum update. */
552 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
553 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
554 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
555 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
556
557 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
558}
559
560void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
561 unsigned hs_type,
562 unsigned char const *msg,
563 size_t msg_len )
564{
565 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
566 ssl->handshake->update_checksum( ssl, msg, msg_len );
567}
568
Jerry Yuc73c6182022-02-08 20:29:25 +0800569void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
570{
571 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400572#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800573#if defined(MBEDTLS_USE_PSA_CRYPTO)
574 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
575 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
576#else
577 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
578#endif
579#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400580#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800581#if defined(MBEDTLS_USE_PSA_CRYPTO)
582 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
583 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
584#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400585 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800586#endif
587#endif
588}
589
590static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
591 const unsigned char *buf, size_t len )
592{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400593#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800594#if defined(MBEDTLS_USE_PSA_CRYPTO)
595 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
596#else
597 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
598#endif
599#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400600#if defined(MBEDTLS_HAS_ALG_SHA_384_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_sha384_psa, buf, len );
603#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400604 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800605#endif
606#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400607#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
608 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
609 (void) ssl;
610 (void) buf;
611 (void) len;
612#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800613}
614
Andrzej Kurek25f27152022-08-17 16:09:31 -0400615#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800616static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
617 const unsigned char *buf, size_t len )
618{
619#if defined(MBEDTLS_USE_PSA_CRYPTO)
620 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
621#else
622 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
623#endif
624}
625#endif
626
Andrzej Kurek25f27152022-08-17 16:09:31 -0400627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800628static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
629 const unsigned char *buf, size_t len )
630{
631#if defined(MBEDTLS_USE_PSA_CRYPTO)
632 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
633#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400634 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800635#endif
636}
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200642
Andrzej Kurek25f27152022-08-17 16:09:31 -0400643#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500644#if defined(MBEDTLS_USE_PSA_CRYPTO)
645 handshake->fin_sha256_psa = psa_hash_operation_init();
646 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200649 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200650#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500651#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400652#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500654 handshake->fin_sha384_psa = psa_hash_operation_init();
655 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500656#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400657 mbedtls_sha512_init( &handshake->fin_sha384 );
658 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200659#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500660#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200661
662 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_DHM_C)
665 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200666#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200667#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200669#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200670#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200671#if defined(MBEDTLS_USE_PSA_CRYPTO)
672 handshake->psa_pake_ctx = psa_pake_operation_init();
673 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
674#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200675 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200677#if defined(MBEDTLS_SSL_CLI_C)
678 handshake->ecjpake_cache = NULL;
679 handshake->ecjpake_cache_len = 0;
680#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200681#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200682
Gilles Peskineeccd8882020-03-10 12:19:08 +0100683#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200684 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200685#endif
686
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200687#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
688 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
689#endif
Hanno Becker75173122019-02-06 16:18:31 +0000690
691#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
692 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
693 mbedtls_pk_init( &handshake->peer_pubkey );
694#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200695}
696
Hanno Beckera18d1322018-01-03 14:27:32 +0000697void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200700
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100701#if defined(MBEDTLS_USE_PSA_CRYPTO)
702 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
703 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100704#else
705 mbedtls_cipher_init( &transform->cipher_ctx_enc );
706 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100707#endif
708
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000709#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100710#if defined(MBEDTLS_USE_PSA_CRYPTO)
711 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
712 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100713#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_init( &transform->md_ctx_enc );
715 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000716#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100717#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200718}
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200723}
724
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200725MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000727{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200728 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000729 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200731 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200733 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200734 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200735
736 /*
737 * Either the pointers are now NULL or cleared properly and can be freed.
738 * Now allocate missing structures.
739 */
740 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200741 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200745 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200746 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 }
Paul Bakker48916f92012-09-16 19:57:18 +0000749
Paul Bakker82788fb2014-10-20 13:59:19 +0200750 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200751 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200752 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200753 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500754#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
755 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400756
Andrzej Kurek4a063792020-10-21 15:08:44 +0200757 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
758 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500759#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000760
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200761 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000762 if( ssl->handshake == NULL ||
763 ssl->transform_negotiate == NULL ||
764 ssl->session_negotiate == NULL )
765 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_free( ssl->handshake );
769 mbedtls_free( ssl->transform_negotiate );
770 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200771
772 ssl->handshake = NULL;
773 ssl->transform_negotiate = NULL;
774 ssl->session_negotiate = NULL;
775
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +0000777 }
778
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200779 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +0000781 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +0200782 ssl_handshake_params_init( ssl->handshake );
783
Jerry Yud0766ec2022-09-22 10:46:57 +0800784#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
785 defined(MBEDTLS_SSL_SRV_C) && \
786 defined(MBEDTLS_SSL_SESSION_TICKETS)
787 ssl->handshake->new_session_tickets_count =
788 ssl->conf->new_session_tickets_count ;
789#endif
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
793 {
794 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200795
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
797 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
798 else
799 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200800
Hanno Becker0f57a652020-02-05 10:37:26 +0000801 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200802 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +0200803#endif
804
Brett Warrene0edc842021-08-17 09:53:13 +0100805/*
806 * curve_list is translated to IANA TLS group identifiers here because
807 * mbedtls_ssl_conf_curves returns void and so can't return
808 * any error codes.
809 */
810#if defined(MBEDTLS_ECP_C)
811#if !defined(MBEDTLS_DEPRECATED_REMOVED)
812 /* Heap allocate and translate curve_list from internal to IANA group ids */
813 if ( ssl->conf->curve_list != NULL )
814 {
815 size_t length;
816 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
817
818 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
819 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
820
821 /* Leave room for zero termination */
822 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
823 if ( group_list == NULL )
824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
825
826 for( size_t i = 0; i < length; i++ )
827 {
828 const mbedtls_ecp_curve_info *info =
829 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
830 if ( info == NULL )
831 {
832 mbedtls_free( group_list );
833 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
834 }
835 group_list[i] = info->tls_id;
836 }
837
838 group_list[length] = 0;
839
840 ssl->handshake->group_list = group_list;
841 ssl->handshake->group_list_heap_allocated = 1;
842 }
843 else
844 {
845 ssl->handshake->group_list = ssl->conf->group_list;
846 ssl->handshake->group_list_heap_allocated = 0;
847 }
848#endif /* MBEDTLS_DEPRECATED_REMOVED */
849#endif /* MBEDTLS_ECP_C */
850
Ronald Crone68ab4f2022-10-05 12:46:29 +0200851#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +0800852#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +0800853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +0800854 /* Heap allocate and translate sig_hashes from internal hash identifiers to
855 signature algorithms IANA identifiers. */
856 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +0800857 ssl->conf->sig_hashes != NULL )
858 {
859 const int *md;
860 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +0800861 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +0800862 uint16_t *p;
863
Jerry Yub476a442022-01-21 18:14:45 +0800864#if defined(static_assert)
865 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
866 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
867 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +0800868#endif
869
Jerry Yuf017ee42022-01-12 15:49:48 +0800870 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
871 {
872 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
873 continue;
Jerry Yub476a442022-01-21 18:14:45 +0800874#if defined(MBEDTLS_ECDSA_C)
875 sig_algs_len += sizeof( uint16_t );
876#endif
Jerry Yua68dca22022-01-20 16:28:27 +0800877
Jerry Yub476a442022-01-21 18:14:45 +0800878#if defined(MBEDTLS_RSA_C)
879 sig_algs_len += sizeof( uint16_t );
880#endif
881 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
882 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +0800883 }
884
Jerry Yub476a442022-01-21 18:14:45 +0800885 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +0800886 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
887
Jerry Yub476a442022-01-21 18:14:45 +0800888 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
889 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +0800890 if( ssl->handshake->sig_algs == NULL )
891 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
892
893 p = (uint16_t *)ssl->handshake->sig_algs;
894 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
895 {
896 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
897 if( hash == MBEDTLS_SSL_HASH_NONE )
898 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800899#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800900 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
901 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800902#endif
903#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +0800904 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
905 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +0800906#endif
Jerry Yuf017ee42022-01-12 15:49:48 +0800907 }
Gabor Mezei15b95a62022-05-09 16:37:58 +0200908 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +0800909 ssl->handshake->sig_algs_heap_allocated = 1;
910 }
911 else
Jerry Yua69269a2022-01-17 21:06:01 +0800912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +0800913 {
Jerry Yuf017ee42022-01-12 15:49:48 +0800914 ssl->handshake->sig_algs_heap_allocated = 0;
915 }
Jerry Yucc539102022-06-27 16:27:35 +0800916#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +0200917#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +0000918 return( 0 );
919}
920
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200921#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200922/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200923MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200924static int ssl_cookie_write_dummy( void *ctx,
925 unsigned char **p, unsigned char *end,
926 const unsigned char *cli_id, size_t cli_id_len )
927{
928 ((void) ctx);
929 ((void) p);
930 ((void) end);
931 ((void) cli_id);
932 ((void) cli_id_len);
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200935}
936
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200937MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200938static int ssl_cookie_check_dummy( void *ctx,
939 const unsigned char *cookie, size_t cookie_len,
940 const unsigned char *cli_id, size_t cli_id_len )
941{
942 ((void) ctx);
943 ((void) cookie);
944 ((void) cookie_len);
945 ((void) cli_id);
946 ((void) cli_id_len);
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200949}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +0200950#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +0200951
Paul Bakker5121ce52009-01-03 21:22:43 +0000952/*
953 * Initialize an SSL context
954 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200955void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
956{
957 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
958}
959
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +0800961static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
962{
Ronald Cron086ee0b2022-03-15 15:18:51 +0100963 const mbedtls_ssl_config *conf = ssl->conf;
964
Ronald Cron6f135e12021-12-08 16:57:54 +0100965#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100966 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
967 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000968 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
969 {
970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
971 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
972 }
973
Jerry Yu60835a82021-08-04 10:13:52 +0800974 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
975 return( 0 );
976 }
977#endif
978
979#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100980 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800981 {
982 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
983 return( 0 );
984 }
985#endif
986
Ronald Cron6f135e12021-12-08 16:57:54 +0100987#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +0100988 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +0800989 {
XiaokangQianed582dd2022-04-13 08:21:05 +0000990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
991 {
992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
993 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
994 }
995
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000996 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
997 {
998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
999 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1000 }
1001
1002
Ronald Crone1d3f062022-02-10 14:50:54 +01001003 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1004 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001005 }
1006#endif
1007
1008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1009 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1010}
1011
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001013static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1014{
1015 int ret;
1016 ret = ssl_conf_version_check( ssl );
1017 if( ret != 0 )
1018 return( ret );
1019
Jerry Yudef7ae42022-10-30 14:13:19 +08001020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1021 /* RFC 8446 section 4.4.3
1022 *
1023 * If the verification fails, the receiver MUST terminate the handshake with
1024 * a "decrypt_error" alert.
1025 *
1026 * If the client is configured as TLS 1.3 only with optional verify, return
1027 * bad config.
1028 *
1029 */
1030 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1031 (mbedtls_ssl_context *)ssl ) &&
1032 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1033 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1034 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1035 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1036 {
1037 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001038 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001039 "is not available for TLS 1.3 client" ) );
1040 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1041 }
1042#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1043
Jerry Yu60835a82021-08-04 10:13:52 +08001044 /* Space for further checks */
1045
1046 return( 0 );
1047}
1048
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001049/*
1050 * Setup an SSL context
1051 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001052
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001053int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001054 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055{
Janos Follath865b3eb2019-12-16 11:46:15 +00001056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001057 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1058 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001060 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001061
Jerry Yu60835a82021-08-04 10:13:52 +08001062 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1063 return( ret );
1064
Paul Bakker62f2dee2012-09-28 07:31:51 +00001065 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001066 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001067 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001068
1069 /* Set to NULL in case of an error condition */
1070 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001071
Darryl Greenb33cc762019-11-28 14:29:44 +00001072#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1073 ssl->in_buf_len = in_buf_len;
1074#endif
1075 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001076 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001079 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001080 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001081 }
1082
Darryl Greenb33cc762019-11-28 14:29:44 +00001083#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1084 ssl->out_buf_len = out_buf_len;
1085#endif
1086 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001087 if( ssl->out_buf == NULL )
1088 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001090 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001091 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 }
1093
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001094 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001095
Johan Pascalb62bb512015-12-03 21:56:45 +01001096#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001097 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001098#endif
1099
Paul Bakker48916f92012-09-16 19:57:18 +00001100 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001101 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
1103 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001104
1105error:
1106 mbedtls_free( ssl->in_buf );
1107 mbedtls_free( ssl->out_buf );
1108
1109 ssl->conf = NULL;
1110
Darryl Greenb33cc762019-11-28 14:29:44 +00001111#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1112 ssl->in_buf_len = 0;
1113 ssl->out_buf_len = 0;
1114#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001115 ssl->in_buf = NULL;
1116 ssl->out_buf = NULL;
1117
1118 ssl->in_hdr = NULL;
1119 ssl->in_ctr = NULL;
1120 ssl->in_len = NULL;
1121 ssl->in_iv = NULL;
1122 ssl->in_msg = NULL;
1123
1124 ssl->out_hdr = NULL;
1125 ssl->out_ctr = NULL;
1126 ssl->out_len = NULL;
1127 ssl->out_iv = NULL;
1128 ssl->out_msg = NULL;
1129
k-stachowiak9f7798e2018-07-31 16:52:32 +02001130 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131}
1132
1133/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001134 * Reset an initialized and used SSL context for re-use while retaining
1135 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001136 *
1137 * If partial is non-zero, keep data in the input buffer and client ID.
1138 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001139 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001140void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1141 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001142{
Darryl Greenb33cc762019-11-28 14:29:44 +00001143#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1144 size_t in_buf_len = ssl->in_buf_len;
1145 size_t out_buf_len = ssl->out_buf_len;
1146#else
1147 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1148 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1149#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001150
Hanno Beckerb0302c42021-08-03 09:39:42 +01001151#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1152 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001153#endif
1154
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001155 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001156 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001157
Hanno Beckerb0302c42021-08-03 09:39:42 +01001158 mbedtls_ssl_reset_in_out_pointers( ssl );
1159
1160 /* Reset incoming message parsing */
1161 ssl->in_offt = NULL;
1162 ssl->nb_zero = 0;
1163 ssl->in_msgtype = 0;
1164 ssl->in_msglen = 0;
1165 ssl->in_hslen = 0;
1166 ssl->keep_current_message = 0;
1167 ssl->transform_in = NULL;
1168
1169#if defined(MBEDTLS_SSL_PROTO_DTLS)
1170 ssl->next_record_offset = 0;
1171 ssl->in_epoch = 0;
1172#endif
1173
1174 /* Keep current datagram if partial == 1 */
1175 if( partial == 0 )
1176 {
1177 ssl->in_left = 0;
1178 memset( ssl->in_buf, 0, in_buf_len );
1179 }
1180
Ronald Cronad8c17b2022-06-10 17:18:09 +02001181 ssl->send_alert = 0;
1182
Hanno Beckerb0302c42021-08-03 09:39:42 +01001183 /* Reset outgoing message writing */
1184 ssl->out_msgtype = 0;
1185 ssl->out_msglen = 0;
1186 ssl->out_left = 0;
1187 memset( ssl->out_buf, 0, out_buf_len );
1188 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1189 ssl->transform_out = NULL;
1190
1191#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1192 mbedtls_ssl_dtls_replay_reset( ssl );
1193#endif
1194
XiaokangQian2b01dc32022-01-21 02:53:13 +00001195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001196 if( ssl->transform )
1197 {
1198 mbedtls_ssl_transform_free( ssl->transform );
1199 mbedtls_free( ssl->transform );
1200 ssl->transform = NULL;
1201 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1203
XiaokangQian2b01dc32022-01-21 02:53:13 +00001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1205 mbedtls_ssl_transform_free( ssl->transform_application );
1206 mbedtls_free( ssl->transform_application );
1207 ssl->transform_application = NULL;
1208
1209 if( ssl->handshake != NULL )
1210 {
1211 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1212 mbedtls_free( ssl->handshake->transform_earlydata );
1213 ssl->handshake->transform_earlydata = NULL;
1214
1215 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1216 mbedtls_free( ssl->handshake->transform_handshake );
1217 ssl->handshake->transform_handshake = NULL;
1218 }
1219
XiaokangQian2b01dc32022-01-21 02:53:13 +00001220#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001221}
1222
1223int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1224{
1225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1226
1227 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1228
XiaokangQian78b1fa72022-01-19 06:56:30 +00001229 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001230
1231 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_SSL_RENEGOTIATION)
1233 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001234 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001235
1236 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1238 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001241
Hanno Beckerb0302c42021-08-03 09:39:42 +01001242 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001243 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001244 if( ssl->session )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_ssl_session_free( ssl->session );
1247 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001248 ssl->session = NULL;
1249 }
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001252 ssl->alpn_chosen = NULL;
1253#endif
1254
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001255#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001256 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001257#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001258 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001259#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001260 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001261 {
1262 mbedtls_free( ssl->cli_id );
1263 ssl->cli_id = NULL;
1264 ssl->cli_id_len = 0;
1265 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001266#endif
1267
Paul Bakker48916f92012-09-16 19:57:18 +00001268 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1269 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001270
1271 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001272}
1273
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001274/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001275 * Reset an initialized and used SSL context for re-use while retaining
1276 * all application-set variables, function pointers and data.
1277 */
1278int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1279{
Hanno Becker43aefe22020-02-05 10:44:56 +00001280 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001281}
1282
1283/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 * SSL set accessors
1285 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001286void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001288 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289}
1290
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001291void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001293 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001294}
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001297void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001299 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001300}
1301#endif
1302
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001303void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001304{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001305 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001306}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001309
Hanno Becker1841b0a2018-08-24 11:13:57 +01001310void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1311 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001312{
1313 ssl->disable_datagram_packing = !allow_packing;
1314}
1315
1316void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1317 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001318{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001319 conf->hs_timeout_min = min;
1320 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001321}
1322#endif
1323
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001324void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001326 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001327}
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001330void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001331 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001332 void *p_vrfy )
1333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001334 conf->f_vrfy = f_vrfy;
1335 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001338
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001339void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001340 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 void *p_rng )
1342{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001343 conf->f_rng = f_rng;
1344 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345}
1346
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001347void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001348 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 void *p_dbg )
1350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001351 conf->f_dbg = f_dbg;
1352 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353}
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001356 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001357 mbedtls_ssl_send_t *f_send,
1358 mbedtls_ssl_recv_t *f_recv,
1359 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001360{
1361 ssl->p_bio = p_bio;
1362 ssl->f_send = f_send;
1363 ssl->f_recv = f_recv;
1364 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001365}
1366
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001367#if defined(MBEDTLS_SSL_PROTO_DTLS)
1368void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1369{
1370 ssl->mtu = mtu;
1371}
1372#endif
1373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001374void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001375{
1376 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001377}
1378
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001379void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1380 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001381 mbedtls_ssl_set_timer_t *f_set_timer,
1382 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001383{
1384 ssl->p_timer = p_timer;
1385 ssl->f_set_timer = f_set_timer;
1386 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001387
1388 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001389 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001390}
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001393void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001394 void *p_cache,
1395 mbedtls_ssl_cache_get_t *f_get_cache,
1396 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001398 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001399 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001400 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_SSL_CLI_C)
1405int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406{
Janos Follath865b3eb2019-12-16 11:46:15 +00001407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001408
1409 if( ssl == NULL ||
1410 session == NULL ||
1411 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001412 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001415 }
1416
Hanno Beckere810bbc2021-05-14 16:01:05 +01001417 if( ssl->handshake->resume == 1 )
1418 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1419
Jerry Yu21092062022-10-10 21:21:31 +08001420#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1421 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001422 {
Jerry Yu21092062022-10-10 21:21:31 +08001423 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1424 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1425
1426 if( mbedtls_ssl_validate_ciphersuite(
1427 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1428 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1429 {
1430 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1431 session->ciphersuite ) );
1432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1433 }
Jerry Yu40afab62022-10-08 10:42:13 +08001434 }
Jerry Yu21092062022-10-10 21:21:31 +08001435#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001436
Hanno Becker52055ae2019-02-06 14:30:46 +00001437 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1438 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001439 return( ret );
1440
Paul Bakker0a597072012-09-25 21:55:46 +00001441 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001442
1443 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001447void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1448 const int *ciphersuites )
1449{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001450 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001451}
1452
Ronald Cron6f135e12021-12-08 16:57:54 +01001453#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001454void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001455 const int kex_modes )
1456{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001457 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001458}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001459
1460#if defined(MBEDTLS_SSL_EARLY_DATA)
1461void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001462 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001463{
1464 conf->early_data_enabled = early_data_enabled;
1465}
1466#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001467#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001470void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001471 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001472{
1473 conf->cert_profile = profile;
1474}
1475
Glenn Strauss36872db2022-01-22 05:06:31 -05001476static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1477{
1478 mbedtls_ssl_key_cert *cur = key_cert, *next;
1479
1480 while( cur != NULL )
1481 {
1482 next = cur->next;
1483 mbedtls_free( cur );
1484 cur = next;
1485 }
1486}
1487
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001488/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001489MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001490static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1491 mbedtls_x509_crt *cert,
1492 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001493{
niisato8ee24222018-06-25 19:05:48 +09001494 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001495
Glenn Strauss36872db2022-01-22 05:06:31 -05001496 if( cert == NULL )
1497 {
1498 /* Free list if cert is null */
1499 ssl_key_cert_free( *head );
1500 *head = NULL;
1501 return( 0 );
1502 }
1503
niisato8ee24222018-06-25 19:05:48 +09001504 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1505 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001507
niisato8ee24222018-06-25 19:05:48 +09001508 new_cert->cert = cert;
1509 new_cert->key = key;
1510 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001511
Glenn Strauss36872db2022-01-22 05:06:31 -05001512 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001513 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001514 {
niisato8ee24222018-06-25 19:05:48 +09001515 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001516 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001517 else
1518 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001519 mbedtls_ssl_key_cert *cur = *head;
1520 while( cur->next != NULL )
1521 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001522 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001523 }
1524
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001525 return( 0 );
1526}
1527
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001528int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001529 mbedtls_x509_crt *own_cert,
1530 mbedtls_pk_context *pk_key )
1531{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001532 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001533}
1534
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001535void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001536 mbedtls_x509_crt *ca_chain,
1537 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001538{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001539 conf->ca_chain = ca_chain;
1540 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001541
1542#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1543 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1544 * cannot be used together. */
1545 conf->f_ca_cb = NULL;
1546 conf->p_ca_cb = NULL;
1547#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001548}
Hanno Becker5adaad92019-03-27 16:54:37 +00001549
1550#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1551void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001552 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001553 void *p_ca_cb )
1554{
1555 conf->f_ca_cb = f_ca_cb;
1556 conf->p_ca_cb = p_ca_cb;
1557
1558 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1559 * cannot be used together. */
1560 conf->ca_chain = NULL;
1561 conf->ca_crl = NULL;
1562}
1563#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001565
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001566#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001567const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1568 size_t *name_len )
1569{
1570 *name_len = ssl->handshake->sni_name_len;
1571 return( ssl->handshake->sni_name );
1572}
1573
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001574int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1575 mbedtls_x509_crt *own_cert,
1576 mbedtls_pk_context *pk_key )
1577{
1578 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1579 own_cert, pk_key ) );
1580}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001581
1582void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1583 mbedtls_x509_crt *ca_chain,
1584 mbedtls_x509_crl *ca_crl )
1585{
1586 ssl->handshake->sni_ca_chain = ca_chain;
1587 ssl->handshake->sni_ca_crl = ca_crl;
1588}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001589
Glenn Strauss999ef702022-03-11 01:37:23 -05001590#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1591void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1592 const mbedtls_x509_crt *crt)
1593{
1594 ssl->handshake->dn_hints = crt;
1595}
1596#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1597
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001598void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1599 int authmode )
1600{
1601 ssl->handshake->sni_authmode = authmode;
1602}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001603#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1604
Hanno Becker8927c832019-04-03 12:52:50 +01001605#if defined(MBEDTLS_X509_CRT_PARSE_C)
1606void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1607 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1608 void *p_vrfy )
1609{
1610 ssl->f_vrfy = f_vrfy;
1611 ssl->p_vrfy = p_vrfy;
1612}
1613#endif
1614
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001615#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001616/*
1617 * Set EC J-PAKE password for current handshake
1618 */
Valerio Setti02c25b52022-11-15 14:08:42 +01001619#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001620int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1621 const unsigned char *pw,
1622 size_t pw_len )
1623{
Neil Armstrongca7d5062022-05-31 14:43:23 +02001624 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1626 psa_pake_role_t psa_role;
1627 psa_status_t status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001628
Janos Follath8eb64132016-06-03 15:40:57 +01001629 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1631
Neil Armstrongca7d5062022-05-31 14:43:23 +02001632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1633 psa_role = PSA_PAKE_ROLE_SERVER;
1634 else
1635 psa_role = PSA_PAKE_ROLE_CLIENT;
1636
Valerio Settiaca21b72022-11-17 18:17:01 +01001637 /* Empty password is not valid */
1638 if( ( pw == NULL) || ( pw_len == 0 ) )
1639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001640
Valerio Settiaca21b72022-11-17 18:17:01 +01001641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1642 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1643 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001644
Valerio Settiaca21b72022-11-17 18:17:01 +01001645 status = psa_import_key( &attributes, pw, pw_len,
1646 &ssl->handshake->psa_pake_password );
1647 if( status != PSA_SUCCESS )
1648 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001649
1650 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1651 psa_pake_cs_set_primitive( &cipher_suite,
1652 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1653 PSA_ECC_FAMILY_SECP_R1,
1654 256) );
1655 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1656
1657 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1658 if( status != PSA_SUCCESS )
1659 {
1660 psa_destroy_key( ssl->handshake->psa_pake_password );
1661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1662 }
1663
1664 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1665 if( status != PSA_SUCCESS )
1666 {
1667 psa_destroy_key( ssl->handshake->psa_pake_password );
1668 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1670 }
1671
Valerio Settiaca21b72022-11-17 18:17:01 +01001672 psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx,
1673 ssl->handshake->psa_pake_password );
1674 if( status != PSA_SUCCESS )
Neil Armstrongca7d5062022-05-31 14:43:23 +02001675 {
Valerio Settiaca21b72022-11-17 18:17:01 +01001676 psa_destroy_key( ssl->handshake->psa_pake_password );
1677 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1678 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001679 }
1680
1681 ssl->handshake->psa_pake_ctx_is_ok = 1;
1682
1683 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001684}
1685#else /* MBEDTLS_USE_PSA_CRYPTO */
1686int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1687 const unsigned char *pw,
1688 size_t pw_len )
1689{
1690 mbedtls_ecjpake_role role;
1691
1692 if( ssl->handshake == NULL || ssl->conf == NULL )
1693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1694
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1696 role = MBEDTLS_ECJPAKE_SERVER;
1697 else
1698 role = MBEDTLS_ECJPAKE_CLIENT;
1699
1700 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1701 role,
1702 MBEDTLS_MD_SHA256,
1703 MBEDTLS_ECP_DP_SECP256R1,
1704 pw, pw_len ) );
1705}
Valerio Setti02c25b52022-11-15 14:08:42 +01001706#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001707#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001708
Ronald Cron73fe8df2022-10-05 14:31:43 +02001709#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001710int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001711{
Ronald Crond29e13e2022-10-19 10:33:48 +02001712 if( conf->psk_identity == NULL ||
1713 conf->psk_identity_len == 0 )
1714 {
1715 return( 0 );
1716 }
1717
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001718#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001719 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001720 return( 1 );
1721#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001722
1723 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001724 return( 1 );
1725
1726 return( 0 );
1727}
1728
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001729static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1730{
1731 /* Remove reference to existing PSK, if any. */
1732#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001733 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001734 {
1735 /* The maintenance of the PSK key slot is the
1736 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001737 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001738 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001739#endif /* MBEDTLS_USE_PSA_CRYPTO */
1740 if( conf->psk != NULL )
1741 {
1742 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1743
1744 mbedtls_free( conf->psk );
1745 conf->psk = NULL;
1746 conf->psk_len = 0;
1747 }
1748
1749 /* Remove reference to PSK identity, if any. */
1750 if( conf->psk_identity != NULL )
1751 {
1752 mbedtls_free( conf->psk_identity );
1753 conf->psk_identity = NULL;
1754 conf->psk_identity_len = 0;
1755 }
1756}
1757
Hanno Becker7390c712018-11-15 13:33:04 +00001758/* This function assumes that PSK identity in the SSL config is unset.
1759 * It checks that the provided identity is well-formed and attempts
1760 * to make a copy of it in the SSL config.
1761 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001762MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001763static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1764 unsigned char const *psk_identity,
1765 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001766{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001767 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001768 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001769 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001770 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001771 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001772 {
1773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1774 }
1775
Hanno Becker7390c712018-11-15 13:33:04 +00001776 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1777 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001779
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001780 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001781 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001782
1783 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001784}
1785
Hanno Becker7390c712018-11-15 13:33:04 +00001786int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1787 const unsigned char *psk, size_t psk_len,
1788 const unsigned char *psk_identity, size_t psk_identity_len )
1789{
Janos Follath865b3eb2019-12-16 11:46:15 +00001790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001791
1792 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001793 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001794 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001795
1796 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001797 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001799 if( psk_len == 0 )
1800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1801 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1803
Hanno Becker7390c712018-11-15 13:33:04 +00001804 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1805 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1806 conf->psk_len = psk_len;
1807 memcpy( conf->psk, psk, conf->psk_len );
1808
1809 /* Check and set PSK Identity */
1810 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1811 if( ret != 0 )
1812 ssl_conf_remove_psk( conf );
1813
1814 return( ret );
1815}
1816
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001817static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1818{
1819#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001820 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001821 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001822 /* The maintenance of the external PSK key slot is the
1823 * user's responsibility. */
1824 if( ssl->handshake->psk_opaque_is_internal )
1825 {
1826 psa_destroy_key( ssl->handshake->psk_opaque );
1827 ssl->handshake->psk_opaque_is_internal = 0;
1828 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001829 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001830 }
Neil Armstronge952a302022-05-03 10:22:14 +02001831#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001832 if( ssl->handshake->psk != NULL )
1833 {
1834 mbedtls_platform_zeroize( ssl->handshake->psk,
1835 ssl->handshake->psk_len );
1836 mbedtls_free( ssl->handshake->psk );
1837 ssl->handshake->psk_len = 0;
1838 }
Neil Armstronge952a302022-05-03 10:22:14 +02001839#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001840}
1841
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001842int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
1843 const unsigned char *psk, size_t psk_len )
1844{
Neil Armstrong501c9322022-05-03 09:35:09 +02001845#if defined(MBEDTLS_USE_PSA_CRYPTO)
1846 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08001847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08001848 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08001849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02001850#endif /* MBEDTLS_USE_PSA_CRYPTO */
1851
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001852 if( psk == NULL || ssl->handshake == NULL )
1853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1854
1855 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1857
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001858 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001859
Neil Armstrong501c9322022-05-03 09:35:09 +02001860#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08001861#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1862 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
1863 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08001864 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08001865 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
1866 else
1867 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
1868 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1869 }
1870#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02001871
Jerry Yu568ec252022-07-22 21:27:34 +08001872#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08001873 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
1874 {
1875 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
1876 psa_set_key_usage_flags( &key_attributes,
1877 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
1878 }
1879#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1880
Neil Armstrong501c9322022-05-03 09:35:09 +02001881 psa_set_key_algorithm( &key_attributes, alg );
1882 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
1883
1884 status = psa_import_key( &key_attributes, psk, psk_len, &key );
1885 if( status != PSA_SUCCESS )
1886 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1887
1888 /* Allow calling psa_destroy_key() on psk remove */
1889 ssl->handshake->psk_opaque_is_internal = 1;
1890 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
1891#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001892 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001893 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001894
1895 ssl->handshake->psk_len = psk_len;
1896 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
1897
1898 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02001899#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001900}
1901
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001902#if defined(MBEDTLS_USE_PSA_CRYPTO)
1903int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01001904 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001905 const unsigned char *psk_identity,
1906 size_t psk_identity_len )
1907{
Janos Follath865b3eb2019-12-16 11:46:15 +00001908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001909
1910 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001911 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001912 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001913
Hanno Becker7390c712018-11-15 13:33:04 +00001914 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001915 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00001916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02001917 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00001918
1919 /* Check and set PSK Identity */
1920 ret = ssl_conf_set_psk_identity( conf, psk_identity,
1921 psk_identity_len );
1922 if( ret != 0 )
1923 ssl_conf_remove_psk( conf );
1924
1925 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001926}
1927
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001928int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
1929 mbedtls_svc_key_id_t psk )
1930{
1931 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
1932 ( ssl->handshake == NULL ) )
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934
1935 ssl_remove_psk( ssl );
1936 ssl->handshake->psk_opaque = psk;
1937 return( 0 );
1938}
1939#endif /* MBEDTLS_USE_PSA_CRYPTO */
1940
Jerry Yu8897c072022-08-12 13:56:53 +08001941#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001942void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
1943 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
1944 size_t),
1945 void *p_psk )
1946{
1947 conf->f_psk = f_psk;
1948 conf->p_psk = p_psk;
1949}
Jerry Yu8897c072022-08-12 13:56:53 +08001950#endif /* MBEDTLS_SSL_SRV_C */
1951
Ronald Cron73fe8df2022-10-05 14:31:43 +02001952#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01001953
1954#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02001955static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1956 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001957{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001958#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001959 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001960 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001961#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02001962 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001963 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02001964 return( MBEDTLS_SSL_MODE_STREAM );
1965}
1966
1967#else /* MBEDTLS_USE_PSA_CRYPTO */
1968
1969static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
1970 mbedtls_cipher_mode_t mode )
1971{
1972#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1973 if( mode == MBEDTLS_MODE_CBC )
1974 return( MBEDTLS_SSL_MODE_CBC );
1975#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1976
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001977#if defined(MBEDTLS_GCM_C) || \
1978 defined(MBEDTLS_CCM_C) || \
1979 defined(MBEDTLS_CHACHAPOLY_C)
1980 if( mode == MBEDTLS_MODE_GCM ||
1981 mode == MBEDTLS_MODE_CCM ||
1982 mode == MBEDTLS_MODE_CHACHAPOLY )
1983 return( MBEDTLS_SSL_MODE_AEAD );
1984#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001985
1986 return( MBEDTLS_SSL_MODE_STREAM );
1987}
Gilles Peskine301711e2022-04-26 16:57:05 +02001988#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02001989
Gilles Peskinee108d982022-04-26 16:50:40 +02001990static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
1991 mbedtls_ssl_mode_t base_mode,
1992 int encrypt_then_mac )
1993{
1994#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1995 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
1996 base_mode == MBEDTLS_SSL_MODE_CBC )
1997 {
1998 return( MBEDTLS_SSL_MODE_CBC_ETM );
1999 }
2000#else
2001 (void) encrypt_then_mac;
2002#endif
2003 return( base_mode );
2004}
2005
Neil Armstrongab555e02022-04-04 11:07:59 +02002006mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002007 const mbedtls_ssl_transform *transform )
2008{
Gilles Peskinee108d982022-04-26 16:50:40 +02002009 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002011 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002012#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002013 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2014#endif
2015 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002016
Gilles Peskinee108d982022-04-26 16:50:40 +02002017 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002018#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002019 encrypt_then_mac = transform->encrypt_then_mac;
2020#endif
2021 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002022}
2023
Neil Armstrongab555e02022-04-04 11:07:59 +02002024mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002025#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002026 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002027#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002028 const mbedtls_ssl_ciphersuite_t *suite )
2029{
Gilles Peskinee108d982022-04-26 16:50:40 +02002030 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2031
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002032#if defined(MBEDTLS_USE_PSA_CRYPTO)
2033 psa_status_t status;
2034 psa_algorithm_t alg;
2035 psa_key_type_t type;
2036 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002037 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2038 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002039 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002040#else
2041 const mbedtls_cipher_info_t *cipher =
2042 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002043 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002044 {
2045 base_mode =
2046 mbedtls_ssl_get_base_mode(
2047 mbedtls_cipher_info_get_mode( cipher ) );
2048 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002049#endif /* MBEDTLS_USE_PSA_CRYPTO */
2050
Gilles Peskinee108d982022-04-26 16:50:40 +02002051#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2052 int encrypt_then_mac = 0;
2053#endif
2054 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002055}
2056
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002057#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002058
2059#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002060/* Serialization of TLS 1.3 sessions:
2061 *
2062 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002063 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002064 * uint64 ticket_received;
2065 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002066 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002067 * } ClientOnlyData;
2068 *
2069 * struct {
2070 * uint8 endpoint;
2071 * uint8 ciphersuite[2];
2072 * uint32 ticket_age_add;
2073 * uint8 ticket_flags;
2074 * opaque resumption_key<0..255>;
2075 * select ( endpoint ) {
2076 * case client: ClientOnlyData;
2077 * case server: uint64 start_time;
2078 * };
2079 * } serialized_session_tls13;
2080 *
2081 */
2082#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002083MBEDTLS_CHECK_RETURN_CRITICAL
2084static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2085 unsigned char *buf,
2086 size_t buf_len,
2087 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002088{
2089 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002090#if defined(MBEDTLS_SSL_CLI_C) && \
2091 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2092 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002093 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002094#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002095 size_t needed = 1 /* endpoint */
2096 + 2 /* ciphersuite */
2097 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002098 + 1 /* ticket_flags */
2099 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002100 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002101
2102 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2104 needed += session->resumption_key_len; /* resumption_key */
2105
Jerry Yu438ddd82022-07-07 06:55:50 +00002106#if defined(MBEDTLS_HAVE_TIME)
2107 needed += 8; /* start_time or ticket_received */
2108#endif
2109
2110#if defined(MBEDTLS_SSL_CLI_C)
2111 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2112 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002113#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002114 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002115 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002116#endif
2117
Jerry Yu438ddd82022-07-07 06:55:50 +00002118 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002119 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002120
2121 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002122 if( session->ticket_len > SIZE_MAX - needed )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002124
Jerry Yue28d9742022-08-18 15:44:03 +08002125 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002126 }
2127#endif /* MBEDTLS_SSL_CLI_C */
2128
Jerry Yue36fdd62022-08-17 21:31:36 +08002129 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002130 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002131 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002132
2133 p[0] = session->endpoint;
2134 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2135 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2136 p[7] = session->ticket_flags;
2137
2138 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002139 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002140 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002141 memcpy( p, session->resumption_key, session->resumption_key_len );
2142 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002143
2144#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2145 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2146 {
2147 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2148 p += 8;
2149 }
2150#endif /* MBEDTLS_HAVE_TIME */
2151
2152#if defined(MBEDTLS_SSL_CLI_C)
2153 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2154 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002155#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2156 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2157 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002158 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002159 {
2160 /* save host name */
2161 memcpy( p, session->hostname, hostname_len );
2162 p += hostname_len;
2163 }
2164#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2165
Jerry Yu438ddd82022-07-07 06:55:50 +00002166#if defined(MBEDTLS_HAVE_TIME)
2167 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2168 p += 8;
2169#endif
2170 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2171 p += 4;
2172
2173 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2174 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002175
2176 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002177 {
2178 memcpy( p, session->ticket, session->ticket_len );
2179 p += session->ticket_len;
2180 }
2181 }
2182#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002183 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002184}
2185
2186MBEDTLS_CHECK_RETURN_CRITICAL
2187static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2188 const unsigned char *buf,
2189 size_t len )
2190{
2191 const unsigned char *p = buf;
2192 const unsigned char *end = buf + len;
2193
2194 if( end - p < 9 )
2195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2196 session->endpoint = p[0];
2197 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2198 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2199 session->ticket_flags = p[7];
2200
2201 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002202 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002203 p += 9;
2204
Jerry Yubc7c1a42022-07-21 22:57:37 +08002205 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2207
Jerry Yubc7c1a42022-07-21 22:57:37 +08002208 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002210 memcpy( session->resumption_key, p, session->resumption_key_len );
2211 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002212
2213#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2214 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2215 {
2216 if( end - p < 8 )
2217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2218 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2219 p += 8;
2220 }
2221#endif /* MBEDTLS_HAVE_TIME */
2222
2223#if defined(MBEDTLS_SSL_CLI_C)
2224 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2225 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002226#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2227 defined(MBEDTLS_SSL_SESSION_TICKETS)
2228 size_t hostname_len;
2229 /* load host name */
2230 if( end - p < 2 )
2231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2232 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2233 p += 2;
2234
2235 if( end - p < ( long int )hostname_len )
2236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2237 if( hostname_len > 0 )
2238 {
2239 session->hostname = mbedtls_calloc( 1, hostname_len );
2240 if( session->hostname == NULL )
2241 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2242 memcpy( session->hostname, p, hostname_len );
2243 p += hostname_len;
2244 }
2245#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2246 MBEDTLS_SSL_SESSION_TICKETS */
2247
Jerry Yu438ddd82022-07-07 06:55:50 +00002248#if defined(MBEDTLS_HAVE_TIME)
2249 if( end - p < 8 )
2250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2251 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2252 p += 8;
2253#endif
2254 if( end - p < 4 )
2255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2256 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2257 p += 4;
2258
2259 if( end - p < 2 )
2260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2261 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2262 p += 2;
2263
2264 if( end - p < ( long int )session->ticket_len )
2265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2266 if( session->ticket_len > 0 )
2267 {
2268 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2269 if( session->ticket == NULL )
2270 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2271 memcpy( session->ticket, p, session->ticket_len );
2272 p += session->ticket_len;
2273 }
2274 }
2275#endif /* MBEDTLS_SSL_CLI_C */
2276
2277 return( 0 );
2278
2279}
2280#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002281MBEDTLS_CHECK_RETURN_CRITICAL
2282static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2283 unsigned char *buf,
2284 size_t buf_len,
2285 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002286{
2287 ((void) session);
2288 ((void) buf);
2289 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002290 *olen = 0;
2291 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002292}
Jerry Yu438ddd82022-07-07 06:55:50 +00002293
2294static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2295 unsigned char *buf,
2296 size_t buf_len )
2297{
2298 ((void) session);
2299 ((void) buf);
2300 ((void) buf_len);
2301 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2302}
2303#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002304#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2305
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002306psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002307 size_t taglen,
2308 psa_algorithm_t *alg,
2309 psa_key_type_t *key_type,
2310 size_t *key_size )
2311{
2312 switch ( mbedtls_cipher_type )
2313 {
2314 case MBEDTLS_CIPHER_AES_128_CBC:
2315 *alg = PSA_ALG_CBC_NO_PADDING;
2316 *key_type = PSA_KEY_TYPE_AES;
2317 *key_size = 128;
2318 break;
2319 case MBEDTLS_CIPHER_AES_128_CCM:
2320 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2321 *key_type = PSA_KEY_TYPE_AES;
2322 *key_size = 128;
2323 break;
2324 case MBEDTLS_CIPHER_AES_128_GCM:
2325 *alg = PSA_ALG_GCM;
2326 *key_type = PSA_KEY_TYPE_AES;
2327 *key_size = 128;
2328 break;
2329 case MBEDTLS_CIPHER_AES_192_CCM:
2330 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2331 *key_type = PSA_KEY_TYPE_AES;
2332 *key_size = 192;
2333 break;
2334 case MBEDTLS_CIPHER_AES_192_GCM:
2335 *alg = PSA_ALG_GCM;
2336 *key_type = PSA_KEY_TYPE_AES;
2337 *key_size = 192;
2338 break;
2339 case MBEDTLS_CIPHER_AES_256_CBC:
2340 *alg = PSA_ALG_CBC_NO_PADDING;
2341 *key_type = PSA_KEY_TYPE_AES;
2342 *key_size = 256;
2343 break;
2344 case MBEDTLS_CIPHER_AES_256_CCM:
2345 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2346 *key_type = PSA_KEY_TYPE_AES;
2347 *key_size = 256;
2348 break;
2349 case MBEDTLS_CIPHER_AES_256_GCM:
2350 *alg = PSA_ALG_GCM;
2351 *key_type = PSA_KEY_TYPE_AES;
2352 *key_size = 256;
2353 break;
2354 case MBEDTLS_CIPHER_ARIA_128_CBC:
2355 *alg = PSA_ALG_CBC_NO_PADDING;
2356 *key_type = PSA_KEY_TYPE_ARIA;
2357 *key_size = 128;
2358 break;
2359 case MBEDTLS_CIPHER_ARIA_128_CCM:
2360 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2361 *key_type = PSA_KEY_TYPE_ARIA;
2362 *key_size = 128;
2363 break;
2364 case MBEDTLS_CIPHER_ARIA_128_GCM:
2365 *alg = PSA_ALG_GCM;
2366 *key_type = PSA_KEY_TYPE_ARIA;
2367 *key_size = 128;
2368 break;
2369 case MBEDTLS_CIPHER_ARIA_192_CCM:
2370 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2371 *key_type = PSA_KEY_TYPE_ARIA;
2372 *key_size = 192;
2373 break;
2374 case MBEDTLS_CIPHER_ARIA_192_GCM:
2375 *alg = PSA_ALG_GCM;
2376 *key_type = PSA_KEY_TYPE_ARIA;
2377 *key_size = 192;
2378 break;
2379 case MBEDTLS_CIPHER_ARIA_256_CBC:
2380 *alg = PSA_ALG_CBC_NO_PADDING;
2381 *key_type = PSA_KEY_TYPE_ARIA;
2382 *key_size = 256;
2383 break;
2384 case MBEDTLS_CIPHER_ARIA_256_CCM:
2385 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2386 *key_type = PSA_KEY_TYPE_ARIA;
2387 *key_size = 256;
2388 break;
2389 case MBEDTLS_CIPHER_ARIA_256_GCM:
2390 *alg = PSA_ALG_GCM;
2391 *key_type = PSA_KEY_TYPE_ARIA;
2392 *key_size = 256;
2393 break;
2394 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2395 *alg = PSA_ALG_CBC_NO_PADDING;
2396 *key_type = PSA_KEY_TYPE_CAMELLIA;
2397 *key_size = 128;
2398 break;
2399 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2400 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2401 *key_type = PSA_KEY_TYPE_CAMELLIA;
2402 *key_size = 128;
2403 break;
2404 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2405 *alg = PSA_ALG_GCM;
2406 *key_type = PSA_KEY_TYPE_CAMELLIA;
2407 *key_size = 128;
2408 break;
2409 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2410 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2411 *key_type = PSA_KEY_TYPE_CAMELLIA;
2412 *key_size = 192;
2413 break;
2414 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2415 *alg = PSA_ALG_GCM;
2416 *key_type = PSA_KEY_TYPE_CAMELLIA;
2417 *key_size = 192;
2418 break;
2419 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2420 *alg = PSA_ALG_CBC_NO_PADDING;
2421 *key_type = PSA_KEY_TYPE_CAMELLIA;
2422 *key_size = 256;
2423 break;
2424 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2425 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2426 *key_type = PSA_KEY_TYPE_CAMELLIA;
2427 *key_size = 256;
2428 break;
2429 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2430 *alg = PSA_ALG_GCM;
2431 *key_type = PSA_KEY_TYPE_CAMELLIA;
2432 *key_size = 256;
2433 break;
2434 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2435 *alg = PSA_ALG_CHACHA20_POLY1305;
2436 *key_type = PSA_KEY_TYPE_CHACHA20;
2437 *key_size = 256;
2438 break;
2439 case MBEDTLS_CIPHER_NULL:
2440 *alg = MBEDTLS_SSL_NULL_CIPHER;
2441 *key_type = 0;
2442 *key_size = 0;
2443 break;
2444 default:
2445 return PSA_ERROR_NOT_SUPPORTED;
2446 }
2447
2448 return PSA_SUCCESS;
2449}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002450#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002451
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002452#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002453int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2454 const unsigned char *dhm_P, size_t P_len,
2455 const unsigned char *dhm_G, size_t G_len )
2456{
Janos Follath865b3eb2019-12-16 11:46:15 +00002457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002458
Glenn Strausscee11292021-12-20 01:43:17 -05002459 mbedtls_mpi_free( &conf->dhm_P );
2460 mbedtls_mpi_free( &conf->dhm_G );
2461
Hanno Beckera90658f2017-10-04 15:29:08 +01002462 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2463 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2464 {
2465 mbedtls_mpi_free( &conf->dhm_P );
2466 mbedtls_mpi_free( &conf->dhm_G );
2467 return( ret );
2468 }
2469
2470 return( 0 );
2471}
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002473int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002474{
Janos Follath865b3eb2019-12-16 11:46:15 +00002475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002476
Glenn Strausscee11292021-12-20 01:43:17 -05002477 mbedtls_mpi_free( &conf->dhm_P );
2478 mbedtls_mpi_free( &conf->dhm_G );
2479
Gilles Peskinee5702482021-06-11 21:59:08 +02002480 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2481 &conf->dhm_P ) ) != 0 ||
2482 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2483 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002484 {
2485 mbedtls_mpi_free( &conf->dhm_P );
2486 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002487 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002488 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002489
2490 return( 0 );
2491}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002492#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002493
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002494#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2495/*
2496 * Set the minimum length for Diffie-Hellman parameters
2497 */
2498void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2499 unsigned int bitlen )
2500{
2501 conf->dhm_min_bitlen = bitlen;
2502}
2503#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2504
Ronald Crone68ab4f2022-10-05 12:46:29 +02002505#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002506#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002507/*
2508 * Set allowed/preferred hashes for handshake signatures
2509 */
2510void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2511 const int *hashes )
2512{
2513 conf->sig_hashes = hashes;
2514}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002515#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002516
Jerry Yuf017ee42022-01-12 15:49:48 +08002517/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002518void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2519 const uint16_t* sig_algs )
2520{
Jerry Yuf017ee42022-01-12 15:49:48 +08002521#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2522 conf->sig_hashes = NULL;
2523#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2524 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002525}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002526#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002527
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002528#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002529#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002530/*
2531 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002532 *
2533 * mbedtls_ssl_setup() takes the provided list
2534 * and translates it to a list of IANA TLS group identifiers,
2535 * stored in ssl->handshake->group_list.
2536 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002537 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002538void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002539 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002541 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002542 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002543}
Brett Warrene0edc842021-08-17 09:53:13 +01002544#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002545#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002546
Brett Warrene0edc842021-08-17 09:53:13 +01002547/*
2548 * Set the allowed groups
2549 */
2550void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2551 const uint16_t *group_list )
2552{
2553#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2554 conf->curve_list = NULL;
2555#endif
2556 conf->group_list = group_list;
2557}
2558
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002559#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002561{
Hanno Becker947194e2017-04-07 13:25:49 +01002562 /* Initialize to suppress unnecessary compiler warning */
2563 size_t hostname_len = 0;
2564
2565 /* Check if new hostname is valid before
2566 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002567 if( hostname != NULL )
2568 {
2569 hostname_len = strlen( hostname );
2570
2571 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2573 }
2574
2575 /* Now it's clear that we will overwrite the old hostname,
2576 * so we can free it safely */
2577
2578 if( ssl->hostname != NULL )
2579 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002580 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002581 mbedtls_free( ssl->hostname );
2582 }
2583
2584 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002587 {
2588 ssl->hostname = NULL;
2589 }
2590 else
2591 {
2592 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002593 if( ssl->hostname == NULL )
2594 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002595
Hanno Becker947194e2017-04-07 13:25:49 +01002596 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002597
Hanno Becker947194e2017-04-07 13:25:49 +01002598 ssl->hostname[hostname_len] = '\0';
2599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 return( 0 );
2602}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002603#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002605#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002606void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002608 const unsigned char *, size_t),
2609 void *p_sni )
2610{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002611 conf->f_sni = f_sni;
2612 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002617int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002618{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002619 size_t cur_len, tot_len;
2620 const char **p;
2621
2622 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002623 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2624 * MUST NOT be truncated."
2625 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002626 */
2627 tot_len = 0;
2628 for( p = protos; *p != NULL; p++ )
2629 {
2630 cur_len = strlen( *p );
2631 tot_len += cur_len;
2632
Ronald Cron8216dd32020-04-23 16:41:44 +02002633 if( ( cur_len == 0 ) ||
2634 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2635 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002637 }
2638
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002639 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002640
2641 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002642}
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002645{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002646 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002649
Johan Pascalb62bb512015-12-03 21:56:45 +01002650#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002651void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2652 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002653{
2654 conf->dtls_srtp_mki_support = support_mki_value;
2655}
2656
Ron Eldoref72faf2018-07-12 11:54:20 +03002657int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2658 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002659 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002660{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002661 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002662 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002664 }
Ron Eldor591f1622018-01-22 12:30:04 +02002665
2666 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002667 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002668 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002669 }
Ron Eldor591f1622018-01-22 12:30:04 +02002670
2671 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2672 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002673 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002674}
2675
Ron Eldoref72faf2018-07-12 11:54:20 +03002676int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002677 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002678{
Johan Pascal253d0262020-09-22 13:04:45 +02002679 const mbedtls_ssl_srtp_profile *p;
2680 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002681
Johan Pascal253d0262020-09-22 13:04:45 +02002682 /* check the profiles list: all entry must be valid,
2683 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002684 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2685 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2686 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002687 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002688 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002689 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002690 list_size++;
2691 }
2692 else
2693 {
2694 /* unsupported value, stop parsing and set the size to an error value */
2695 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002696 }
2697 }
2698
Johan Pascal5ef72d22020-10-28 17:05:47 +01002699 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002700 {
Johan Pascal253d0262020-09-22 13:04:45 +02002701 conf->dtls_srtp_profile_list = NULL;
2702 conf->dtls_srtp_profile_list_len = 0;
2703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2704 }
2705
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002706 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002707 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002708
2709 return( 0 );
2710}
2711
Johan Pascal5ef72d22020-10-28 17:05:47 +01002712void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2713 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002714{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002715 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2716 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002717 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002718 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002719 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002720 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002721 else
2722 {
2723 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002724 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2725 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002726 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002727}
Johan Pascalb62bb512015-12-03 21:56:45 +01002728#endif /* MBEDTLS_SSL_DTLS_SRTP */
2729
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302730#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002731void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002732{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002733 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002734}
2735
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002736void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002737{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002738 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002739}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302740#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002741
Janos Follath088ce432017-04-10 12:42:31 +01002742#if defined(MBEDTLS_SSL_SRV_C)
2743void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2744 char cert_req_ca_list )
2745{
2746 conf->cert_req_ca_list = cert_req_ca_list;
2747}
2748#endif
2749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002751void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002752{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002753 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002754}
2755#endif
2756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002758void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002759{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002760 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002761}
2762#endif
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002765int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002768 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002771 }
2772
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002773 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002774
2775 return( 0 );
2776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002778
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002779void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002780{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002781 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002782}
2783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002785void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002786{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002787 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002788}
2789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002790void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002791{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002792 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002793}
2794
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002795void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002796 const unsigned char period[8] )
2797{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002798 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002803#if defined(MBEDTLS_SSL_CLI_C)
2804void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002805{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002806 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002807}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002808#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002809
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002810#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002811
Jerry Yud0766ec2022-09-22 10:46:57 +08002812#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002813void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2814 uint16_t num_tickets )
2815{
Jerry Yud0766ec2022-09-22 10:46:57 +08002816 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002817}
2818#endif
2819
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002820void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2821 mbedtls_ssl_ticket_write_t *f_ticket_write,
2822 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2823 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002824{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002825 conf->f_ticket_write = f_ticket_write;
2826 conf->f_ticket_parse = f_ticket_parse;
2827 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002828}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002831
Hanno Becker7e6c1782021-06-08 09:24:55 +01002832void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2833 mbedtls_ssl_export_keys_t *f_export_keys,
2834 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002835{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002836 ssl->f_export_keys = f_export_keys;
2837 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002838}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002839
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002840#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002841void mbedtls_ssl_conf_async_private_cb(
2842 mbedtls_ssl_config *conf,
2843 mbedtls_ssl_async_sign_t *f_async_sign,
2844 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2845 mbedtls_ssl_async_resume_t *f_async_resume,
2846 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002847 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002848{
2849 conf->f_async_sign_start = f_async_sign;
2850 conf->f_async_decrypt_start = f_async_decrypt;
2851 conf->f_async_resume = f_async_resume;
2852 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002853 conf->p_async_config_data = async_config_data;
2854}
2855
Gilles Peskine8f97af72018-04-26 11:46:10 +02002856void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
2857{
2858 return( conf->p_async_config_data );
2859}
2860
Gilles Peskine1febfef2018-04-30 11:54:39 +02002861void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002862{
2863 if( ssl->handshake == NULL )
2864 return( NULL );
2865 else
2866 return( ssl->handshake->user_async_ctx );
2867}
2868
Gilles Peskine1febfef2018-04-30 11:54:39 +02002869void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002870 void *ctx )
2871{
2872 if( ssl->handshake != NULL )
2873 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002874}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002875#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002876
Paul Bakker5121ce52009-01-03 21:22:43 +00002877/*
2878 * SSL get accessors
2879 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002880uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002882 if( ssl->session != NULL )
2883 return( ssl->session->verify_result );
2884
2885 if( ssl->session_negotiate != NULL )
2886 return( ssl->session_negotiate->verify_result );
2887
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02002888 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889}
2890
Glenn Strauss8f526902022-01-13 00:04:49 -05002891int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
2892{
2893 if( ssl == NULL || ssl->session == NULL )
2894 return( 0 );
2895
2896 return( ssl->session->ciphersuite );
2897}
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00002900{
Paul Bakker926c8e42013-03-06 10:23:34 +01002901 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002902 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00002905}
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002911 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002912 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002913 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002914 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002915 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002916 default:
2917 return( "unknown (DTLS)" );
2918 }
2919 }
2920#endif
2921
Glenn Strauss60bfe602022-03-14 19:04:24 -04002922 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00002923 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002924 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04002926 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01002927 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002928 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002929 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00002930 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002931}
2932
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002933#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002934size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
2935{
David Horstmann95d516f2021-05-04 18:36:56 +01002936 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002937 size_t read_mfl;
2938
2939 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
2940 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2941 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
2942 {
2943 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
2944 }
2945
2946 /* Check if a smaller max length was negotiated */
2947 if( ssl->session_out != NULL )
2948 {
2949 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
2950 if( read_mfl < max_len )
2951 {
2952 max_len = read_mfl;
2953 }
2954 }
2955
2956 // During a handshake, use the value being negotiated
2957 if( ssl->session_negotiate != NULL )
2958 {
2959 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2960 if( read_mfl < max_len )
2961 {
2962 max_len = read_mfl;
2963 }
2964 }
2965
2966 return( max_len );
2967}
2968
2969size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002970{
2971 size_t max_len;
2972
2973 /*
2974 * Assume mfl_code is correct since it was checked when set
2975 */
Angus Grattond8213d02016-05-25 20:56:48 +10002976 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002977
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002978 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002979 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10002980 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002981 {
Angus Grattond8213d02016-05-25 20:56:48 +10002982 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002983 }
2984
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002985 /* During a handshake, use the value being negotiated */
2986 if( ssl->session_negotiate != NULL &&
2987 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
2988 {
2989 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
2990 }
2991
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002992 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002993}
2994#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2995
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00002997size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002998{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002999 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3000 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3001 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3002 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3003 return ( 0 );
3004
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003005 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3006 return( ssl->mtu );
3007
3008 if( ssl->mtu == 0 )
3009 return( ssl->handshake->mtu );
3010
3011 return( ssl->mtu < ssl->handshake->mtu ?
3012 ssl->mtu : ssl->handshake->mtu );
3013}
3014#endif /* MBEDTLS_SSL_PROTO_DTLS */
3015
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003016int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3017{
3018 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3019
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003020#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3021 !defined(MBEDTLS_SSL_PROTO_DTLS)
3022 (void) ssl;
3023#endif
3024
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003025#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003026 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003027
3028 if( max_len > mfl )
3029 max_len = mfl;
3030#endif
3031
3032#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003033 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003034 {
Hanno Becker89490712020-02-05 10:50:12 +00003035 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003036 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3037 const size_t overhead = (size_t) ret;
3038
3039 if( ret < 0 )
3040 return( ret );
3041
3042 if( mtu <= overhead )
3043 {
3044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3045 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3046 }
3047
3048 if( max_len > mtu - overhead )
3049 max_len = mtu - overhead;
3050 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003051#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003052
Hanno Becker0defedb2018-08-10 12:35:02 +01003053#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3054 !defined(MBEDTLS_SSL_PROTO_DTLS)
3055 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003056#endif
3057
3058 return( (int) max_len );
3059}
3060
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003061int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3062{
3063 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3064
3065#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3066 (void) ssl;
3067#endif
3068
3069#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3070 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3071
3072 if( max_len > mfl )
3073 max_len = mfl;
3074#endif
3075
3076 return( (int) max_len );
3077}
3078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_X509_CRT_PARSE_C)
3080const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003081{
3082 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003083 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003084
Hanno Beckere6824572019-02-07 13:18:46 +00003085#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003086 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003087#else
3088 return( NULL );
3089#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003094int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3095 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003096{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003097 int ret;
3098
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003099 if( ssl == NULL ||
3100 dst == NULL ||
3101 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003102 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003105 }
3106
Hanno Beckere810bbc2021-05-14 16:01:05 +01003107 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3108 * idempotent: Each session can only be exported once.
3109 *
3110 * (This is in preparation for TLS 1.3 support where we will
3111 * need the ability to export multiple sessions (aka tickets),
3112 * which will be achieved by calling mbedtls_ssl_get_session()
3113 * multiple times until it fails.)
3114 *
3115 * Check whether we have already exported the current session,
3116 * and fail if so.
3117 */
3118 if( ssl->session->exported == 1 )
3119 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3120
3121 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3122 if( ret != 0 )
3123 return( ret );
3124
3125 /* Remember that we've exported the session. */
3126 ssl->session->exported = 1;
3127 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131/*
Hanno Beckera835da52019-05-16 12:39:07 +01003132 * Define ticket header determining Mbed TLS version
3133 * and structure of the ticket.
3134 */
3135
Hanno Becker94ef3b32019-05-16 12:50:45 +01003136/*
Hanno Becker50b59662019-05-28 14:30:45 +01003137 * Define bitflag determining compile-time settings influencing
3138 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003139 */
3140
Hanno Becker50b59662019-05-28 14:30:45 +01003141#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003142#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003143#else
Hanno Becker3e088662019-05-29 11:10:18 +01003144#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003145#endif /* MBEDTLS_HAVE_TIME */
3146
3147#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003148#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003149#else
Hanno Becker3e088662019-05-29 11:10:18 +01003150#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003151#endif /* MBEDTLS_X509_CRT_PARSE_C */
3152
3153#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003154#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003155#else
Hanno Becker3e088662019-05-29 11:10:18 +01003156#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003157#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3158
3159#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003160#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003161#else
Hanno Becker3e088662019-05-29 11:10:18 +01003162#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003163#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3164
Hanno Becker94ef3b32019-05-16 12:50:45 +01003165#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003166#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003167#else
Hanno Becker3e088662019-05-29 11:10:18 +01003168#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003169#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3170
Hanno Becker94ef3b32019-05-16 12:50:45 +01003171#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3172#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3173#else
3174#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3175#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3176
Hanno Becker3e088662019-05-29 11:10:18 +01003177#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3178#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3179#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3180#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003181#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3182#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003183
Hanno Becker50b59662019-05-28 14:30:45 +01003184#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003185 ( (uint16_t) ( \
3186 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3187 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3188 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3189 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003190 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003191 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003192
Hanno Beckerf8787072019-05-16 12:41:07 +01003193static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003194 MBEDTLS_VERSION_MAJOR,
3195 MBEDTLS_VERSION_MINOR,
3196 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003197 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3198 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003199};
Hanno Beckera835da52019-05-16 12:39:07 +01003200
3201/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003202 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003203 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003204 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003205 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003206 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003207 * opaque mbedtls_version[3]; // library version: major, minor, patch
3208 * opaque session_format[2]; // library-version specific 16-bit field
3209 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003210 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003211 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003212 * Note: When updating the format, remember to keep
3213 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003214 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003215 * // In this version, `session_format` determines
3216 * // the setting of those compile-time
3217 * // configuration options which influence
3218 * // the structure of mbedtls_ssl_session.
3219 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003220 * uint8_t minor_ver; // Protocol minor version. Possible values:
3221 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003222 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003223 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003224 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003225 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003226 * serialized_session_tls12 data;
3227 *
3228 * };
3229 *
3230 * } serialized_session;
3231 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003232 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003233
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003234MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003235static int ssl_session_save( const mbedtls_ssl_session *session,
3236 unsigned char omit_header,
3237 unsigned char *buf,
3238 size_t buf_len,
3239 size_t *olen )
3240{
3241 unsigned char *p = buf;
3242 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003243 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003244#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3245 size_t out_len;
3246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3247#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003248 if( session == NULL )
3249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3250
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003251 if( !omit_header )
3252 {
3253 /*
3254 * Add Mbed TLS version identifier
3255 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003256 used += sizeof( ssl_serialized_session_header );
3257
3258 if( used <= buf_len )
3259 {
3260 memcpy( p, ssl_serialized_session_header,
3261 sizeof( ssl_serialized_session_header ) );
3262 p += sizeof( ssl_serialized_session_header );
3263 }
3264 }
3265
3266 /*
3267 * TLS version identifier
3268 */
3269 used += 1;
3270 if( used <= buf_len )
3271 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003272 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003273 }
3274
3275 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003276 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003277 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003278 {
3279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003280 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003281 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003282 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003283#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3284
Jerry Yu251a12e2022-07-13 15:15:48 +08003285#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3286 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003287 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3288 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003289 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003290 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003291 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003292#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3293
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003294 default:
3295 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3296 }
3297
3298 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003299 if( used > buf_len )
3300 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003301
3302 return( 0 );
3303}
3304
3305/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003306 * Public wrapper for ssl_session_save()
3307 */
3308int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3309 unsigned char *buf,
3310 size_t buf_len,
3311 size_t *olen )
3312{
3313 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3314}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003315
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003316/*
3317 * Deserialize session, see mbedtls_ssl_session_save() for format.
3318 *
3319 * This internal version is wrapped by a public function that cleans up in
3320 * case of error, and has an extra option omit_header.
3321 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003322MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003323static int ssl_session_load( mbedtls_ssl_session *session,
3324 unsigned char omit_header,
3325 const unsigned char *buf,
3326 size_t len )
3327{
3328 const unsigned char *p = buf;
3329 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003330 size_t remaining_len;
3331
3332
3333 if( session == NULL )
3334 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003335
3336 if( !omit_header )
3337 {
3338 /*
3339 * Check Mbed TLS version identifier
3340 */
3341
3342 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3344
3345 if( memcmp( p, ssl_serialized_session_header,
3346 sizeof( ssl_serialized_session_header ) ) != 0 )
3347 {
3348 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3349 }
3350 p += sizeof( ssl_serialized_session_header );
3351 }
3352
3353 /*
3354 * TLS version identifier
3355 */
3356 if( 1 > (size_t)( end - p ) )
3357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003358 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003359
3360 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003361 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003362 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003363 {
3364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003365 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003366 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003367#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3368
Jerry Yu438ddd82022-07-07 06:55:50 +00003369#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3370 case MBEDTLS_SSL_VERSION_TLS1_3:
3371 return( ssl_tls13_session_load( session, p, remaining_len ) );
3372#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3373
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003374 default:
3375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3376 }
3377}
3378
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003379/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003380 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003381 */
3382int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3383 const unsigned char *buf,
3384 size_t len )
3385{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003386 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003387
3388 if( ret != 0 )
3389 mbedtls_ssl_session_free( session );
3390
3391 return( ret );
3392}
3393
3394/*
Paul Bakker1961b702013-01-25 14:49:24 +01003395 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003397MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003398static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3399{
3400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3401
Ronald Cron66dbf912022-02-02 15:33:46 +01003402 /*
3403 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003404 * that were written into the output buffer by the previous handshake step,
3405 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003406 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3407 * We proceed to the next handshake step only when all data from the
3408 * previous one have been sent to the peer, thus we make sure that this is
3409 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3410 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3411 * we have to wait before to go ahead.
3412 * In the case of TLS 1.3, handshake step handlers do not send data to the
3413 * peer. Data are only sent here and through
3414 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003415 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003416 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003417 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3418 return( ret );
3419
3420#if defined(MBEDTLS_SSL_PROTO_DTLS)
3421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3422 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3423 {
3424 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3425 return( ret );
3426 }
3427#endif /* MBEDTLS_SSL_PROTO_DTLS */
3428
3429 return( ret );
3430}
3431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003433{
Hanno Becker41934dd2021-08-07 19:13:43 +01003434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
Hanno Becker41934dd2021-08-07 19:13:43 +01003436 if( ssl == NULL ||
3437 ssl->conf == NULL ||
3438 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003439 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003440 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003442 }
3443
3444 ret = ssl_prepare_handshake_step( ssl );
3445 if( ret != 0 )
3446 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003447
Jerry Yue7047812021-09-13 19:26:39 +08003448 ret = mbedtls_ssl_handle_pending_alert( ssl );
3449 if( ret != 0 )
3450 goto cleanup;
3451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003454 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3456 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003457
Ronald Cron9f0fba32022-02-10 16:45:15 +01003458 switch( ssl->state )
3459 {
3460 case MBEDTLS_SSL_HELLO_REQUEST:
3461 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
3462 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003463
Ronald Cron9f0fba32022-02-10 16:45:15 +01003464 case MBEDTLS_SSL_CLIENT_HELLO:
3465 ret = mbedtls_ssl_write_client_hello( ssl );
3466 break;
3467
3468 default:
3469#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003470 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003471 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3472 else
3473 ret = mbedtls_ssl_handshake_client_step( ssl );
3474#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3475 ret = mbedtls_ssl_handshake_client_step( ssl );
3476#else
3477 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3478#endif
3479 }
Jerry Yub9930e72021-08-06 17:11:51 +08003480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003484 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003485#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003486 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003487 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003488#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003489
3490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3491 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3492 ret = mbedtls_ssl_handshake_server_step( ssl );
3493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003495#endif
3496
Jerry Yue7047812021-09-13 19:26:39 +08003497 if( ret != 0 )
3498 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003499 /* handshake_step return error. And it is same
3500 * with alert_reason.
3501 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003502 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003503 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003504 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003505 goto cleanup;
3506 }
3507 }
3508
3509cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003510 return( ret );
3511}
3512
3513/*
3514 * Perform the SSL handshake
3515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003517{
3518 int ret = 0;
3519
Hanno Beckera817ea42020-10-20 15:20:23 +01003520 /* Sanity checks */
3521
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
Hanno Beckera817ea42020-10-20 15:20:23 +01003525#if defined(MBEDTLS_SSL_PROTO_DTLS)
3526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3527 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3528 {
3529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3530 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3532 }
3533#endif /* MBEDTLS_SSL_PROTO_DTLS */
3534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003536
Hanno Beckera817ea42020-10-20 15:20:23 +01003537 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003538 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003541
3542 if( ret != 0 )
3543 break;
3544 }
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
3548 return( ret );
3549}
3550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551#if defined(MBEDTLS_SSL_RENEGOTIATION)
3552#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003553/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003554 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003555 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003556MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003558{
Janos Follath865b3eb2019-12-16 11:46:15 +00003559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003562
3563 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3565 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003566
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003567 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003568 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003570 return( ret );
3571 }
3572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003574
3575 return( 0 );
3576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003578
3579/*
3580 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 * - any side: calling mbedtls_ssl_renegotiate(),
3582 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3583 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003584 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003585 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003587 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003588int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003589{
Janos Follath865b3eb2019-12-16 11:46:15 +00003590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003593
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003594 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3595 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003596
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003597 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3598 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003600 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003602 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003603 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003604 ssl->handshake->out_msg_seq = 1;
3605 else
3606 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003607 }
3608#endif
3609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3611 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003616 return( ret );
3617 }
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003620
3621 return( 0 );
3622}
3623
3624/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003625 * Renegotiate current connection on client,
3626 * or request renegotiation on server
3627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003631
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003632 if( ssl == NULL || ssl->conf == NULL )
3633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003636 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003637 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003638 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003639 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003643
3644 /* Did we already try/start sending HelloRequest? */
3645 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003647
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003648 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003653 /*
3654 * On client, either start the renegotiation process or,
3655 * if already in progress, continue the handshake
3656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003658 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003659 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003661
Hanno Becker40cdaa12020-02-05 10:48:27 +00003662 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003663 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003665 return( ret );
3666 }
3667 }
3668 else
3669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003673 return( ret );
3674 }
3675 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003677
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003678 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003681
Gilles Peskine9b562d52018-04-25 20:32:43 +02003682void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003683{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003684 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3685
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003686 if( handshake == NULL )
3687 return;
3688
Brett Warrene0edc842021-08-17 09:53:13 +01003689#if defined(MBEDTLS_ECP_C)
3690#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3691 if ( ssl->handshake->group_list_heap_allocated )
3692 mbedtls_free( (void*) handshake->group_list );
3693 handshake->group_list = NULL;
3694#endif /* MBEDTLS_DEPRECATED_REMOVED */
3695#endif /* MBEDTLS_ECP_C */
3696
Ronald Crone68ab4f2022-10-05 12:46:29 +02003697#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003698#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3699 if ( ssl->handshake->sig_algs_heap_allocated )
3700 mbedtls_free( (void*) handshake->sig_algs );
3701 handshake->sig_algs = NULL;
3702#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003703#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3704 if( ssl->handshake->certificate_request_context )
3705 {
3706 mbedtls_free( (void*) handshake->certificate_request_context );
3707 }
3708#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003709#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003710
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003711#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3712 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3713 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003714 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003715 handshake->async_in_progress = 0;
3716 }
3717#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3718
Andrzej Kurek25f27152022-08-17 16:09:31 -04003719#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003720#if defined(MBEDTLS_USE_PSA_CRYPTO)
3721 psa_hash_abort( &handshake->fin_sha256_psa );
3722#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003723 mbedtls_sha256_free( &handshake->fin_sha256 );
3724#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003725#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003726#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003727#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003728 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003729#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003730 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003731#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003732#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734#if defined(MBEDTLS_DHM_C)
3735 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003736#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003737#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003739#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01003740
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003741#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02003742#if defined(MBEDTLS_USE_PSA_CRYPTO)
3743 psa_pake_abort( &handshake->psa_pake_ctx );
3744 psa_destroy_key( handshake->psa_pake_password );
3745 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
3746#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003747 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02003748#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003749#if defined(MBEDTLS_SSL_CLI_C)
3750 mbedtls_free( handshake->ecjpake_cache );
3751 handshake->ecjpake_cache = NULL;
3752 handshake->ecjpake_cache_len = 0;
3753#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003754#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003755
Janos Follath4ae5c292016-02-10 11:27:43 +00003756#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3757 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003758 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003760#endif
3761
Ronald Cron73fe8df2022-10-05 14:31:43 +02003762#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003763#if defined(MBEDTLS_USE_PSA_CRYPTO)
3764 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3765 {
3766 /* The maintenance of the external PSK key slot is the
3767 * user's responsibility. */
3768 if( ssl->handshake->psk_opaque_is_internal )
3769 {
3770 psa_destroy_key( ssl->handshake->psk_opaque );
3771 ssl->handshake->psk_opaque_is_internal = 0;
3772 }
3773 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3774 }
Neil Armstronge952a302022-05-03 10:22:14 +02003775#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003776 if( handshake->psk != NULL )
3777 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003778 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003779 mbedtls_free( handshake->psk );
3780 }
Neil Armstronge952a302022-05-03 10:22:14 +02003781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003782#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3785 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003786 /*
3787 * Free only the linked list wrapper, not the keys themselves
3788 * since the belong to the SNI callback
3789 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003790 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003792
Gilles Peskineeccd8882020-03-10 12:19:08 +01003793#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003794 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003795 if( handshake->ecrs_peer_cert != NULL )
3796 {
3797 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3798 mbedtls_free( handshake->ecrs_peer_cert );
3799 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003800#endif
3801
Hanno Becker75173122019-02-06 16:18:31 +00003802#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3803 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3804 mbedtls_pk_free( &handshake->peer_pubkey );
3805#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3806
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003807#if defined(MBEDTLS_SSL_CLI_C) && \
3808 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3809 mbedtls_free( handshake->cookie );
3810#endif /* MBEDTLS_SSL_CLI_C &&
3811 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003812
3813#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003814 mbedtls_ssl_flight_free( handshake->flight );
3815 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003816#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003817
Ronald Cronf12b81d2022-03-15 10:42:41 +01003818#if defined(MBEDTLS_ECDH_C) && \
3819 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003820 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003821 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003822#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3823
Ronald Cron6f135e12021-12-08 16:57:54 +01003824#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003825 mbedtls_ssl_transform_free( handshake->transform_handshake );
3826 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003827 mbedtls_free( handshake->transform_earlydata );
3828 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003829#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003830
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003831
3832#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3833 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3834 * processes datagrams and the fact that a datagram is allowed to have
3835 * several records in it, it is possible that the I/O buffers are not
3836 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02003837 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
3838 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003839#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01003840
Jerry Yuba9c7272021-10-30 11:54:10 +08003841 /* mbedtls_platform_zeroize MUST be last one in this function */
3842 mbedtls_platform_zeroize( handshake,
3843 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003844}
3845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00003847{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003848 if( session == NULL )
3849 return;
3850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00003852 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02003853#endif
Paul Bakker0a597072012-09-25 21:55:46 +00003854
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003855#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00003856#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
3857 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00003858 mbedtls_free( session->hostname );
3859#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02003861#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02003862
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003863 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003864}
3865
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02003866#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003867
3868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3869#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
3870#else
3871#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
3872#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3873
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003874#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003875
3876#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3877#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
3878#else
3879#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
3880#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
3881
3882#if defined(MBEDTLS_SSL_ALPN)
3883#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
3884#else
3885#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
3886#endif /* MBEDTLS_SSL_ALPN */
3887
3888#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
3889#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
3890#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
3891#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
3892
3893#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
3894 ( (uint32_t) ( \
3895 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
3896 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
3897 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
3898 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
3899 0u ) )
3900
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003901static unsigned char ssl_serialized_context_header[] = {
3902 MBEDTLS_VERSION_MAJOR,
3903 MBEDTLS_VERSION_MINOR,
3904 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003905 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3906 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3907 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3908 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
3909 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003910};
3911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003913 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003914 *
3915 * The format of the serialized data is:
3916 * (in the presentation language of TLS, RFC 8446 section 3)
3917 *
3918 * // header
3919 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003920 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003921 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003922 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02003923 * Note: When updating the format, remember to keep these
3924 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02003925 *
3926 * // session sub-structure
3927 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
3928 * // transform sub-structure
3929 * uint8 random[64]; // ServerHello.random+ClientHello.random
3930 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
3931 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
3932 * // fields from ssl_context
3933 * uint32 badmac_seen; // DTLS: number of records with failing MAC
3934 * uint64 in_window_top; // DTLS: last validated record seq_num
3935 * uint64 in_window; // DTLS: bitmask for replay protection
3936 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
3937 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
3938 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
3939 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
3940 *
3941 * Note that many fields of the ssl_context or sub-structures are not
3942 * serialized, as they fall in one of the following categories:
3943 *
3944 * 1. forced value (eg in_left must be 0)
3945 * 2. pointer to dynamically-allocated memory (eg session, transform)
3946 * 3. value can be re-derived from other data (eg session keys from MS)
3947 * 4. value was temporary (eg content of input buffer)
3948 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02003949 */
3950int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
3951 unsigned char *buf,
3952 size_t buf_len,
3953 size_t *olen )
3954{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003955 unsigned char *p = buf;
3956 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02003957 size_t session_len;
3958 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02003959
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003960 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003961 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
3962 * this function's documentation.
3963 *
3964 * These are due to assumptions/limitations in the implementation. Some of
3965 * them are likely to stay (no handshake in progress) some might go away
3966 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003967 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003968 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00003969 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003970 {
3971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02003972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003973 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003974 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003975 {
3976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003978 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003979 /* Double-check that sub-structures are indeed ready */
3980 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003981 {
3982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003984 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003985 /* There must be no pending incoming or outgoing data */
3986 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003987 {
3988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003989 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003990 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003991 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003992 {
3993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003995 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02003996 /* Protocol must be DLTS, not TLS */
3997 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03003998 {
3999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004001 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004002 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004003 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004004 {
4005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004007 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004008 /* We must be using an AEAD ciphersuite */
4009 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004010 {
4011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004013 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004014 /* Renegotiation must not be enabled */
4015#if defined(MBEDTLS_SSL_RENEGOTIATION)
4016 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004017 {
4018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004020 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004021#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004022
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004023 /*
4024 * Version and format identifier
4025 */
4026 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004027
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004028 if( used <= buf_len )
4029 {
4030 memcpy( p, ssl_serialized_context_header,
4031 sizeof( ssl_serialized_context_header ) );
4032 p += sizeof( ssl_serialized_context_header );
4033 }
4034
4035 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004036 * Session (length + data)
4037 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004038 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004039 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4040 return( ret );
4041
4042 used += 4 + session_len;
4043 if( used <= buf_len )
4044 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004045 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4046 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004047
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004048 ret = ssl_session_save( ssl->session, 1,
4049 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004050 if( ret != 0 )
4051 return( ret );
4052
4053 p += session_len;
4054 }
4055
4056 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004057 * Transform
4058 */
4059 used += sizeof( ssl->transform->randbytes );
4060 if( used <= buf_len )
4061 {
4062 memcpy( p, ssl->transform->randbytes,
4063 sizeof( ssl->transform->randbytes ) );
4064 p += sizeof( ssl->transform->randbytes );
4065 }
4066
4067#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4068 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4069 if( used <= buf_len )
4070 {
4071 *p++ = ssl->transform->in_cid_len;
4072 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4073 p += ssl->transform->in_cid_len;
4074
4075 *p++ = ssl->transform->out_cid_len;
4076 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4077 p += ssl->transform->out_cid_len;
4078 }
4079#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4080
4081 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004082 * Saved fields from top-level ssl_context structure
4083 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004084 used += 4;
4085 if( used <= buf_len )
4086 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004087 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4088 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004089 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004090
4091#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4092 used += 16;
4093 if( used <= buf_len )
4094 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004095 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4096 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004097
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004098 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4099 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004100 }
4101#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4102
4103#if defined(MBEDTLS_SSL_PROTO_DTLS)
4104 used += 1;
4105 if( used <= buf_len )
4106 {
4107 *p++ = ssl->disable_datagram_packing;
4108 }
4109#endif /* MBEDTLS_SSL_PROTO_DTLS */
4110
Jerry Yuae0b2e22021-10-08 15:21:19 +08004111 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004112 if( used <= buf_len )
4113 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004114 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4115 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004116 }
4117
4118#if defined(MBEDTLS_SSL_PROTO_DTLS)
4119 used += 2;
4120 if( used <= buf_len )
4121 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004122 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4123 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004124 }
4125#endif /* MBEDTLS_SSL_PROTO_DTLS */
4126
4127#if defined(MBEDTLS_SSL_ALPN)
4128 {
4129 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004130 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004131 : 0;
4132
4133 used += 1 + alpn_len;
4134 if( used <= buf_len )
4135 {
4136 *p++ = alpn_len;
4137
4138 if( ssl->alpn_chosen != NULL )
4139 {
4140 memcpy( p, ssl->alpn_chosen, alpn_len );
4141 p += alpn_len;
4142 }
4143 }
4144 }
4145#endif /* MBEDTLS_SSL_ALPN */
4146
4147 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004148 * Done
4149 */
4150 *olen = used;
4151
4152 if( used > buf_len )
4153 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004154
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004155 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4156
Hanno Becker43aefe22020-02-05 10:44:56 +00004157 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004158}
4159
4160/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004161 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004162 *
4163 * This internal version is wrapped by a public function that cleans up in
4164 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004165 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004166MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004167static int ssl_context_load( mbedtls_ssl_context *ssl,
4168 const unsigned char *buf,
4169 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004170{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004171 const unsigned char *p = buf;
4172 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004173 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004176 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004177#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004178
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004179 /*
4180 * The context should have been freshly setup or reset.
4181 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004182 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004183 * renegotiating, or if the user mistakenly loaded a session first.)
4184 */
4185 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4186 ssl->session != NULL )
4187 {
4188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4189 }
4190
4191 /*
4192 * We can't check that the config matches the initial one, but we can at
4193 * least check it matches the requirements for serializing.
4194 */
4195 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004196 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4197 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004198#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004199 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004200#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004201 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004202 {
4203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4204 }
4205
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004206 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4207
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004208 /*
4209 * Check version identifier
4210 */
4211 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4213
4214 if( memcmp( p, ssl_serialized_context_header,
4215 sizeof( ssl_serialized_context_header ) ) != 0 )
4216 {
4217 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4218 }
4219 p += sizeof( ssl_serialized_context_header );
4220
4221 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004222 * Session
4223 */
4224 if( (size_t)( end - p ) < 4 )
4225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4226
4227 session_len = ( (size_t) p[0] << 24 ) |
4228 ( (size_t) p[1] << 16 ) |
4229 ( (size_t) p[2] << 8 ) |
4230 ( (size_t) p[3] );
4231 p += 4;
4232
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004233 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004234 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004235 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004236 ssl->session_in = ssl->session;
4237 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004238 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004239
4240 if( (size_t)( end - p ) < session_len )
4241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4242
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004243 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004244 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004245 {
4246 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004247 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004248 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004249
4250 p += session_len;
4251
4252 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004253 * Transform
4254 */
4255
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004256 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004257 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004258 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004259 ssl->transform_in = ssl->transform;
4260 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004261 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004262
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004264 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4265 if( prf_func == NULL )
4266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4267
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004268 /* Read random bytes and populate structure */
4269 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004271
Hanno Beckerbd257552021-03-22 06:59:27 +00004272 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004273 ssl->session->ciphersuite,
4274 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004275#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004276 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004277#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004278 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004279 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004280 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004281 ssl->conf->endpoint,
4282 ssl );
4283 if( ret != 0 )
4284 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004285#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004286 p += sizeof( ssl->transform->randbytes );
4287
4288#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4289 /* Read connection IDs and store them */
4290 if( (size_t)( end - p ) < 1 )
4291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4292
4293 ssl->transform->in_cid_len = *p++;
4294
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004295 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4297
4298 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4299 p += ssl->transform->in_cid_len;
4300
4301 ssl->transform->out_cid_len = *p++;
4302
4303 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4305
4306 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4307 p += ssl->transform->out_cid_len;
4308#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4309
4310 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004311 * Saved fields from top-level ssl_context structure
4312 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004313 if( (size_t)( end - p ) < 4 )
4314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4315
4316 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4317 ( (uint32_t) p[1] << 16 ) |
4318 ( (uint32_t) p[2] << 8 ) |
4319 ( (uint32_t) p[3] );
4320 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004321
4322#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4323 if( (size_t)( end - p ) < 16 )
4324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4325
4326 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4327 ( (uint64_t) p[1] << 48 ) |
4328 ( (uint64_t) p[2] << 40 ) |
4329 ( (uint64_t) p[3] << 32 ) |
4330 ( (uint64_t) p[4] << 24 ) |
4331 ( (uint64_t) p[5] << 16 ) |
4332 ( (uint64_t) p[6] << 8 ) |
4333 ( (uint64_t) p[7] );
4334 p += 8;
4335
4336 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4337 ( (uint64_t) p[1] << 48 ) |
4338 ( (uint64_t) p[2] << 40 ) |
4339 ( (uint64_t) p[3] << 32 ) |
4340 ( (uint64_t) p[4] << 24 ) |
4341 ( (uint64_t) p[5] << 16 ) |
4342 ( (uint64_t) p[6] << 8 ) |
4343 ( (uint64_t) p[7] );
4344 p += 8;
4345#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4346
4347#if defined(MBEDTLS_SSL_PROTO_DTLS)
4348 if( (size_t)( end - p ) < 1 )
4349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4350
4351 ssl->disable_datagram_packing = *p++;
4352#endif /* MBEDTLS_SSL_PROTO_DTLS */
4353
Jerry Yud9a94fe2021-09-28 18:58:59 +08004354 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004356 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4357 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004358
4359#if defined(MBEDTLS_SSL_PROTO_DTLS)
4360 if( (size_t)( end - p ) < 2 )
4361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4362
4363 ssl->mtu = ( p[0] << 8 ) | p[1];
4364 p += 2;
4365#endif /* MBEDTLS_SSL_PROTO_DTLS */
4366
4367#if defined(MBEDTLS_SSL_ALPN)
4368 {
4369 uint8_t alpn_len;
4370 const char **cur;
4371
4372 if( (size_t)( end - p ) < 1 )
4373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4374
4375 alpn_len = *p++;
4376
4377 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4378 {
4379 /* alpn_chosen should point to an item in the configured list */
4380 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4381 {
4382 if( strlen( *cur ) == alpn_len &&
4383 memcmp( p, cur, alpn_len ) == 0 )
4384 {
4385 ssl->alpn_chosen = *cur;
4386 break;
4387 }
4388 }
4389 }
4390
4391 /* can only happen on conf mismatch */
4392 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4394
4395 p += alpn_len;
4396 }
4397#endif /* MBEDTLS_SSL_ALPN */
4398
4399 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004400 * Forced fields from top-level ssl_context structure
4401 *
4402 * Most of them already set to the correct value by mbedtls_ssl_init() and
4403 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4404 */
4405 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004406 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004407
Hanno Becker361b10d2019-08-30 10:42:49 +01004408 /* Adjust pointers for header fields of outgoing records to
4409 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004410 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004411
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004412#if defined(MBEDTLS_SSL_PROTO_DTLS)
4413 ssl->in_epoch = 1;
4414#endif
4415
4416 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4417 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004418 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4419 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004420 if( ssl->handshake != NULL )
4421 {
4422 mbedtls_ssl_handshake_free( ssl );
4423 mbedtls_free( ssl->handshake );
4424 ssl->handshake = NULL;
4425 }
4426
4427 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004428 * Done - should have consumed entire buffer
4429 */
4430 if( p != end )
4431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004432
4433 return( 0 );
4434}
4435
4436/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004437 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004438 */
4439int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4440 const unsigned char *buf,
4441 size_t len )
4442{
4443 int ret = ssl_context_load( context, buf, len );
4444
4445 if( ret != 0 )
4446 mbedtls_ssl_free( context );
4447
4448 return( ret );
4449}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004450#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004451
4452/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 * Free an SSL context
4454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004456{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004457 if( ssl == NULL )
4458 return;
4459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004461
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004462 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 {
sander-visserb8aa2072020-05-06 22:05:13 +02004464#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4465 size_t out_buf_len = ssl->out_buf_len;
4466#else
4467 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4468#endif
4469
Darryl Greenb33cc762019-11-28 14:29:44 +00004470 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004472 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 }
4474
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004475 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 {
sander-visserb8aa2072020-05-06 22:05:13 +02004477#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4478 size_t in_buf_len = ssl->in_buf_len;
4479#else
4480 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4481#endif
4482
Darryl Greenb33cc762019-11-28 14:29:44 +00004483 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004485 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 }
4487
Paul Bakker48916f92012-09-16 19:57:18 +00004488 if( ssl->transform )
4489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 mbedtls_ssl_transform_free( ssl->transform );
4491 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004492 }
4493
4494 if( ssl->handshake )
4495 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004496 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4498 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 mbedtls_free( ssl->handshake );
4501 mbedtls_free( ssl->transform_negotiate );
4502 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004503 }
4504
Ronald Cron6f135e12021-12-08 16:57:54 +01004505#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004506 mbedtls_ssl_transform_free( ssl->transform_application );
4507 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004508#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004509
Paul Bakkerc0463502013-02-14 11:19:38 +01004510 if( ssl->session )
4511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 mbedtls_ssl_session_free( ssl->session );
4513 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004514 }
4515
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004516#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004517 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004519 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004523
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004524#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004526#endif
4527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004529
Paul Bakker86f04f42013-02-14 11:20:09 +01004530 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004531 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004532}
4533
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004534/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004535 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004536 */
4537void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4538{
4539 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4540}
4541
Gilles Peskineae270bf2021-06-02 00:05:29 +02004542/* The selection should be the same as mbedtls_x509_crt_profile_default in
4543 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004544 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004545 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004546 * about this list.
4547 */
Brett Warrene0edc842021-08-17 09:53:13 +01004548static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004549#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004550 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004551#endif
4552#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004553 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004554#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004555#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004556 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004557#endif
4558#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004559 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004560#endif
4561#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004562 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004563#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004564#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004565 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004566#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004567#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004568 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004569#endif
4570#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004571 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004572#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004573 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004574};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004575
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004576static int ssl_preset_suiteb_ciphersuites[] = {
4577 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4578 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4579 0
4580};
4581
Ronald Crone68ab4f2022-10-05 12:46:29 +02004582#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004583
Jerry Yu909df7b2022-01-22 11:56:27 +08004584/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004585 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004586 * rules SHOULD be upheld.
4587 * - No duplicate entries.
4588 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004589 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004590 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004591 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004592static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004593
Andrzej Kurek25f27152022-08-17 16:09:31 -04004594#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 +08004595 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4596 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004597#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004598 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004599
Andrzej Kurek25f27152022-08-17 16:09:31 -04004600#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 +08004601 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4602 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004603#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004604 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4605
Andrzej Kurek25f27152022-08-17 16:09:31 -04004606#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 +08004607 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4608 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004609#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004610 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004611
Andrzej Kurek25f27152022-08-17 16:09:31 -04004612#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 +08004613 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004614#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 +08004615
Andrzej Kurek25f27152022-08-17 16:09:31 -04004616#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 +08004617 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004618#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 +08004619
Andrzej Kurek25f27152022-08-17 16:09:31 -04004620#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 +08004621 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004622#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 +08004623
Andrzej Kurek25f27152022-08-17 16:09:31 -04004624#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 +08004625 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4626#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4627
Andrzej Kurek25f27152022-08-17 16:09:31 -04004628#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 +08004629 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4630#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4631
Andrzej Kurek25f27152022-08-17 16:09:31 -04004632#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 +08004633 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4634#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4635
Gabor Mezei15b95a62022-05-09 16:37:58 +02004636 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004637};
4638
4639/* NOTICE: see above */
4640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4641static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004642#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004643#if defined(MBEDTLS_ECDSA_C)
4644 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004645#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004646#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4647 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4648#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004649#if defined(MBEDTLS_RSA_C)
4650 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4651#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004652#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004653#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004654#if defined(MBEDTLS_ECDSA_C)
4655 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004656#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004657#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4658 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4659#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004660#if defined(MBEDTLS_RSA_C)
4661 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4662#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004663#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004664#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004665#if defined(MBEDTLS_ECDSA_C)
4666 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004667#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004668#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4669 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4670#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004671#if defined(MBEDTLS_RSA_C)
4672 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4673#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004674#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004675 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004676};
4677#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4678/* NOTICE: see above */
4679static uint16_t ssl_preset_suiteb_sig_algs[] = {
4680
Andrzej Kurek25f27152022-08-17 16:09:31 -04004681#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 +08004682 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4683 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004684#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004685 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4686
Andrzej Kurek25f27152022-08-17 16:09:31 -04004687#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 +08004688 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4689 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004690#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004691 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004692
Andrzej Kurek25f27152022-08-17 16:09:31 -04004693#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 +08004694 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004695#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 +08004696
Andrzej Kurek25f27152022-08-17 16:09:31 -04004697#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 +08004698 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004699#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004700
Gabor Mezei15b95a62022-05-09 16:37:58 +02004701 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004702};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004703
Jerry Yu909df7b2022-01-22 11:56:27 +08004704/* NOTICE: see above */
4705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4706static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004707#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004708#if defined(MBEDTLS_ECDSA_C)
4709 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004710#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004711#if defined(MBEDTLS_RSA_C)
4712 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4713#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004714#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004715#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004716#if defined(MBEDTLS_ECDSA_C)
4717 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004718#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004719#if defined(MBEDTLS_RSA_C)
4720 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4721#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004722#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004723 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004724};
Jerry Yu909df7b2022-01-22 11:56:27 +08004725#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4726
Ronald Crone68ab4f2022-10-05 12:46:29 +02004727#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004728
Brett Warrene0edc842021-08-17 09:53:13 +01004729static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004730#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004731 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004732#endif
4733#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004734 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004735#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004736 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004737};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004738
Ronald Crone68ab4f2022-10-05 12:46:29 +02004739#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004740/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004741 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004742MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004743static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004744{
4745 size_t i, j;
4746 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004747
Gabor Mezei15b95a62022-05-09 16:37:58 +02004748 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004749 {
Jerry Yuf377d642022-01-25 10:43:59 +08004750 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004751 {
Jerry Yuf377d642022-01-25 10:43:59 +08004752 if( sig_algs[i] != sig_algs[j] )
4753 continue;
4754 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4755 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4756 sig_algs[i], j, i );
4757 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004758 }
4759 }
4760 return( ret );
4761}
4762
Ronald Crone68ab4f2022-10-05 12:46:29 +02004763#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004764
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004765/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004766 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004767 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004768int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004769 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004770{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004771#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004773#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004774
Ronald Crone68ab4f2022-10-05 12:46:29 +02004775#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004776 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004777 {
4778 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4779 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4780 }
4781
Jerry Yuf377d642022-01-25 10:43:59 +08004782 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004783 {
4784 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4785 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4786 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004787
4788#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004789 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004790 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004791 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004792 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4793 }
4794
Jerry Yuf377d642022-01-25 10:43:59 +08004795 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004796 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004797 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004798 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4799 }
4800#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004801#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004802
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004803 /* Use the functions here so that they are covered in tests,
4804 * but otherwise access member directly for efficiency */
4805 mbedtls_ssl_conf_endpoint( conf, endpoint );
4806 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004807
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004808 /*
4809 * Things that are common to all presets
4810 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004811#if defined(MBEDTLS_SSL_CLI_C)
4812 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4813 {
4814 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4815#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4816 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4817#endif
4818 }
4819#endif
4820
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004821#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4822 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4823#endif
4824
4825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4826 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4827#endif
4828
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004829#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004830 conf->f_cookie_write = ssl_cookie_write_dummy;
4831 conf->f_cookie_check = ssl_cookie_check_dummy;
4832#endif
4833
4834#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4835 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
4836#endif
4837
Janos Follath088ce432017-04-10 12:42:31 +01004838#if defined(MBEDTLS_SSL_SRV_C)
4839 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02004840 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01004841#endif
4842
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004843#if defined(MBEDTLS_SSL_PROTO_DTLS)
4844 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
4845 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
4846#endif
4847
4848#if defined(MBEDTLS_SSL_RENEGOTIATION)
4849 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00004850 memset( conf->renego_period, 0x00, 2 );
4851 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004852#endif
4853
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004854#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01004855 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4856 {
4857 const unsigned char dhm_p[] =
4858 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
4859 const unsigned char dhm_g[] =
4860 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01004861
Hanno Beckere2defad2021-07-24 05:59:17 +01004862 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
4863 dhm_p, sizeof( dhm_p ),
4864 dhm_g, sizeof( dhm_g ) ) ) != 0 )
4865 {
4866 return( ret );
4867 }
4868 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004869#endif
4870
Ronald Cron6f135e12021-12-08 16:57:54 +01004871#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08004872#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08004873 mbedtls_ssl_conf_new_session_tickets(
4874 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
4875#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01004876 /*
4877 * Allow all TLS 1.3 key exchange modes by default.
4878 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004879 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01004880#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01004881
XiaokangQian4d3a6042022-04-21 13:46:17 +00004882 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4883 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00004885 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00004886 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00004887#else
XiaokangQian060d8672022-04-21 09:24:56 +00004888 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00004889#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00004890 }
4891 else
4892 {
4893#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4894 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4895 {
4896 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4897 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4898 }
4899 else
4900 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
4901 {
4902 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4903 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4904 }
4905#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
4906 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4907 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
4908#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4910 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
4911#else
4912 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4913#endif
4914 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004915
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004916 /*
4917 * Preset-specific defaults
4918 */
4919 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004920 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004921 /*
4922 * NSA Suite B
4923 */
4924 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004925
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004926 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004927
4928#if defined(MBEDTLS_X509_CRT_PARSE_C)
4929 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004930#endif
4931
Ronald Crone68ab4f2022-10-05 12:46:29 +02004932#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4934 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4935 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
4936 else
4937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4938 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004939#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004940
Brett Warrene0edc842021-08-17 09:53:13 +01004941#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4942 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004943#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004944 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02004945 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004946
4947 /*
4948 * Default
4949 */
4950 default:
Ronald Cronf6606552022-03-15 11:23:25 +01004951
Hanno Beckerd60b6c62021-04-29 12:04:11 +01004952 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004953
4954#if defined(MBEDTLS_X509_CRT_PARSE_C)
4955 conf->cert_profile = &mbedtls_x509_crt_profile_default;
4956#endif
4957
Ronald Crone68ab4f2022-10-05 12:46:29 +02004958#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004959#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4960 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
4961 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
4962 else
4963#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4964 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02004965#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004966
Brett Warrene0edc842021-08-17 09:53:13 +01004967#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
4968 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004969#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004970 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004971
4972#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4973 conf->dhm_min_bitlen = 1024;
4974#endif
4975 }
4976
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004977 return( 0 );
4978}
4979
4980/*
4981 * Free mbedtls_ssl_config
4982 */
4983void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
4984{
4985#if defined(MBEDTLS_DHM_C)
4986 mbedtls_mpi_free( &conf->dhm_P );
4987 mbedtls_mpi_free( &conf->dhm_G );
4988#endif
4989
Ronald Cron73fe8df2022-10-05 14:31:43 +02004990#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004991#if defined(MBEDTLS_USE_PSA_CRYPTO)
4992 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
4993 {
Neil Armstrong501c9322022-05-03 09:35:09 +02004994 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4995 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02004996#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004997 if( conf->psk != NULL )
4998 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004999 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005000 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005001 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005002 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005003 }
5004
5005 if( conf->psk_identity != NULL )
5006 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005007 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005008 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005009 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005010 conf->psk_identity_len = 0;
5011 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005012#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005013
5014#if defined(MBEDTLS_X509_CRT_PARSE_C)
5015 ssl_key_cert_free( conf->key_cert );
5016#endif
5017
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005018 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005019}
5020
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005021#if defined(MBEDTLS_PK_C) && \
5022 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005023/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#if defined(MBEDTLS_RSA_C)
5029 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5030 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032#if defined(MBEDTLS_ECDSA_C)
5033 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5034 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005037}
5038
Hanno Becker7e5437a2017-04-28 17:15:26 +01005039unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5040{
5041 switch( type ) {
5042 case MBEDTLS_PK_RSA:
5043 return( MBEDTLS_SSL_SIG_RSA );
5044 case MBEDTLS_PK_ECDSA:
5045 case MBEDTLS_PK_ECKEY:
5046 return( MBEDTLS_SSL_SIG_ECDSA );
5047 default:
5048 return( MBEDTLS_SSL_SIG_ANON );
5049 }
5050}
5051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005053{
5054 switch( sig )
5055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if defined(MBEDTLS_RSA_C)
5057 case MBEDTLS_SSL_SIG_RSA:
5058 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if defined(MBEDTLS_ECDSA_C)
5061 case MBEDTLS_SSL_SIG_ECDSA:
5062 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005063#endif
5064 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005066 }
5067}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005068#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005069
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005070/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005071 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005074{
5075 switch( hash )
5076 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005077#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 case MBEDTLS_SSL_HASH_MD5:
5079 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005080#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005081#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 case MBEDTLS_SSL_HASH_SHA1:
5083 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005084#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005085#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 case MBEDTLS_SSL_HASH_SHA224:
5087 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005088#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005089#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 case MBEDTLS_SSL_HASH_SHA256:
5091 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005092#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005093#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 case MBEDTLS_SSL_HASH_SHA384:
5095 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005096#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005097#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 case MBEDTLS_SSL_HASH_SHA512:
5099 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005100#endif
5101 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005103 }
5104}
5105
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005106/*
5107 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5108 */
5109unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5110{
5111 switch( md )
5112 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005113#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005114 case MBEDTLS_MD_MD5:
5115 return( MBEDTLS_SSL_HASH_MD5 );
5116#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005117#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005118 case MBEDTLS_MD_SHA1:
5119 return( MBEDTLS_SSL_HASH_SHA1 );
5120#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005121#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005122 case MBEDTLS_MD_SHA224:
5123 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005124#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005125#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005126 case MBEDTLS_MD_SHA256:
5127 return( MBEDTLS_SSL_HASH_SHA256 );
5128#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005129#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005130 case MBEDTLS_MD_SHA384:
5131 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005132#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005133#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005134 case MBEDTLS_MD_SHA512:
5135 return( MBEDTLS_SSL_HASH_SHA512 );
5136#endif
5137 default:
5138 return( MBEDTLS_SSL_HASH_NONE );
5139 }
5140}
5141
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005142/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005143 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005144 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005145 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005146int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005147{
Brett Warrene0edc842021-08-17 09:53:13 +01005148 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005149
Brett Warrene0edc842021-08-17 09:53:13 +01005150 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005151 return( -1 );
5152
Brett Warrene0edc842021-08-17 09:53:13 +01005153 for( ; *group_list != 0; group_list++ )
5154 {
5155 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005156 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005157 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005158
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005159 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005160}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005161
5162#if defined(MBEDTLS_ECP_C)
5163/*
5164 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5165 */
5166int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5167{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005168 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005169 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005170
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005171 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005172 return -1;
5173
5174 uint16_t tls_id = grp_info->tls_id;
5175
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005176 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5177}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005178#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180#if defined(MBEDTLS_X509_CRT_PARSE_C)
5181int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5182 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005183 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005184 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005185{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005186 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005187 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005188 const char *ext_oid;
5189 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005192 {
5193 /* Server part of the key exchange */
5194 switch( ciphersuite->key_exchange )
5195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196 case MBEDTLS_KEY_EXCHANGE_RSA:
5197 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005198 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005199 break;
5200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5202 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5203 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5204 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005205 break;
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5208 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005209 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005210 break;
5211
5212 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 case MBEDTLS_KEY_EXCHANGE_NONE:
5214 case MBEDTLS_KEY_EXCHANGE_PSK:
5215 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5216 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005217 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005218 usage = 0;
5219 }
5220 }
5221 else
5222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5224 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005225 }
5226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005228 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005229 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005230 ret = -1;
5231 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5236 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005237 }
5238 else
5239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5241 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005242 }
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005245 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005246 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005247 ret = -1;
5248 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005249
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005250 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005253
Jerry Yu148165c2021-09-24 23:20:59 +08005254#if defined(MBEDTLS_USE_PSA_CRYPTO)
5255int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5256 const mbedtls_md_type_t md,
5257 unsigned char *dst,
5258 size_t dst_len,
5259 size_t *olen )
5260{
Ronald Cronf6893e12022-01-07 22:09:01 +01005261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5262 psa_hash_operation_t *hash_operation_to_clone;
5263 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5264
Jerry Yu148165c2021-09-24 23:20:59 +08005265 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005266
5267 switch( md )
5268 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005269#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005270 case MBEDTLS_MD_SHA384:
5271 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5272 break;
5273#endif
5274
Andrzej Kurek25f27152022-08-17 16:09:31 -04005275#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005276 case MBEDTLS_MD_SHA256:
5277 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5278 break;
5279#endif
5280
5281 default:
5282 goto exit;
5283 }
5284
5285 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5286 if( status != PSA_SUCCESS )
5287 goto exit;
5288
5289 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5290 if( status != PSA_SUCCESS )
5291 goto exit;
5292
5293exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005294#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5295 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5296 (void) ssl;
5297#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005298 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005299}
5300#else /* MBEDTLS_USE_PSA_CRYPTO */
5301
Andrzej Kurek25f27152022-08-17 16:09:31 -04005302#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005303MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005304static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5305 unsigned char *dst,
5306 size_t dst_len,
5307 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005308{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005309 int ret;
5310 mbedtls_sha512_context sha512;
5311
5312 if( dst_len < 48 )
5313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5314
5315 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005316 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005317
5318 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5319 {
5320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5321 goto exit;
5322 }
5323
5324 *olen = 48;
5325
5326exit:
5327
5328 mbedtls_sha512_free( &sha512 );
5329 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005330}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005331#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005332
Andrzej Kurek25f27152022-08-17 16:09:31 -04005333#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005334MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005335static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5336 unsigned char *dst,
5337 size_t dst_len,
5338 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005339{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005340 int ret;
5341 mbedtls_sha256_context sha256;
5342
5343 if( dst_len < 32 )
5344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5345
5346 mbedtls_sha256_init( &sha256 );
5347 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005348
Jerry Yu24c0ec32021-09-09 14:21:07 +08005349 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5350 {
5351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5352 goto exit;
5353 }
5354
5355 *olen = 32;
5356
5357exit:
5358
5359 mbedtls_sha256_free( &sha256 );
5360 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005361}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005362#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005363
Jerry Yu000f9762021-09-14 11:12:51 +08005364int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5365 const mbedtls_md_type_t md,
5366 unsigned char *dst,
5367 size_t dst_len,
5368 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005369{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005370 switch( md )
5371 {
5372
Andrzej Kurek25f27152022-08-17 16:09:31 -04005373#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005374 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005375 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005376#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005377
Andrzej Kurek25f27152022-08-17 16:09:31 -04005378#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005379 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005380 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005381#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005382
5383 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005384#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5385 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5386 (void) ssl;
5387 (void) dst;
5388 (void) dst_len;
5389 (void) olen;
5390#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005391 break;
5392 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005394}
XiaokangQian647719a2021-12-07 09:16:29 +00005395
Jerry Yu148165c2021-09-24 23:20:59 +08005396#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005397
Ronald Crone68ab4f2022-10-05 12:46:29 +02005398#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005399/* mbedtls_ssl_parse_sig_alg_ext()
5400 *
5401 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5402 * value (TLS 1.3 RFC8446):
5403 * enum {
5404 * ....
5405 * ecdsa_secp256r1_sha256( 0x0403 ),
5406 * ecdsa_secp384r1_sha384( 0x0503 ),
5407 * ecdsa_secp521r1_sha512( 0x0603 ),
5408 * ....
5409 * } SignatureScheme;
5410 *
5411 * struct {
5412 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5413 * } SignatureSchemeList;
5414 *
5415 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5416 * value (TLS 1.2 RFC5246):
5417 * enum {
5418 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5419 * sha512(6), (255)
5420 * } HashAlgorithm;
5421 *
5422 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5423 * SignatureAlgorithm;
5424 *
5425 * struct {
5426 * HashAlgorithm hash;
5427 * SignatureAlgorithm signature;
5428 * } SignatureAndHashAlgorithm;
5429 *
5430 * SignatureAndHashAlgorithm
5431 * supported_signature_algorithms<2..2^16-2>;
5432 *
5433 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5434 * generalization of the TLS 1.2 signature algorithm extension.
5435 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5436 * `SignatureScheme` field of TLS 1.3
5437 *
5438 */
5439int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5440 const unsigned char *buf,
5441 const unsigned char *end )
5442{
5443 const unsigned char *p = buf;
5444 size_t supported_sig_algs_len = 0;
5445 const unsigned char *supported_sig_algs_end;
5446 uint16_t sig_alg;
5447 uint32_t common_idx = 0;
5448
5449 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5450 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5451 p += 2;
5452
5453 memset( ssl->handshake->received_sig_algs, 0,
5454 sizeof(ssl->handshake->received_sig_algs) );
5455
5456 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5457 supported_sig_algs_end = p + supported_sig_algs_len;
5458 while( p < supported_sig_algs_end )
5459 {
5460 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5461 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5462 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005463 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5464 sig_alg,
5465 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005467 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005468 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5469 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5470 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005471 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005472 }
5473#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005474
Jerry Yuf3b46b52022-06-19 16:52:27 +08005475 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5476 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005477
5478 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5479 {
5480 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5481 common_idx += 1;
5482 }
5483 }
5484 /* Check that we consumed all the message. */
5485 if( p != end )
5486 {
5487 MBEDTLS_SSL_DEBUG_MSG( 1,
5488 ( "Signature algorithms extension length misaligned" ) );
5489 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5490 MBEDTLS_ERR_SSL_DECODE_ERROR );
5491 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5492 }
5493
5494 if( common_idx == 0 )
5495 {
5496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5497 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5498 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5499 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5500 }
5501
Gabor Mezei15b95a62022-05-09 16:37:58 +02005502 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005503 return( 0 );
5504}
5505
Ronald Crone68ab4f2022-10-05 12:46:29 +02005506#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005507
Jerry Yudc7bd172022-02-17 13:44:15 +08005508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5509
5510#if defined(MBEDTLS_USE_PSA_CRYPTO)
5511
5512static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5513 mbedtls_svc_key_id_t key,
5514 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005515 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005516 const unsigned char* seed, size_t seed_length,
5517 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005518 const unsigned char* other_secret,
5519 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005520 size_t capacity )
5521{
5522 psa_status_t status;
5523
5524 status = psa_key_derivation_setup( derivation, alg );
5525 if( status != PSA_SUCCESS )
5526 return( status );
5527
5528 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5529 {
5530 status = psa_key_derivation_input_bytes( derivation,
5531 PSA_KEY_DERIVATION_INPUT_SEED,
5532 seed, seed_length );
5533 if( status != PSA_SUCCESS )
5534 return( status );
5535
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005536 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005537 {
5538 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005539 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5540 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005541 if( status != PSA_SUCCESS )
5542 return( status );
5543 }
5544
Jerry Yudc7bd172022-02-17 13:44:15 +08005545 if( mbedtls_svc_key_id_is_null( key ) )
5546 {
5547 status = psa_key_derivation_input_bytes(
5548 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005549 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005550 }
5551 else
5552 {
5553 status = psa_key_derivation_input_key(
5554 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5555 }
5556 if( status != PSA_SUCCESS )
5557 return( status );
5558
5559 status = psa_key_derivation_input_bytes( derivation,
5560 PSA_KEY_DERIVATION_INPUT_LABEL,
5561 label, label_length );
5562 if( status != PSA_SUCCESS )
5563 return( status );
5564 }
5565 else
5566 {
5567 return( PSA_ERROR_NOT_SUPPORTED );
5568 }
5569
5570 status = psa_key_derivation_set_capacity( derivation, capacity );
5571 if( status != PSA_SUCCESS )
5572 return( status );
5573
5574 return( PSA_SUCCESS );
5575}
5576
Andrzej Kurek57d10632022-10-24 10:32:01 -04005577#if defined(PSA_WANT_ALG_SHA_384) || \
5578 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005579MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005580static int tls_prf_generic( mbedtls_md_type_t md_type,
5581 const unsigned char *secret, size_t slen,
5582 const char *label,
5583 const unsigned char *random, size_t rlen,
5584 unsigned char *dstbuf, size_t dlen )
5585{
5586 psa_status_t status;
5587 psa_algorithm_t alg;
5588 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5589 psa_key_derivation_operation_t derivation =
5590 PSA_KEY_DERIVATION_OPERATION_INIT;
5591
5592 if( md_type == MBEDTLS_MD_SHA384 )
5593 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5594 else
5595 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5596
5597 /* Normally a "secret" should be long enough to be impossible to
5598 * find by brute force, and in particular should not be empty. But
5599 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5600 * and for this use case it makes sense to have a 0-length "secret".
5601 * Since the key API doesn't allow importing a key of length 0,
5602 * keep master_key=0, which setup_psa_key_derivation() understands
5603 * to mean a 0-length "secret" input. */
5604 if( slen != 0 )
5605 {
5606 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5607 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5608 psa_set_key_algorithm( &key_attributes, alg );
5609 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5610
5611 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5612 if( status != PSA_SUCCESS )
5613 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5614 }
5615
5616 status = setup_psa_key_derivation( &derivation,
5617 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005618 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005619 random, rlen,
5620 (unsigned char const *) label,
5621 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005622 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005623 dlen );
5624 if( status != PSA_SUCCESS )
5625 {
5626 psa_key_derivation_abort( &derivation );
5627 psa_destroy_key( master_key );
5628 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5629 }
5630
5631 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5632 if( status != PSA_SUCCESS )
5633 {
5634 psa_key_derivation_abort( &derivation );
5635 psa_destroy_key( master_key );
5636 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5637 }
5638
5639 status = psa_key_derivation_abort( &derivation );
5640 if( status != PSA_SUCCESS )
5641 {
5642 psa_destroy_key( master_key );
5643 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5644 }
5645
5646 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5647 status = psa_destroy_key( master_key );
5648 if( status != PSA_SUCCESS )
5649 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5650
5651 return( 0 );
5652}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005653#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005654#else /* MBEDTLS_USE_PSA_CRYPTO */
5655
Andrzej Kurek57d10632022-10-24 10:32:01 -04005656#if defined(MBEDTLS_MD_C) && \
5657 ( defined(MBEDTLS_SHA256_C) || \
5658 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005659MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005660static int tls_prf_generic( mbedtls_md_type_t md_type,
5661 const unsigned char *secret, size_t slen,
5662 const char *label,
5663 const unsigned char *random, size_t rlen,
5664 unsigned char *dstbuf, size_t dlen )
5665{
5666 size_t nb;
5667 size_t i, j, k, md_len;
5668 unsigned char *tmp;
5669 size_t tmp_len = 0;
5670 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5671 const mbedtls_md_info_t *md_info;
5672 mbedtls_md_context_t md_ctx;
5673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5674
5675 mbedtls_md_init( &md_ctx );
5676
5677 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5679
5680 md_len = mbedtls_md_get_size( md_info );
5681
5682 tmp_len = md_len + strlen( label ) + rlen;
5683 tmp = mbedtls_calloc( 1, tmp_len );
5684 if( tmp == NULL )
5685 {
5686 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5687 goto exit;
5688 }
5689
5690 nb = strlen( label );
5691 memcpy( tmp + md_len, label, nb );
5692 memcpy( tmp + md_len + nb, random, rlen );
5693 nb += rlen;
5694
5695 /*
5696 * Compute P_<hash>(secret, label + random)[0..dlen]
5697 */
5698 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5699 goto exit;
5700
5701 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5702 if( ret != 0 )
5703 goto exit;
5704 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5705 if( ret != 0 )
5706 goto exit;
5707 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5708 if( ret != 0 )
5709 goto exit;
5710
5711 for( i = 0; i < dlen; i += md_len )
5712 {
5713 ret = mbedtls_md_hmac_reset ( &md_ctx );
5714 if( ret != 0 )
5715 goto exit;
5716 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5717 if( ret != 0 )
5718 goto exit;
5719 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5720 if( ret != 0 )
5721 goto exit;
5722
5723 ret = mbedtls_md_hmac_reset ( &md_ctx );
5724 if( ret != 0 )
5725 goto exit;
5726 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5727 if( ret != 0 )
5728 goto exit;
5729 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5730 if( ret != 0 )
5731 goto exit;
5732
5733 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5734
5735 for( j = 0; j < k; j++ )
5736 dstbuf[i + j] = h_i[j];
5737 }
5738
5739exit:
5740 mbedtls_md_free( &md_ctx );
5741
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005742 if ( tmp != NULL )
5743 mbedtls_platform_zeroize( tmp, tmp_len );
5744
Jerry Yudc7bd172022-02-17 13:44:15 +08005745 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5746
5747 mbedtls_free( tmp );
5748
5749 return( ret );
5750}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005751#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005752#endif /* MBEDTLS_USE_PSA_CRYPTO */
5753
Andrzej Kurek25f27152022-08-17 16:09:31 -04005754#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005755MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005756static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5757 const char *label,
5758 const unsigned char *random, size_t rlen,
5759 unsigned char *dstbuf, size_t dlen )
5760{
5761 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5762 label, random, rlen, dstbuf, dlen ) );
5763}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005764#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005765
Andrzej Kurek25f27152022-08-17 16:09:31 -04005766#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005767MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005768static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5769 const char *label,
5770 const unsigned char *random, size_t rlen,
5771 unsigned char *dstbuf, size_t dlen )
5772{
5773 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5774 label, random, rlen, dstbuf, dlen ) );
5775}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005776#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005777
Jerry Yuf009d862022-02-17 14:01:37 +08005778/*
5779 * Set appropriate PRF function and other SSL / TLS1.2 functions
5780 *
5781 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005782 * - hash associated with the ciphersuite (only used by TLS 1.2)
5783 *
5784 * Outputs:
5785 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5786 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005787MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005788static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005789 mbedtls_md_type_t hash )
5790{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005791#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005792 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005793 {
5794 handshake->tls_prf = tls_prf_sha384;
5795 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5796 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5797 }
5798 else
5799#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005800#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005801 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005802 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005803 handshake->tls_prf = tls_prf_sha256;
5804 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5805 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5806 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005807#else
Jerry Yuf009d862022-02-17 14:01:37 +08005808 {
Jerry Yuf009d862022-02-17 14:01:37 +08005809 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005810 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5812 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005813#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005814
5815 return( 0 );
5816}
Jerry Yud6ab2352022-02-17 14:03:43 +08005817
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005818/*
5819 * Compute master secret if needed
5820 *
5821 * Parameters:
5822 * [in/out] handshake
5823 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5824 * (PSA-PSK) ciphersuite_info, psk_opaque
5825 * [out] premaster (cleared)
5826 * [out] master
5827 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5828 * debug: conf->f_dbg, conf->p_dbg
5829 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005830 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005831 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005832MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005833static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5834 unsigned char *master,
5835 const mbedtls_ssl_context *ssl )
5836{
5837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5838
5839 /* cf. RFC 5246, Section 8.1:
5840 * "The master secret is always exactly 48 bytes in length." */
5841 size_t const master_secret_len = 48;
5842
5843#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5844 unsigned char session_hash[48];
5845#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
5846
5847 /* The label for the KDF used for key expansion.
5848 * This is either "master secret" or "extended master secret"
5849 * depending on whether the Extended Master Secret extension
5850 * is used. */
5851 char const *lbl = "master secret";
5852
Przemek Stekielae4ed302022-04-05 17:15:55 +02005853 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005854 * - If the Extended Master Secret extension is not used,
5855 * this is ClientHello.Random + ServerHello.Random
5856 * (see Sect. 8.1 in RFC 5246).
5857 * - If the Extended Master Secret extension is used,
5858 * this is the transcript of the handshake so far.
5859 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02005860 unsigned char const *seed = handshake->randbytes;
5861 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005862
5863#if !defined(MBEDTLS_DEBUG_C) && \
5864 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
5865 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
5866 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
5867 ssl = NULL; /* make sure we don't use it except for those cases */
5868 (void) ssl;
5869#endif
5870
5871 if( handshake->resume != 0 )
5872 {
5873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
5874 return( 0 );
5875 }
5876
5877#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5878 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
5879 {
5880 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02005881 seed = session_hash;
5882 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005883
5884 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02005885 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005886 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02005887#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005888
Przemek Stekiel99114f32022-04-22 11:20:09 +02005889#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02005890 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02005891 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005892 {
5893 /* Perform PSK-to-MS expansion in a single step. */
5894 psa_status_t status;
5895 psa_algorithm_t alg;
5896 mbedtls_svc_key_id_t psk;
5897 psa_key_derivation_operation_t derivation =
5898 PSA_KEY_DERIVATION_OPERATION_INIT;
5899 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
5900
5901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
5902
5903 psk = mbedtls_ssl_get_opaque_psk( ssl );
5904
5905 if( hash_alg == MBEDTLS_MD_SHA384 )
5906 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
5907 else
5908 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
5909
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005910 size_t other_secret_len = 0;
5911 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02005912
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005913 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02005914 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005915 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005916 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005917 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02005918 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005919 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02005920 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005921 other_secret_len = 48;
5922 other_secret = handshake->premaster + 2;
5923 break;
5924 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02005925 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02005926 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
5927 other_secret = handshake->premaster + 2;
5928 break;
5929 default:
5930 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02005931 }
5932
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005933 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005934 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02005935 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005936 (unsigned char const *) lbl,
5937 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005938 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005939 master_secret_len );
5940 if( status != PSA_SUCCESS )
5941 {
5942 psa_key_derivation_abort( &derivation );
5943 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5944 }
5945
5946 status = psa_key_derivation_output_bytes( &derivation,
5947 master,
5948 master_secret_len );
5949 if( status != PSA_SUCCESS )
5950 {
5951 psa_key_derivation_abort( &derivation );
5952 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5953 }
5954
5955 status = psa_key_derivation_abort( &derivation );
5956 if( status != PSA_SUCCESS )
5957 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5958 }
5959 else
5960#endif
5961 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02005962#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
5963 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
5964 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5965 {
5966 psa_status_t status;
5967 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
5968 psa_key_derivation_operation_t derivation =
5969 PSA_KEY_DERIVATION_OPERATION_INIT;
5970
5971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
5972
5973 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
5974
5975 status = psa_key_derivation_setup( &derivation, alg );
5976 if( status != PSA_SUCCESS )
5977 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5978
5979 status = psa_key_derivation_set_capacity( &derivation,
5980 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
5981 if( status != PSA_SUCCESS )
5982 {
5983 psa_key_derivation_abort( &derivation );
5984 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5985 }
5986
5987 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
5988 &derivation );
5989 if( status != PSA_SUCCESS )
5990 {
5991 psa_key_derivation_abort( &derivation );
5992 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5993 }
5994
5995 status = psa_key_derivation_output_bytes( &derivation,
5996 handshake->premaster,
5997 handshake->pmslen );
5998 if( status != PSA_SUCCESS )
5999 {
6000 psa_key_derivation_abort( &derivation );
6001 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6002 }
6003
6004 status = psa_key_derivation_abort( &derivation );
6005 if( status != PSA_SUCCESS )
6006 {
6007 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6008 }
6009 }
6010#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006011 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006012 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006013 master,
6014 master_secret_len );
6015 if( ret != 0 )
6016 {
6017 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6018 return( ret );
6019 }
6020
6021 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6022 handshake->premaster,
6023 handshake->pmslen );
6024
6025 mbedtls_platform_zeroize( handshake->premaster,
6026 sizeof(handshake->premaster) );
6027 }
6028
6029 return( 0 );
6030}
6031
Jerry Yud62f87e2022-02-17 14:09:02 +08006032int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6033{
6034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6035 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6036 ssl->handshake->ciphersuite_info;
6037
6038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6039
6040 /* Set PRF, calc_verify and calc_finished function pointers */
6041 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006042 ciphersuite_info->mac );
6043 if( ret != 0 )
6044 {
6045 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6046 return( ret );
6047 }
6048
6049 /* Compute master secret if needed */
6050 ret = ssl_compute_master( ssl->handshake,
6051 ssl->session_negotiate->master,
6052 ssl );
6053 if( ret != 0 )
6054 {
6055 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6056 return( ret );
6057 }
6058
6059 /* Swap the client and server random values:
6060 * - MS derivation wanted client+server (RFC 5246 8.1)
6061 * - key derivation wants server+client (RFC 5246 6.3) */
6062 {
6063 unsigned char tmp[64];
6064 memcpy( tmp, ssl->handshake->randbytes, 64 );
6065 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6066 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6067 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6068 }
6069
6070 /* Populate transform structure */
6071 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6072 ssl->session_negotiate->ciphersuite,
6073 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006074#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006075 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006076#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006077 ssl->handshake->tls_prf,
6078 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006079 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006080 ssl->conf->endpoint,
6081 ssl );
6082 if( ret != 0 )
6083 {
6084 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6085 return( ret );
6086 }
6087
6088 /* We no longer need Server/ClientHello.random values */
6089 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6090 sizeof( ssl->handshake->randbytes ) );
6091
6092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6093
6094 return( 0 );
6095}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006096
Ronald Cron4dcbca92022-03-07 10:21:40 +01006097int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6098{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006099 switch( md )
6100 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006101#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006102 case MBEDTLS_SSL_HASH_SHA384:
6103 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6104 break;
6105#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006106#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006107 case MBEDTLS_SSL_HASH_SHA256:
6108 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6109 break;
6110#endif
6111 default:
6112 return( -1 );
6113 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006114#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6115 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6116 (void) ssl;
6117#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006118 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006119}
6120
Andrzej Kurek25f27152022-08-17 16:09:31 -04006121#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006122void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6123 unsigned char *hash,
6124 size_t *hlen )
6125{
6126#if defined(MBEDTLS_USE_PSA_CRYPTO)
6127 size_t hash_size;
6128 psa_status_t status;
6129 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6130
6131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6132 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6133 if( status != PSA_SUCCESS )
6134 {
6135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6136 return;
6137 }
6138
6139 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6140 if( status != PSA_SUCCESS )
6141 {
6142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6143 return;
6144 }
6145
6146 *hlen = 32;
6147 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6149#else
6150 mbedtls_sha256_context sha256;
6151
6152 mbedtls_sha256_init( &sha256 );
6153
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6155
6156 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6157 mbedtls_sha256_finish( &sha256, hash );
6158
6159 *hlen = 32;
6160
6161 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6163
6164 mbedtls_sha256_free( &sha256 );
6165#endif /* MBEDTLS_USE_PSA_CRYPTO */
6166 return;
6167}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006168#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006169
Andrzej Kurek25f27152022-08-17 16:09:31 -04006170#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006171void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6172 unsigned char *hash,
6173 size_t *hlen )
6174{
6175#if defined(MBEDTLS_USE_PSA_CRYPTO)
6176 size_t hash_size;
6177 psa_status_t status;
6178 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6179
6180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6181 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6182 if( status != PSA_SUCCESS )
6183 {
6184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6185 return;
6186 }
6187
6188 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6189 if( status != PSA_SUCCESS )
6190 {
6191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6192 return;
6193 }
6194
6195 *hlen = 48;
6196 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6198#else
6199 mbedtls_sha512_context sha512;
6200
6201 mbedtls_sha512_init( &sha512 );
6202
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6204
Andrzej Kureka242e832022-08-11 10:03:14 -04006205 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006206 mbedtls_sha512_finish( &sha512, hash );
6207
6208 *hlen = 48;
6209
6210 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6212
6213 mbedtls_sha512_free( &sha512 );
6214#endif /* MBEDTLS_USE_PSA_CRYPTO */
6215 return;
6216}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006217#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006218
Neil Armstrong80f6f322022-05-03 17:56:38 +02006219#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6220 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006221int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6222{
6223 unsigned char *p = ssl->handshake->premaster;
6224 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6225 const unsigned char *psk = NULL;
6226 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006227 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006228
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006229 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006230 {
6231 /*
6232 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006233 * checked before calling this function.
6234 *
6235 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006236 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006237 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006238 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6239 {
6240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6242 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006243 }
6244
6245 /*
6246 * PMS = struct {
6247 * opaque other_secret<0..2^16-1>;
6248 * opaque psk<0..2^16-1>;
6249 * };
6250 * with "other_secret" depending on the particular key exchange
6251 */
6252#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6253 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6254 {
6255 if( end - p < 2 )
6256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6257
6258 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6259 p += 2;
6260
6261 if( end < p || (size_t)( end - p ) < psk_len )
6262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6263
6264 memset( p, 0, psk_len );
6265 p += psk_len;
6266 }
6267 else
6268#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6269#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6270 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6271 {
6272 /*
6273 * other_secret already set by the ClientKeyExchange message,
6274 * and is 48 bytes long
6275 */
6276 if( end - p < 2 )
6277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6278
6279 *p++ = 0;
6280 *p++ = 48;
6281 p += 48;
6282 }
6283 else
6284#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6285#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6286 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6287 {
6288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6289 size_t len;
6290
6291 /* Write length only when we know the actual value */
6292 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6293 p + 2, end - ( p + 2 ), &len,
6294 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6295 {
6296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6297 return( ret );
6298 }
6299 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6300 p += 2 + len;
6301
6302 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6303 }
6304 else
6305#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006306#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006307 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6308 {
6309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6310 size_t zlen;
6311
6312 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6313 p + 2, end - ( p + 2 ),
6314 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6315 {
6316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6317 return( ret );
6318 }
6319
6320 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6321 p += 2 + zlen;
6322
6323 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6324 MBEDTLS_DEBUG_ECDH_Z );
6325 }
6326 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006327#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006328 {
6329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6331 }
6332
6333 /* opaque psk<0..2^16-1>; */
6334 if( end - p < 2 )
6335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6336
6337 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6338 p += 2;
6339
6340 if( end < p || (size_t)( end - p ) < psk_len )
6341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6342
6343 memcpy( p, psk, psk_len );
6344 p += psk_len;
6345
6346 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6347
6348 return( 0 );
6349}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006350#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006351
6352#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006353MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006354static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6355
6356#if defined(MBEDTLS_SSL_PROTO_DTLS)
6357int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6358{
6359 /* If renegotiation is not enforced, retransmit until we would reach max
6360 * timeout if we were using the usual handshake doubling scheme */
6361 if( ssl->conf->renego_max_records < 0 )
6362 {
6363 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6364 unsigned char doublings = 1;
6365
6366 while( ratio != 0 )
6367 {
6368 ++doublings;
6369 ratio >>= 1;
6370 }
6371
6372 if( ++ssl->renego_records_seen > doublings )
6373 {
6374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6375 return( 0 );
6376 }
6377 }
6378
6379 return( ssl_write_hello_request( ssl ) );
6380}
6381#endif
6382#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006383
Jerry Yud9526692022-02-17 14:23:47 +08006384/*
6385 * Handshake functions
6386 */
6387#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6388/* No certificate support -> dummy functions */
6389int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6390{
6391 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6392 ssl->handshake->ciphersuite_info;
6393
6394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6395
6396 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6397 {
6398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6399 ssl->state++;
6400 return( 0 );
6401 }
6402
6403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6405}
6406
6407int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6408{
6409 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6410 ssl->handshake->ciphersuite_info;
6411
6412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6413
6414 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6415 {
6416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6417 ssl->state++;
6418 return( 0 );
6419 }
6420
6421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6423}
6424
6425#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6426/* Some certificate support -> implement write and parse */
6427
6428int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6429{
6430 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6431 size_t i, n;
6432 const mbedtls_x509_crt *crt;
6433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6434 ssl->handshake->ciphersuite_info;
6435
6436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6437
6438 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6439 {
6440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6441 ssl->state++;
6442 return( 0 );
6443 }
6444
6445#if defined(MBEDTLS_SSL_CLI_C)
6446 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6447 {
6448 if( ssl->handshake->client_auth == 0 )
6449 {
6450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6451 ssl->state++;
6452 return( 0 );
6453 }
6454 }
6455#endif /* MBEDTLS_SSL_CLI_C */
6456#if defined(MBEDTLS_SSL_SRV_C)
6457 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6458 {
6459 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6460 {
6461 /* Should never happen because we shouldn't have picked the
6462 * ciphersuite if we don't have a certificate. */
6463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6464 }
6465 }
6466#endif
6467
6468 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6469
6470 /*
6471 * 0 . 0 handshake type
6472 * 1 . 3 handshake length
6473 * 4 . 6 length of all certs
6474 * 7 . 9 length of cert. 1
6475 * 10 . n-1 peer certificate
6476 * n . n+2 length of cert. 2
6477 * n+3 . ... upper level cert, etc.
6478 */
6479 i = 7;
6480 crt = mbedtls_ssl_own_cert( ssl );
6481
6482 while( crt != NULL )
6483 {
6484 n = crt->raw.len;
6485 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6486 {
6487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6488 " > %" MBEDTLS_PRINTF_SIZET,
6489 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6490 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6491 }
6492
6493 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6494 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6495 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6496
6497 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6498 i += n; crt = crt->next;
6499 }
6500
6501 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6502 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6503 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6504
6505 ssl->out_msglen = i;
6506 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6507 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6508
6509 ssl->state++;
6510
6511 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6512 {
6513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6514 return( ret );
6515 }
6516
6517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6518
6519 return( ret );
6520}
6521
6522#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6523
6524#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006525MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006526static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6527 unsigned char *crt_buf,
6528 size_t crt_buf_len )
6529{
6530 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6531
6532 if( peer_crt == NULL )
6533 return( -1 );
6534
6535 if( peer_crt->raw.len != crt_buf_len )
6536 return( -1 );
6537
6538 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6539}
6540#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006541MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006542static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6543 unsigned char *crt_buf,
6544 size_t crt_buf_len )
6545{
6546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6547 unsigned char const * const peer_cert_digest =
6548 ssl->session->peer_cert_digest;
6549 mbedtls_md_type_t const peer_cert_digest_type =
6550 ssl->session->peer_cert_digest_type;
6551 mbedtls_md_info_t const * const digest_info =
6552 mbedtls_md_info_from_type( peer_cert_digest_type );
6553 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6554 size_t digest_len;
6555
6556 if( peer_cert_digest == NULL || digest_info == NULL )
6557 return( -1 );
6558
6559 digest_len = mbedtls_md_get_size( digest_info );
6560 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6561 return( -1 );
6562
6563 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6564 if( ret != 0 )
6565 return( -1 );
6566
6567 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6568}
6569#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6570#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6571
6572/*
6573 * Once the certificate message is read, parse it into a cert chain and
6574 * perform basic checks, but leave actual verification to the caller
6575 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006576MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006577static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6578 mbedtls_x509_crt *chain )
6579{
6580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6581#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6582 int crt_cnt=0;
6583#endif
6584 size_t i, n;
6585 uint8_t alert;
6586
6587 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6588 {
6589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6590 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6591 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6592 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6593 }
6594
6595 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6596 {
6597 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6598 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6599 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6600 }
6601
6602 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6603 {
6604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6606 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6607 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6608 }
6609
6610 i = mbedtls_ssl_hs_hdr_len( ssl );
6611
6612 /*
6613 * Same message structure as in mbedtls_ssl_write_certificate()
6614 */
6615 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6616
6617 if( ssl->in_msg[i] != 0 ||
6618 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6619 {
6620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6621 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6622 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6623 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6624 }
6625
6626 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6627 i += 3;
6628
6629 /* Iterate through and parse the CRTs in the provided chain. */
6630 while( i < ssl->in_hslen )
6631 {
6632 /* Check that there's room for the next CRT's length fields. */
6633 if ( i + 3 > ssl->in_hslen ) {
6634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6635 mbedtls_ssl_send_alert_message( ssl,
6636 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6637 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6638 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6639 }
6640 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6641 * anything beyond 2**16 ~ 64K. */
6642 if( ssl->in_msg[i] != 0 )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6645 mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6648 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6649 }
6650
6651 /* Read length of the next CRT in the chain. */
6652 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6653 | (unsigned int) ssl->in_msg[i + 2];
6654 i += 3;
6655
6656 if( n < 128 || i + n > ssl->in_hslen )
6657 {
6658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6659 mbedtls_ssl_send_alert_message( ssl,
6660 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6661 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6662 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6663 }
6664
6665 /* Check if we're handling the first CRT in the chain. */
6666#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6667 if( crt_cnt++ == 0 &&
6668 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6669 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6670 {
6671 /* During client-side renegotiation, check that the server's
6672 * end-CRTs hasn't changed compared to the initial handshake,
6673 * mitigating the triple handshake attack. On success, reuse
6674 * the original end-CRT instead of parsing it again. */
6675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6676 if( ssl_check_peer_crt_unchanged( ssl,
6677 &ssl->in_msg[i],
6678 n ) != 0 )
6679 {
6680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6681 mbedtls_ssl_send_alert_message( ssl,
6682 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6683 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6684 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6685 }
6686
6687 /* Now we can safely free the original chain. */
6688 ssl_clear_peer_cert( ssl->session );
6689 }
6690#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6691
6692 /* Parse the next certificate in the chain. */
6693#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6694 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6695#else
6696 /* If we don't need to store the CRT chain permanently, parse
6697 * it in-place from the input buffer instead of making a copy. */
6698 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6699#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6700 switch( ret )
6701 {
6702 case 0: /*ok*/
6703 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6704 /* Ignore certificate with an unknown algorithm: maybe a
6705 prior certificate was already trusted. */
6706 break;
6707
6708 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6709 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6710 goto crt_parse_der_failed;
6711
6712 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6713 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6714 goto crt_parse_der_failed;
6715
6716 default:
6717 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6718 crt_parse_der_failed:
6719 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6720 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6721 return( ret );
6722 }
6723
6724 i += n;
6725 }
6726
6727 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6728 return( 0 );
6729}
6730
6731#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006732MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006733static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6734{
6735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6736 return( -1 );
6737
6738 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6739 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6740 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6741 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6742 {
Ronald Cron19385882022-06-15 16:26:13 +02006743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006744 return( 0 );
6745 }
6746 return( -1 );
6747}
6748#endif /* MBEDTLS_SSL_SRV_C */
6749
6750/* Check if a certificate message is expected.
6751 * Return either
6752 * - SSL_CERTIFICATE_EXPECTED, or
6753 * - SSL_CERTIFICATE_SKIP
6754 * indicating whether a Certificate message is expected or not.
6755 */
6756#define SSL_CERTIFICATE_EXPECTED 0
6757#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006758MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006759static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6760 int authmode )
6761{
6762 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6763 ssl->handshake->ciphersuite_info;
6764
6765 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6766 return( SSL_CERTIFICATE_SKIP );
6767
6768#if defined(MBEDTLS_SSL_SRV_C)
6769 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6770 {
6771 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6772 return( SSL_CERTIFICATE_SKIP );
6773
6774 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6775 {
6776 ssl->session_negotiate->verify_result =
6777 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6778 return( SSL_CERTIFICATE_SKIP );
6779 }
6780 }
6781#else
6782 ((void) authmode);
6783#endif /* MBEDTLS_SSL_SRV_C */
6784
6785 return( SSL_CERTIFICATE_EXPECTED );
6786}
6787
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006788MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006789static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6790 int authmode,
6791 mbedtls_x509_crt *chain,
6792 void *rs_ctx )
6793{
6794 int ret = 0;
6795 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6796 ssl->handshake->ciphersuite_info;
6797 int have_ca_chain = 0;
6798
6799 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6800 void *p_vrfy;
6801
6802 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6803 return( 0 );
6804
6805 if( ssl->f_vrfy != NULL )
6806 {
6807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6808 f_vrfy = ssl->f_vrfy;
6809 p_vrfy = ssl->p_vrfy;
6810 }
6811 else
6812 {
6813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6814 f_vrfy = ssl->conf->f_vrfy;
6815 p_vrfy = ssl->conf->p_vrfy;
6816 }
6817
6818 /*
6819 * Main check: verify certificate
6820 */
6821#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6822 if( ssl->conf->f_ca_cb != NULL )
6823 {
6824 ((void) rs_ctx);
6825 have_ca_chain = 1;
6826
6827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6828 ret = mbedtls_x509_crt_verify_with_ca_cb(
6829 chain,
6830 ssl->conf->f_ca_cb,
6831 ssl->conf->p_ca_cb,
6832 ssl->conf->cert_profile,
6833 ssl->hostname,
6834 &ssl->session_negotiate->verify_result,
6835 f_vrfy, p_vrfy );
6836 }
6837 else
6838#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6839 {
6840 mbedtls_x509_crt *ca_chain;
6841 mbedtls_x509_crl *ca_crl;
6842
6843#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6844 if( ssl->handshake->sni_ca_chain != NULL )
6845 {
6846 ca_chain = ssl->handshake->sni_ca_chain;
6847 ca_crl = ssl->handshake->sni_ca_crl;
6848 }
6849 else
6850#endif
6851 {
6852 ca_chain = ssl->conf->ca_chain;
6853 ca_crl = ssl->conf->ca_crl;
6854 }
6855
6856 if( ca_chain != NULL )
6857 have_ca_chain = 1;
6858
6859 ret = mbedtls_x509_crt_verify_restartable(
6860 chain,
6861 ca_chain, ca_crl,
6862 ssl->conf->cert_profile,
6863 ssl->hostname,
6864 &ssl->session_negotiate->verify_result,
6865 f_vrfy, p_vrfy, rs_ctx );
6866 }
6867
6868 if( ret != 0 )
6869 {
6870 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6871 }
6872
6873#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6874 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6875 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6876#endif
6877
6878 /*
6879 * Secondary checks: always done, but change 'ret' only if it was 0
6880 */
6881
6882#if defined(MBEDTLS_ECP_C)
6883 {
6884 const mbedtls_pk_context *pk = &chain->pk;
6885
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02006886 /* If certificate uses an EC key, make sure the curve is OK.
6887 * This is a public key, so it can't be opaque, so can_do() is a good
6888 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006889 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08006890 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006891 /* and in the unlikely case the above assumption no longer holds
6892 * we are making sure that pk_ec() here does not return a NULL
6893 */
6894 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
6895 if( ec == NULL )
6896 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01006897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6899 }
Jerry Yud9526692022-02-17 14:23:47 +08006900
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006901 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
6902 {
6903 ssl->session_negotiate->verify_result |=
6904 MBEDTLS_X509_BADCERT_BAD_KEY;
6905
6906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6907 if( ret == 0 )
6908 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6909 }
Jerry Yud9526692022-02-17 14:23:47 +08006910 }
6911 }
6912#endif /* MBEDTLS_ECP_C */
6913
6914 if( mbedtls_ssl_check_cert_usage( chain,
6915 ciphersuite_info,
6916 ! ssl->conf->endpoint,
6917 &ssl->session_negotiate->verify_result ) != 0 )
6918 {
6919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6920 if( ret == 0 )
6921 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
6922 }
6923
6924 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6925 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6926 * with details encoded in the verification flags. All other kinds
6927 * of error codes, including those from the user provided f_vrfy
6928 * functions, are treated as fatal and lead to a failure of
6929 * ssl_parse_certificate even if verification was optional. */
6930 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6931 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6932 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
6933 {
6934 ret = 0;
6935 }
6936
6937 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6938 {
6939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6940 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6941 }
6942
6943 if( ret != 0 )
6944 {
6945 uint8_t alert;
6946
6947 /* The certificate may have been rejected for several reasons.
6948 Pick one and send the corresponding alert. Which alert to send
6949 may be a subject of debate in some cases. */
6950 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6951 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6952 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6953 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6954 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6955 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6956 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6957 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6958 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6959 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6960 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6961 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6962 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6963 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6964 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6965 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6966 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6967 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6968 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6969 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6970 else
6971 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6972 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6973 alert );
6974 }
6975
6976#if defined(MBEDTLS_DEBUG_C)
6977 if( ssl->session_negotiate->verify_result != 0 )
6978 {
6979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
6980 (unsigned int) ssl->session_negotiate->verify_result ) );
6981 }
6982 else
6983 {
6984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6985 }
6986#endif /* MBEDTLS_DEBUG_C */
6987
6988 return( ret );
6989}
6990
6991#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006992MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006993static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6994 unsigned char *start, size_t len )
6995{
6996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6997 /* Remember digest of the peer's end-CRT. */
6998 ssl->session_negotiate->peer_cert_digest =
6999 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7000 if( ssl->session_negotiate->peer_cert_digest == NULL )
7001 {
7002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7003 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7004 mbedtls_ssl_send_alert_message( ssl,
7005 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7006 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7007
7008 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7009 }
7010
7011 ret = mbedtls_md( mbedtls_md_info_from_type(
7012 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7013 start, len,
7014 ssl->session_negotiate->peer_cert_digest );
7015
7016 ssl->session_negotiate->peer_cert_digest_type =
7017 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7018 ssl->session_negotiate->peer_cert_digest_len =
7019 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7020
7021 return( ret );
7022}
7023
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007024MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007025static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7026 unsigned char *start, size_t len )
7027{
7028 unsigned char *end = start + len;
7029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7030
7031 /* Make a copy of the peer's raw public key. */
7032 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7033 ret = mbedtls_pk_parse_subpubkey( &start, end,
7034 &ssl->handshake->peer_pubkey );
7035 if( ret != 0 )
7036 {
7037 /* We should have parsed the public key before. */
7038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7039 }
7040
7041 return( 0 );
7042}
7043#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7044
7045int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7046{
7047 int ret = 0;
7048 int crt_expected;
7049#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7050 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7051 ? ssl->handshake->sni_authmode
7052 : ssl->conf->authmode;
7053#else
7054 const int authmode = ssl->conf->authmode;
7055#endif
7056 void *rs_ctx = NULL;
7057 mbedtls_x509_crt *chain = NULL;
7058
7059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7060
7061 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7062 if( crt_expected == SSL_CERTIFICATE_SKIP )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7065 goto exit;
7066 }
7067
7068#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7069 if( ssl->handshake->ecrs_enabled &&
7070 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7071 {
7072 chain = ssl->handshake->ecrs_peer_cert;
7073 ssl->handshake->ecrs_peer_cert = NULL;
7074 goto crt_verify;
7075 }
7076#endif
7077
7078 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7079 {
7080 /* mbedtls_ssl_read_record may have sent an alert already. We
7081 let it decide whether to alert. */
7082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7083 goto exit;
7084 }
7085
7086#if defined(MBEDTLS_SSL_SRV_C)
7087 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7088 {
7089 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7090
7091 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7092 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7093
7094 goto exit;
7095 }
7096#endif /* MBEDTLS_SSL_SRV_C */
7097
7098 /* Clear existing peer CRT structure in case we tried to
7099 * reuse a session but it failed, and allocate a new one. */
7100 ssl_clear_peer_cert( ssl->session_negotiate );
7101
7102 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7103 if( chain == NULL )
7104 {
7105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7106 sizeof( mbedtls_x509_crt ) ) );
7107 mbedtls_ssl_send_alert_message( ssl,
7108 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7109 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7110
7111 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7112 goto exit;
7113 }
7114 mbedtls_x509_crt_init( chain );
7115
7116 ret = ssl_parse_certificate_chain( ssl, chain );
7117 if( ret != 0 )
7118 goto exit;
7119
7120#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7121 if( ssl->handshake->ecrs_enabled)
7122 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7123
7124crt_verify:
7125 if( ssl->handshake->ecrs_enabled)
7126 rs_ctx = &ssl->handshake->ecrs_ctx;
7127#endif
7128
7129 ret = ssl_parse_certificate_verify( ssl, authmode,
7130 chain, rs_ctx );
7131 if( ret != 0 )
7132 goto exit;
7133
7134#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7135 {
7136 unsigned char *crt_start, *pk_start;
7137 size_t crt_len, pk_len;
7138
7139 /* We parse the CRT chain without copying, so
7140 * these pointers point into the input buffer,
7141 * and are hence still valid after freeing the
7142 * CRT chain. */
7143
7144 crt_start = chain->raw.p;
7145 crt_len = chain->raw.len;
7146
7147 pk_start = chain->pk_raw.p;
7148 pk_len = chain->pk_raw.len;
7149
7150 /* Free the CRT structures before computing
7151 * digest and copying the peer's public key. */
7152 mbedtls_x509_crt_free( chain );
7153 mbedtls_free( chain );
7154 chain = NULL;
7155
7156 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7157 if( ret != 0 )
7158 goto exit;
7159
7160 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7161 if( ret != 0 )
7162 goto exit;
7163 }
7164#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7165 /* Pass ownership to session structure. */
7166 ssl->session_negotiate->peer_cert = chain;
7167 chain = NULL;
7168#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7169
7170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7171
7172exit:
7173
7174 if( ret == 0 )
7175 ssl->state++;
7176
7177#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7178 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7179 {
7180 ssl->handshake->ecrs_peer_cert = chain;
7181 chain = NULL;
7182 }
7183#endif
7184
7185 if( chain != NULL )
7186 {
7187 mbedtls_x509_crt_free( chain );
7188 mbedtls_free( chain );
7189 }
7190
7191 return( ret );
7192}
7193#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7194
Andrzej Kurek25f27152022-08-17 16:09:31 -04007195#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007196static void ssl_calc_finished_tls_sha256(
7197 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7198{
7199 int len = 12;
7200 const char *sender;
7201 unsigned char padbuf[32];
7202#if defined(MBEDTLS_USE_PSA_CRYPTO)
7203 size_t hash_size;
7204 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7205 psa_status_t status;
7206#else
7207 mbedtls_sha256_context sha256;
7208#endif
7209
7210 mbedtls_ssl_session *session = ssl->session_negotiate;
7211 if( !session )
7212 session = ssl->session;
7213
7214 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7215 ? "client finished"
7216 : "server finished";
7217
7218#if defined(MBEDTLS_USE_PSA_CRYPTO)
7219 sha256_psa = psa_hash_operation_init();
7220
7221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7222
7223 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7224 if( status != PSA_SUCCESS )
7225 {
7226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7227 return;
7228 }
7229
7230 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7231 if( status != PSA_SUCCESS )
7232 {
7233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7234 return;
7235 }
7236 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7237#else
7238
7239 mbedtls_sha256_init( &sha256 );
7240
7241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7242
7243 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7244
7245 /*
7246 * TLSv1.2:
7247 * hash = PRF( master, finished_label,
7248 * Hash( handshake ) )[0.11]
7249 */
7250
7251#if !defined(MBEDTLS_SHA256_ALT)
7252 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7253 sha256.state, sizeof( sha256.state ) );
7254#endif
7255
7256 mbedtls_sha256_finish( &sha256, padbuf );
7257 mbedtls_sha256_free( &sha256 );
7258#endif /* MBEDTLS_USE_PSA_CRYPTO */
7259
7260 ssl->handshake->tls_prf( session->master, 48, sender,
7261 padbuf, 32, buf, len );
7262
7263 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7264
7265 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7266
7267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7268}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007269#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007270
Jerry Yub7ba49e2022-02-17 14:25:53 +08007271
Andrzej Kurek25f27152022-08-17 16:09:31 -04007272#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007273static void ssl_calc_finished_tls_sha384(
7274 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7275{
7276 int len = 12;
7277 const char *sender;
7278 unsigned char padbuf[48];
7279#if defined(MBEDTLS_USE_PSA_CRYPTO)
7280 size_t hash_size;
7281 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7282 psa_status_t status;
7283#else
7284 mbedtls_sha512_context sha512;
7285#endif
7286
7287 mbedtls_ssl_session *session = ssl->session_negotiate;
7288 if( !session )
7289 session = ssl->session;
7290
7291 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7292 ? "client finished"
7293 : "server finished";
7294
7295#if defined(MBEDTLS_USE_PSA_CRYPTO)
7296 sha384_psa = psa_hash_operation_init();
7297
7298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7299
7300 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7301 if( status != PSA_SUCCESS )
7302 {
7303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7304 return;
7305 }
7306
7307 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7308 if( status != PSA_SUCCESS )
7309 {
7310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7311 return;
7312 }
7313 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7314#else
7315 mbedtls_sha512_init( &sha512 );
7316
7317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7318
Andrzej Kureka242e832022-08-11 10:03:14 -04007319 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007320
7321 /*
7322 * TLSv1.2:
7323 * hash = PRF( master, finished_label,
7324 * Hash( handshake ) )[0.11]
7325 */
7326
7327#if !defined(MBEDTLS_SHA512_ALT)
7328 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7329 sha512.state, sizeof( sha512.state ) );
7330#endif
7331 mbedtls_sha512_finish( &sha512, padbuf );
7332
7333 mbedtls_sha512_free( &sha512 );
7334#endif
7335
7336 ssl->handshake->tls_prf( session->master, 48, sender,
7337 padbuf, 48, buf, len );
7338
7339 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7340
7341 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7342
7343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7344}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007345#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007346
Jerry Yuaef00152022-02-17 14:27:31 +08007347void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7348{
7349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7350
7351 /*
7352 * Free our handshake params
7353 */
7354 mbedtls_ssl_handshake_free( ssl );
7355 mbedtls_free( ssl->handshake );
7356 ssl->handshake = NULL;
7357
7358 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007359 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007360 */
7361 if( ssl->transform )
7362 {
7363 mbedtls_ssl_transform_free( ssl->transform );
7364 mbedtls_free( ssl->transform );
7365 }
7366 ssl->transform = ssl->transform_negotiate;
7367 ssl->transform_negotiate = NULL;
7368
7369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7370}
7371
Jerry Yu2a9fff52022-02-17 14:28:51 +08007372void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7373{
7374 int resume = ssl->handshake->resume;
7375
7376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7377
7378#if defined(MBEDTLS_SSL_RENEGOTIATION)
7379 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7380 {
7381 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7382 ssl->renego_records_seen = 0;
7383 }
7384#endif
7385
7386 /*
7387 * Free the previous session and switch in the current one
7388 */
7389 if( ssl->session )
7390 {
7391#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7392 /* RFC 7366 3.1: keep the EtM state */
7393 ssl->session_negotiate->encrypt_then_mac =
7394 ssl->session->encrypt_then_mac;
7395#endif
7396
7397 mbedtls_ssl_session_free( ssl->session );
7398 mbedtls_free( ssl->session );
7399 }
7400 ssl->session = ssl->session_negotiate;
7401 ssl->session_negotiate = NULL;
7402
7403 /*
7404 * Add cache entry
7405 */
7406 if( ssl->conf->f_set_cache != NULL &&
7407 ssl->session->id_len != 0 &&
7408 resume == 0 )
7409 {
7410 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7411 ssl->session->id,
7412 ssl->session->id_len,
7413 ssl->session ) != 0 )
7414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7415 }
7416
7417#if defined(MBEDTLS_SSL_PROTO_DTLS)
7418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7419 ssl->handshake->flight != NULL )
7420 {
7421 /* Cancel handshake timer */
7422 mbedtls_ssl_set_timer( ssl, 0 );
7423
7424 /* Keep last flight around in case we need to resend it:
7425 * we need the handshake and transform structures for that */
7426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7427 }
7428 else
7429#endif
7430 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7431
7432 ssl->state++;
7433
7434 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7435}
7436
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007437int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7438{
7439 int ret, hash_len;
7440
7441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7442
7443 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7444
7445 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7446
7447 /*
7448 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7449 * may define some other value. Currently (early 2016), no defined
7450 * ciphersuite does this (and this is unlikely to change as activity has
7451 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7452 */
7453 hash_len = 12;
7454
7455#if defined(MBEDTLS_SSL_RENEGOTIATION)
7456 ssl->verify_data_len = hash_len;
7457 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7458#endif
7459
7460 ssl->out_msglen = 4 + hash_len;
7461 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7462 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7463
7464 /*
7465 * In case of session resuming, invert the client and server
7466 * ChangeCipherSpec messages order.
7467 */
7468 if( ssl->handshake->resume != 0 )
7469 {
7470#if defined(MBEDTLS_SSL_CLI_C)
7471 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7472 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7473#endif
7474#if defined(MBEDTLS_SSL_SRV_C)
7475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7476 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7477#endif
7478 }
7479 else
7480 ssl->state++;
7481
7482 /*
7483 * Switch to our negotiated transform and session parameters for outbound
7484 * data.
7485 */
7486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7487
7488#if defined(MBEDTLS_SSL_PROTO_DTLS)
7489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7490 {
7491 unsigned char i;
7492
7493 /* Remember current epoch settings for resending */
7494 ssl->handshake->alt_transform_out = ssl->transform_out;
7495 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7496 sizeof( ssl->handshake->alt_out_ctr ) );
7497
7498 /* Set sequence_number to zero */
7499 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7500
7501
7502 /* Increment epoch */
7503 for( i = 2; i > 0; i-- )
7504 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7505 break;
7506
7507 /* The loop goes to its end iff the counter is wrapping */
7508 if( i == 0 )
7509 {
7510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7511 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7512 }
7513 }
7514 else
7515#endif /* MBEDTLS_SSL_PROTO_DTLS */
7516 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7517
7518 ssl->transform_out = ssl->transform_negotiate;
7519 ssl->session_out = ssl->session_negotiate;
7520
7521#if defined(MBEDTLS_SSL_PROTO_DTLS)
7522 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7523 mbedtls_ssl_send_flight_completed( ssl );
7524#endif
7525
7526 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7527 {
7528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7529 return( ret );
7530 }
7531
7532#if defined(MBEDTLS_SSL_PROTO_DTLS)
7533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7534 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7535 {
7536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7537 return( ret );
7538 }
7539#endif
7540
7541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7542
7543 return( 0 );
7544}
7545
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007546#define SSL_MAX_HASH_LEN 12
7547
7548int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7549{
7550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7551 unsigned int hash_len = 12;
7552 unsigned char buf[SSL_MAX_HASH_LEN];
7553
7554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7555
7556 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7557
7558 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7559 {
7560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7561 goto exit;
7562 }
7563
7564 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7565 {
7566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7567 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7568 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7569 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7570 goto exit;
7571 }
7572
7573 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7574 {
7575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7576 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7577 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7578 goto exit;
7579 }
7580
7581 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7582 {
7583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7584 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7585 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7586 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7587 goto exit;
7588 }
7589
7590 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7591 buf, hash_len ) != 0 )
7592 {
7593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7594 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7595 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7596 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7597 goto exit;
7598 }
7599
7600#if defined(MBEDTLS_SSL_RENEGOTIATION)
7601 ssl->verify_data_len = hash_len;
7602 memcpy( ssl->peer_verify_data, buf, hash_len );
7603#endif
7604
7605 if( ssl->handshake->resume != 0 )
7606 {
7607#if defined(MBEDTLS_SSL_CLI_C)
7608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7609 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7610#endif
7611#if defined(MBEDTLS_SSL_SRV_C)
7612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7613 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7614#endif
7615 }
7616 else
7617 ssl->state++;
7618
7619#if defined(MBEDTLS_SSL_PROTO_DTLS)
7620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7621 mbedtls_ssl_recv_flight_completed( ssl );
7622#endif
7623
7624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7625
7626exit:
7627 mbedtls_platform_zeroize( buf, hash_len );
7628 return( ret );
7629}
7630
Jerry Yu392112c2022-02-17 14:34:10 +08007631#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7632/*
7633 * Helper to get TLS 1.2 PRF from ciphersuite
7634 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7635 */
7636static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7637{
Jerry Yu392112c2022-02-17 14:34:10 +08007638 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007639 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7640#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007641 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007642 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007643 else
Jerry Yu392112c2022-02-17 14:34:10 +08007644#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007645#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7646 {
7647 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7648 return( tls_prf_sha256 );
7649 }
7650#endif
7651#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7652 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7653 (void) ciphersuite_info;
7654#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007655
Andrzej Kurek894edde2022-09-29 06:31:14 -04007656 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007657}
7658#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007659
7660static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7661{
7662 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007663#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007664 if( tls_prf == tls_prf_sha384 )
7665 {
7666 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7667 }
7668 else
7669#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007670#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007671 if( tls_prf == tls_prf_sha256 )
7672 {
7673 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7674 }
7675 else
7676#endif
7677 return( MBEDTLS_SSL_TLS_PRF_NONE );
7678}
7679
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007680/*
7681 * Populate a transform structure with session keys and all the other
7682 * necessary information.
7683 *
7684 * Parameters:
7685 * - [in/out]: transform: structure to populate
7686 * [in] must be just initialised with mbedtls_ssl_transform_init()
7687 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7688 * - [in] ciphersuite
7689 * - [in] master
7690 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007691 * - [in] tls_prf: pointer to PRF to use for key derivation
7692 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007693 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007694 * - [in] endpoint: client or server
7695 * - [in] ssl: used for:
7696 * - ssl->conf->{f,p}_export_keys
7697 * [in] optionally used for:
7698 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7699 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007700MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007701static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7702 int ciphersuite,
7703 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007704#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007705 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007706#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007707 ssl_tls_prf_t tls_prf,
7708 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007709 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007710 unsigned endpoint,
7711 const mbedtls_ssl_context *ssl )
7712{
7713 int ret = 0;
7714 unsigned char keyblk[256];
7715 unsigned char *key1;
7716 unsigned char *key2;
7717 unsigned char *mac_enc;
7718 unsigned char *mac_dec;
7719 size_t mac_key_len = 0;
7720 size_t iv_copy_len;
7721 size_t keylen;
7722 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007723 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007724#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007725 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007726 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007727#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007728
7729#if defined(MBEDTLS_USE_PSA_CRYPTO)
7730 psa_key_type_t key_type;
7731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7732 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007733 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007734 size_t key_bits;
7735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7736#endif
7737
7738#if !defined(MBEDTLS_DEBUG_C) && \
7739 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7740 if( ssl->f_export_keys == NULL )
7741 {
7742 ssl = NULL; /* make sure we don't use it except for these cases */
7743 (void) ssl;
7744 }
7745#endif
7746
7747 /*
7748 * Some data just needs copying into the structure
7749 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007750#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007751 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007752#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007753 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007754
7755#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7756 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7757#endif
7758
7759#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007760 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007761 {
7762 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7763 * generation separate. This should never happen. */
7764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7765 }
7766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7767
7768 /*
7769 * Get various info structures
7770 */
7771 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7772 if( ciphersuite_info == NULL )
7773 {
7774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7775 ciphersuite ) );
7776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7777 }
7778
Neil Armstrongab555e02022-04-04 11:07:59 +02007779 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007780#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007781 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007782#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007783 ciphersuite_info );
7784
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007785 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7786 transform->taglen =
7787 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7788
7789#if defined(MBEDTLS_USE_PSA_CRYPTO)
7790 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7791 transform->taglen,
7792 &alg,
7793 &key_type,
7794 &key_bits ) ) != PSA_SUCCESS )
7795 {
7796 ret = psa_ssl_status_to_mbedtls( status );
7797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7798 goto end;
7799 }
7800#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007801 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7802 if( cipher_info == NULL )
7803 {
7804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7805 ciphersuite_info->cipher ) );
7806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7807 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007808#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007809
Neil Armstronge4512952022-03-08 09:08:22 +01007810#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007811 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007812 if( mac_alg == 0 )
7813 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007815 (unsigned) ciphersuite_info->mac ) );
7816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7817 }
7818#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007819 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7820 if( md_info == NULL )
7821 {
7822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7823 (unsigned) ciphersuite_info->mac ) );
7824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7825 }
Neil Armstronge4512952022-03-08 09:08:22 +01007826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007827
7828#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7829 /* Copy own and peer's CID if the use of the CID
7830 * extension has been negotiated. */
7831 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7832 {
7833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7834
7835 transform->in_cid_len = ssl->own_cid_len;
7836 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7837 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7838 transform->in_cid_len );
7839
7840 transform->out_cid_len = ssl->handshake->peer_cid_len;
7841 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7842 ssl->handshake->peer_cid_len );
7843 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7844 transform->out_cid_len );
7845 }
7846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7847
7848 /*
7849 * Compute key block using the PRF
7850 */
7851 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7852 if( ret != 0 )
7853 {
7854 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7855 return( ret );
7856 }
7857
7858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7859 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7860 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7861 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7862 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7863
7864 /*
7865 * Determine the appropriate key, IV and MAC length.
7866 */
7867
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007868#if defined(MBEDTLS_USE_PSA_CRYPTO)
7869 keylen = PSA_BITS_TO_BYTES(key_bits);
7870#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007871 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007872#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007873
7874#if defined(MBEDTLS_GCM_C) || \
7875 defined(MBEDTLS_CCM_C) || \
7876 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007877 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007878 {
7879 size_t explicit_ivlen;
7880
7881 transform->maclen = 0;
7882 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007883
7884 /* All modes haves 96-bit IVs, but the length of the static parts vary
7885 * with mode and version:
7886 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
7887 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
7888 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
7889 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
7890 * sequence number).
7891 */
7892 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01007893
7894 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007895#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01007896 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007897#else
David Horstmann3b2276a2022-10-06 14:49:08 +01007898 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
7899 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007900#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01007901
7902 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007903 transform->fixed_ivlen = 12;
7904 else
7905 transform->fixed_ivlen = 4;
7906
7907 /* Minimum length of encrypted record */
7908 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
7909 transform->minlen = explicit_ivlen + transform->taglen;
7910 }
7911 else
7912#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
7913#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007914 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
7915 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
7916 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007917 {
Neil Armstronge4512952022-03-08 09:08:22 +01007918#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02007919 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007920#else
7921 size_t block_size = cipher_info->block_size;
7922#endif /* MBEDTLS_USE_PSA_CRYPTO */
7923
7924#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01007925 /* Get MAC length */
7926 mac_key_len = PSA_HASH_LENGTH(mac_alg);
7927#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007928 /* Initialize HMAC contexts */
7929 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
7930 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
7931 {
7932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
7933 goto end;
7934 }
7935
7936 /* Get MAC length */
7937 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01007938#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007939 transform->maclen = mac_key_len;
7940
7941 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007942#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02007943 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007944#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007945 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007946#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007947
7948 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007949 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007950 transform->minlen = transform->maclen;
7951 else
7952 {
7953 /*
7954 * GenericBlockCipher:
7955 * 1. if EtM is in use: one block plus MAC
7956 * otherwise: * first multiple of blocklen greater than maclen
7957 * 2. IV
7958 */
7959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007960 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007961 {
7962 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007963 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007964 }
7965 else
7966#endif
7967 {
7968 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007969 + block_size
7970 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007971 }
7972
Glenn Strauss07c64162022-03-14 12:34:51 -04007973 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007974 {
7975 transform->minlen += transform->ivlen;
7976 }
7977 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007978 {
7979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7980 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7981 goto end;
7982 }
7983 }
7984 }
7985 else
7986#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
7987 {
7988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7990 }
7991
7992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
7993 (unsigned) keylen,
7994 (unsigned) transform->minlen,
7995 (unsigned) transform->ivlen,
7996 (unsigned) transform->maclen ) );
7997
7998 /*
7999 * Finally setup the cipher contexts, IVs and MAC secrets.
8000 */
8001#if defined(MBEDTLS_SSL_CLI_C)
8002 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8003 {
8004 key1 = keyblk + mac_key_len * 2;
8005 key2 = keyblk + mac_key_len * 2 + keylen;
8006
8007 mac_enc = keyblk;
8008 mac_dec = keyblk + mac_key_len;
8009
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008010 iv_copy_len = ( transform->fixed_ivlen ) ?
8011 transform->fixed_ivlen : transform->ivlen;
8012 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8013 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8014 iv_copy_len );
8015 }
8016 else
8017#endif /* MBEDTLS_SSL_CLI_C */
8018#if defined(MBEDTLS_SSL_SRV_C)
8019 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8020 {
8021 key1 = keyblk + mac_key_len * 2 + keylen;
8022 key2 = keyblk + mac_key_len * 2;
8023
8024 mac_enc = keyblk + mac_key_len;
8025 mac_dec = keyblk;
8026
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008027 iv_copy_len = ( transform->fixed_ivlen ) ?
8028 transform->fixed_ivlen : transform->ivlen;
8029 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8030 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8031 iv_copy_len );
8032 }
8033 else
8034#endif /* MBEDTLS_SSL_SRV_C */
8035 {
8036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8037 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8038 goto end;
8039 }
8040
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008041 if( ssl != NULL && ssl->f_export_keys != NULL )
8042 {
8043 ssl->f_export_keys( ssl->p_export_keys,
8044 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8045 master, 48,
8046 randbytes + 32,
8047 randbytes,
8048 tls_prf_get_type( tls_prf ) );
8049 }
8050
8051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008052 transform->psa_alg = alg;
8053
8054 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8055 {
8056 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8057 psa_set_key_algorithm( &attributes, alg );
8058 psa_set_key_type( &attributes, key_type );
8059
8060 if( ( status = psa_import_key( &attributes,
8061 key1,
8062 PSA_BITS_TO_BYTES( key_bits ),
8063 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8064 {
8065 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8066 ret = psa_ssl_status_to_mbedtls( status );
8067 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8068 goto end;
8069 }
8070
8071 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8072
8073 if( ( status = psa_import_key( &attributes,
8074 key2,
8075 PSA_BITS_TO_BYTES( key_bits ),
8076 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8077 {
8078 ret = psa_ssl_status_to_mbedtls( status );
8079 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8080 goto end;
8081 }
8082 }
8083#else
8084 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8085 cipher_info ) ) != 0 )
8086 {
8087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8088 goto end;
8089 }
8090
8091 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8092 cipher_info ) ) != 0 )
8093 {
8094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8095 goto end;
8096 }
8097
8098 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8099 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8100 MBEDTLS_ENCRYPT ) ) != 0 )
8101 {
8102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8103 goto end;
8104 }
8105
8106 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8107 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8108 MBEDTLS_DECRYPT ) ) != 0 )
8109 {
8110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8111 goto end;
8112 }
8113
8114#if defined(MBEDTLS_CIPHER_MODE_CBC)
8115 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8116 {
8117 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8118 MBEDTLS_PADDING_NONE ) ) != 0 )
8119 {
8120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8121 goto end;
8122 }
8123
8124 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8125 MBEDTLS_PADDING_NONE ) ) != 0 )
8126 {
8127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8128 goto end;
8129 }
8130 }
8131#endif /* MBEDTLS_CIPHER_MODE_CBC */
8132#endif /* MBEDTLS_USE_PSA_CRYPTO */
8133
Neil Armstrong29c0c042022-03-17 17:47:28 +01008134#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8135 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8136 For AEAD-based ciphersuites, there is nothing to do here. */
8137 if( mac_key_len != 0 )
8138 {
8139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008140 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008141
8142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008143 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008144 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8145
8146 if( ( status = psa_import_key( &attributes,
8147 mac_enc, mac_key_len,
8148 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8149 {
8150 ret = psa_ssl_status_to_mbedtls( status );
8151 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8152 goto end;
8153 }
8154
Ronald Cronfb39f152022-03-25 14:36:28 +01008155 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008156 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8157#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8158 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8159#endif
8160 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008161 /* mbedtls_ct_hmac() requires the key to be exportable */
8162 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8163 PSA_KEY_USAGE_VERIFY_HASH );
8164 else
8165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8166
8167 if( ( status = psa_import_key( &attributes,
8168 mac_dec, mac_key_len,
8169 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8170 {
8171 ret = psa_ssl_status_to_mbedtls( status );
8172 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8173 goto end;
8174 }
8175#else
8176 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8177 if( ret != 0 )
8178 goto end;
8179 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8180 if( ret != 0 )
8181 goto end;
8182#endif /* MBEDTLS_USE_PSA_CRYPTO */
8183 }
8184#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8185
8186 ((void) mac_dec);
8187 ((void) mac_enc);
8188
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008189end:
8190 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8191 return( ret );
8192}
8193
Valerio Settia08b1a42022-11-17 15:10:02 +01008194#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8195 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008196int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008197 psa_pake_operation_t *pake_ctx,
8198 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008199 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008200{
8201 psa_status_t status;
8202 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008203 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008204 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8205 * At round two perform a single cycle
8206 */
8207 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008208
Valerio Setti6b3dab02022-11-17 17:14:54 +01008209 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008210 {
8211 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8212 step <= PSA_PAKE_STEP_ZK_PROOF;
8213 ++step )
8214 {
8215 /* Length is stored at the first byte */
8216 size_t length = buf[input_offset];
8217 input_offset += 1;
8218
8219 if( input_offset + length > len )
8220 {
8221 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8222 }
8223
8224 status = psa_pake_input( pake_ctx, step,
8225 buf + input_offset, length );
8226 if( status != PSA_SUCCESS)
8227 {
8228 return psa_ssl_status_to_mbedtls( status );
8229 }
8230
8231 input_offset += length;
8232 }
8233 }
8234
Valerio Setti819de862022-11-17 18:05:19 +01008235 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008236 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008237
Valerio Settia08b1a42022-11-17 15:10:02 +01008238 return( 0 );
8239}
8240
Valerio Setti6b3dab02022-11-17 17:14:54 +01008241int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008242 psa_pake_operation_t *pake_ctx,
8243 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008244 size_t len, size_t *olen,
8245 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008246{
8247 psa_status_t status;
8248 size_t output_offset = 0;
8249 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008250 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008251 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8252 * At round two perform a single cycle
8253 */
8254 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008255
Valerio Setti6b3dab02022-11-17 17:14:54 +01008256 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008257 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008258 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8259 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008260 ++step )
8261 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008262 /*
8263 * For each step, prepend 1 byte with the length of the data.
8264 *
8265 * NOTE = psa_pake_output() sometimes output elements which are
8266 * NOT 32 or 65 bytes as expected, but 1 byte less. So, instead
8267 * of hardcoding the expected length, we
8268 * - get the output first
8269 * - then write the length of this output
8270 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008271 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008272 buf + output_offset + 1,
8273 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008274 &output_len );
8275 if( status != PSA_SUCCESS )
8276 {
8277 return( psa_ssl_status_to_mbedtls( status ) );
8278 }
8279
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008280 *(buf + output_offset) = output_len;
8281
8282 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008283 }
8284 }
8285
8286 *olen = output_offset;
8287
8288 return( 0 );
8289}
Valerio Settia08b1a42022-11-17 15:10:02 +01008290#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8291
Jerry Yuee40f9d2022-02-17 14:55:16 +08008292#if defined(MBEDTLS_USE_PSA_CRYPTO)
8293int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8294 unsigned char *hash, size_t *hashlen,
8295 unsigned char *data, size_t data_len,
8296 mbedtls_md_type_t md_alg )
8297{
8298 psa_status_t status;
8299 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008300 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008301
8302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8303
8304 if( ( status = psa_hash_setup( &hash_operation,
8305 hash_alg ) ) != PSA_SUCCESS )
8306 {
8307 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8308 goto exit;
8309 }
8310
8311 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8312 64 ) ) != PSA_SUCCESS )
8313 {
8314 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8315 goto exit;
8316 }
8317
8318 if( ( status = psa_hash_update( &hash_operation,
8319 data, data_len ) ) != PSA_SUCCESS )
8320 {
8321 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8322 goto exit;
8323 }
8324
8325 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8326 hashlen ) ) != PSA_SUCCESS )
8327 {
8328 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8329 goto exit;
8330 }
8331
8332exit:
8333 if( status != PSA_SUCCESS )
8334 {
8335 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8336 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8337 switch( status )
8338 {
8339 case PSA_ERROR_NOT_SUPPORTED:
8340 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8341 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8342 case PSA_ERROR_BUFFER_TOO_SMALL:
8343 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8344 case PSA_ERROR_INSUFFICIENT_MEMORY:
8345 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8346 default:
8347 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8348 }
8349 }
8350 return( 0 );
8351}
8352
8353#else
8354
8355int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8356 unsigned char *hash, size_t *hashlen,
8357 unsigned char *data, size_t data_len,
8358 mbedtls_md_type_t md_alg )
8359{
8360 int ret = 0;
8361 mbedtls_md_context_t ctx;
8362 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8363 *hashlen = mbedtls_md_get_size( md_info );
8364
8365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8366
8367 mbedtls_md_init( &ctx );
8368
8369 /*
8370 * digitally-signed struct {
8371 * opaque client_random[32];
8372 * opaque server_random[32];
8373 * ServerDHParams params;
8374 * };
8375 */
8376 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8377 {
8378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8379 goto exit;
8380 }
8381 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8382 {
8383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8384 goto exit;
8385 }
8386 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8387 {
8388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8389 goto exit;
8390 }
8391 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8392 {
8393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8394 goto exit;
8395 }
8396 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8397 {
8398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8399 goto exit;
8400 }
8401
8402exit:
8403 mbedtls_md_free( &ctx );
8404
8405 if( ret != 0 )
8406 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8407 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8408
8409 return( ret );
8410}
8411#endif /* MBEDTLS_USE_PSA_CRYPTO */
8412
Jerry Yud9d91da2022-02-17 14:57:06 +08008413#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8414
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008415/* Find the preferred hash for a given signature algorithm. */
8416unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8417 mbedtls_ssl_context *ssl,
8418 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008419{
Gabor Mezei078e8032022-04-27 21:17:56 +02008420 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008421 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008422
8423 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008424 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008425
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008426 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008427 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008428 unsigned int hash_alg_received =
8429 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8430 received_sig_algs[i] );
8431 unsigned int sig_alg_received =
8432 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8433 received_sig_algs[i] );
8434
8435 if( sig_alg == sig_alg_received )
8436 {
8437#if defined(MBEDTLS_USE_PSA_CRYPTO)
8438 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8439 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008440 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008441 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008442
Neil Armstrong96eceb82022-06-30 18:05:05 +02008443 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8444 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8445 PSA_ALG_ECDSA( psa_hash_alg ),
8446 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008447 continue;
8448
Neil Armstrong96eceb82022-06-30 18:05:05 +02008449 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008450 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8451 PSA_ALG_RSA_PKCS1V15_SIGN(
8452 psa_hash_alg ),
8453 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008454 continue;
8455 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008456#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008457
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008458 return( hash_alg_received );
8459 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008460 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008461
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008462 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008463}
8464
8465#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8466
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008467/* Serialization of TLS 1.2 sessions:
8468 *
8469 * struct {
8470 * uint64 start_time;
8471 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008472 * uint8 session_id_len; // at most 32
8473 * opaque session_id[32];
8474 * opaque master[48]; // fixed length in the standard
8475 * uint32 verify_result;
8476 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8477 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8478 * uint32 ticket_lifetime;
8479 * uint8 mfl_code; // up to 255 according to standard
8480 * uint8 encrypt_then_mac; // 0 or 1
8481 * } serialized_session_tls12;
8482 *
8483 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008484static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008485 unsigned char *buf,
8486 size_t buf_len )
8487{
8488 unsigned char *p = buf;
8489 size_t used = 0;
8490
8491#if defined(MBEDTLS_HAVE_TIME)
8492 uint64_t start;
8493#endif
8494#if defined(MBEDTLS_X509_CRT_PARSE_C)
8495#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8496 size_t cert_len;
8497#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8498#endif /* MBEDTLS_X509_CRT_PARSE_C */
8499
8500 /*
8501 * Time
8502 */
8503#if defined(MBEDTLS_HAVE_TIME)
8504 used += 8;
8505
8506 if( used <= buf_len )
8507 {
8508 start = (uint64_t) session->start;
8509
8510 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8511 p += 8;
8512 }
8513#endif /* MBEDTLS_HAVE_TIME */
8514
8515 /*
8516 * Basic mandatory fields
8517 */
8518 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008519 + 1 /* id_len */
8520 + sizeof( session->id )
8521 + sizeof( session->master )
8522 + 4; /* verify_result */
8523
8524 if( used <= buf_len )
8525 {
8526 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8527 p += 2;
8528
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008529 *p++ = MBEDTLS_BYTE_0( session->id_len );
8530 memcpy( p, session->id, 32 );
8531 p += 32;
8532
8533 memcpy( p, session->master, 48 );
8534 p += 48;
8535
8536 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8537 p += 4;
8538 }
8539
8540 /*
8541 * Peer's end-entity certificate
8542 */
8543#if defined(MBEDTLS_X509_CRT_PARSE_C)
8544#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8545 if( session->peer_cert == NULL )
8546 cert_len = 0;
8547 else
8548 cert_len = session->peer_cert->raw.len;
8549
8550 used += 3 + cert_len;
8551
8552 if( used <= buf_len )
8553 {
8554 *p++ = MBEDTLS_BYTE_2( cert_len );
8555 *p++ = MBEDTLS_BYTE_1( cert_len );
8556 *p++ = MBEDTLS_BYTE_0( cert_len );
8557
8558 if( session->peer_cert != NULL )
8559 {
8560 memcpy( p, session->peer_cert->raw.p, cert_len );
8561 p += cert_len;
8562 }
8563 }
8564#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8565 if( session->peer_cert_digest != NULL )
8566 {
8567 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8568 if( used <= buf_len )
8569 {
8570 *p++ = (unsigned char) session->peer_cert_digest_type;
8571 *p++ = (unsigned char) session->peer_cert_digest_len;
8572 memcpy( p, session->peer_cert_digest,
8573 session->peer_cert_digest_len );
8574 p += session->peer_cert_digest_len;
8575 }
8576 }
8577 else
8578 {
8579 used += 2;
8580 if( used <= buf_len )
8581 {
8582 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8583 *p++ = 0;
8584 }
8585 }
8586#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8587#endif /* MBEDTLS_X509_CRT_PARSE_C */
8588
8589 /*
8590 * Session ticket if any, plus associated data
8591 */
8592#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8593 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8594
8595 if( used <= buf_len )
8596 {
8597 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8598 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8599 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8600
8601 if( session->ticket != NULL )
8602 {
8603 memcpy( p, session->ticket, session->ticket_len );
8604 p += session->ticket_len;
8605 }
8606
8607 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8608 p += 4;
8609 }
8610#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8611
8612 /*
8613 * Misc extension-related info
8614 */
8615#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8616 used += 1;
8617
8618 if( used <= buf_len )
8619 *p++ = session->mfl_code;
8620#endif
8621
8622#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8623 used += 1;
8624
8625 if( used <= buf_len )
8626 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8627#endif
8628
8629 return( used );
8630}
8631
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008632MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008633static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008634 const unsigned char *buf,
8635 size_t len )
8636{
8637#if defined(MBEDTLS_HAVE_TIME)
8638 uint64_t start;
8639#endif
8640#if defined(MBEDTLS_X509_CRT_PARSE_C)
8641#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8642 size_t cert_len;
8643#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8644#endif /* MBEDTLS_X509_CRT_PARSE_C */
8645
8646 const unsigned char *p = buf;
8647 const unsigned char * const end = buf + len;
8648
8649 /*
8650 * Time
8651 */
8652#if defined(MBEDTLS_HAVE_TIME)
8653 if( 8 > (size_t)( end - p ) )
8654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8655
8656 start = ( (uint64_t) p[0] << 56 ) |
8657 ( (uint64_t) p[1] << 48 ) |
8658 ( (uint64_t) p[2] << 40 ) |
8659 ( (uint64_t) p[3] << 32 ) |
8660 ( (uint64_t) p[4] << 24 ) |
8661 ( (uint64_t) p[5] << 16 ) |
8662 ( (uint64_t) p[6] << 8 ) |
8663 ( (uint64_t) p[7] );
8664 p += 8;
8665
8666 session->start = (time_t) start;
8667#endif /* MBEDTLS_HAVE_TIME */
8668
8669 /*
8670 * Basic mandatory fields
8671 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008672 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8674
8675 session->ciphersuite = ( p[0] << 8 ) | p[1];
8676 p += 2;
8677
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008678 session->id_len = *p++;
8679 memcpy( session->id, p, 32 );
8680 p += 32;
8681
8682 memcpy( session->master, p, 48 );
8683 p += 48;
8684
8685 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8686 ( (uint32_t) p[1] << 16 ) |
8687 ( (uint32_t) p[2] << 8 ) |
8688 ( (uint32_t) p[3] );
8689 p += 4;
8690
8691 /* Immediately clear invalid pointer values that have been read, in case
8692 * we exit early before we replaced them with valid ones. */
8693#if defined(MBEDTLS_X509_CRT_PARSE_C)
8694#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8695 session->peer_cert = NULL;
8696#else
8697 session->peer_cert_digest = NULL;
8698#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8699#endif /* MBEDTLS_X509_CRT_PARSE_C */
8700#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8701 session->ticket = NULL;
8702#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8703
8704 /*
8705 * Peer certificate
8706 */
8707#if defined(MBEDTLS_X509_CRT_PARSE_C)
8708#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8709 /* Deserialize CRT from the end of the ticket. */
8710 if( 3 > (size_t)( end - p ) )
8711 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8712
8713 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8714 p += 3;
8715
8716 if( cert_len != 0 )
8717 {
8718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8719
8720 if( cert_len > (size_t)( end - p ) )
8721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8722
8723 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8724
8725 if( session->peer_cert == NULL )
8726 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8727
8728 mbedtls_x509_crt_init( session->peer_cert );
8729
8730 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8731 p, cert_len ) ) != 0 )
8732 {
8733 mbedtls_x509_crt_free( session->peer_cert );
8734 mbedtls_free( session->peer_cert );
8735 session->peer_cert = NULL;
8736 return( ret );
8737 }
8738
8739 p += cert_len;
8740 }
8741#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8742 /* Deserialize CRT digest from the end of the ticket. */
8743 if( 2 > (size_t)( end - p ) )
8744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8745
8746 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8747 session->peer_cert_digest_len = (size_t) *p++;
8748
8749 if( session->peer_cert_digest_len != 0 )
8750 {
8751 const mbedtls_md_info_t *md_info =
8752 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8753 if( md_info == NULL )
8754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8755 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8757
8758 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8760
8761 session->peer_cert_digest =
8762 mbedtls_calloc( 1, session->peer_cert_digest_len );
8763 if( session->peer_cert_digest == NULL )
8764 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8765
8766 memcpy( session->peer_cert_digest, p,
8767 session->peer_cert_digest_len );
8768 p += session->peer_cert_digest_len;
8769 }
8770#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8771#endif /* MBEDTLS_X509_CRT_PARSE_C */
8772
8773 /*
8774 * Session ticket and associated data
8775 */
8776#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8777 if( 3 > (size_t)( end - p ) )
8778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8779
8780 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8781 p += 3;
8782
8783 if( session->ticket_len != 0 )
8784 {
8785 if( session->ticket_len > (size_t)( end - p ) )
8786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8787
8788 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8789 if( session->ticket == NULL )
8790 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8791
8792 memcpy( session->ticket, p, session->ticket_len );
8793 p += session->ticket_len;
8794 }
8795
8796 if( 4 > (size_t)( end - p ) )
8797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8798
8799 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8800 ( (uint32_t) p[1] << 16 ) |
8801 ( (uint32_t) p[2] << 8 ) |
8802 ( (uint32_t) p[3] );
8803 p += 4;
8804#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8805
8806 /*
8807 * Misc extension-related info
8808 */
8809#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8810 if( 1 > (size_t)( end - p ) )
8811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8812
8813 session->mfl_code = *p++;
8814#endif
8815
8816#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8817 if( 1 > (size_t)( end - p ) )
8818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8819
8820 session->encrypt_then_mac = *p++;
8821#endif
8822
8823 /* Done, should have consumed entire buffer */
8824 if( p != end )
8825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8826
8827 return( 0 );
8828}
Jerry Yudc7bd172022-02-17 13:44:15 +08008829#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8830
XiaokangQian75d40ef2022-04-20 11:05:24 +00008831int mbedtls_ssl_validate_ciphersuite(
8832 const mbedtls_ssl_context *ssl,
8833 const mbedtls_ssl_ciphersuite_t *suite_info,
8834 mbedtls_ssl_protocol_version min_tls_version,
8835 mbedtls_ssl_protocol_version max_tls_version )
8836{
8837 (void) ssl;
8838
8839 if( suite_info == NULL )
8840 return( -1 );
8841
8842 if( ( suite_info->min_tls_version > max_tls_version ) ||
8843 ( suite_info->max_tls_version < min_tls_version ) )
8844 {
8845 return( -1 );
8846 }
8847
XiaokangQian060d8672022-04-21 09:24:56 +00008848#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008849#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02008850#if defined(MBEDTLS_USE_PSA_CRYPTO)
8851 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8852 ssl->handshake->psa_pake_ctx_is_ok != 1 )
8853#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00008854 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8855 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02008856#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00008857 {
8858 return( -1 );
8859 }
8860#endif
8861
8862 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8863#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8864 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8865 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8866 {
8867 return( -1 );
8868 }
8869#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8870#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8871
8872 return( 0 );
8873}
8874
Ronald Crone68ab4f2022-10-05 12:46:29 +02008875#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008876/*
8877 * Function for writing a signature algorithm extension.
8878 *
8879 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8880 * value (TLS 1.3 RFC8446):
8881 * enum {
8882 * ....
8883 * ecdsa_secp256r1_sha256( 0x0403 ),
8884 * ecdsa_secp384r1_sha384( 0x0503 ),
8885 * ecdsa_secp521r1_sha512( 0x0603 ),
8886 * ....
8887 * } SignatureScheme;
8888 *
8889 * struct {
8890 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8891 * } SignatureSchemeList;
8892 *
8893 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8894 * value (TLS 1.2 RFC5246):
8895 * enum {
8896 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8897 * sha512(6), (255)
8898 * } HashAlgorithm;
8899 *
8900 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8901 * SignatureAlgorithm;
8902 *
8903 * struct {
8904 * HashAlgorithm hash;
8905 * SignatureAlgorithm signature;
8906 * } SignatureAndHashAlgorithm;
8907 *
8908 * SignatureAndHashAlgorithm
8909 * supported_signature_algorithms<2..2^16-2>;
8910 *
8911 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8912 * generalization of the TLS 1.2 signature algorithm extension.
8913 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8914 * `SignatureScheme` field of TLS 1.3
8915 *
8916 */
8917int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8918 const unsigned char *end, size_t *out_len )
8919{
8920 unsigned char *p = buf;
8921 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8922 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8923
8924 *out_len = 0;
8925
8926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8927
8928 /* Check if we have space for header and length field:
8929 * - extension_type (2 bytes)
8930 * - extension_data_length (2 bytes)
8931 * - supported_signature_algorithms_length (2 bytes)
8932 */
8933 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8934 p += 6;
8935
8936 /*
8937 * Write supported_signature_algorithms
8938 */
8939 supported_sig_alg = p;
8940 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8941 if( sig_alg == NULL )
8942 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8943
8944 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8945 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8947 *sig_alg,
8948 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008949 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8950 continue;
8951 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8952 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8953 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008955 *sig_alg,
8956 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008957 }
8958
8959 /* Length of supported_signature_algorithms */
8960 supported_sig_alg_len = p - supported_sig_alg;
8961 if( supported_sig_alg_len == 0 )
8962 {
8963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8965 }
8966
XiaokangQianeaf36512022-04-24 09:07:44 +00008967 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008968 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008969 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8970
XiaokangQianeaf36512022-04-24 09:07:44 +00008971 *out_len = p - buf;
8972
8973#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
8974 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
8975#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8976 return( 0 );
8977}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008978#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008979
XiaokangQian40a35232022-05-07 09:02:40 +00008980#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008981/*
8982 * mbedtls_ssl_parse_server_name_ext
8983 *
8984 * Structure of server_name extension:
8985 *
8986 * enum {
8987 * host_name(0), (255)
8988 * } NameType;
8989 * opaque HostName<1..2^16-1>;
8990 *
8991 * struct {
8992 * NameType name_type;
8993 * select (name_type) {
8994 * case host_name: HostName;
8995 * } name;
8996 * } ServerName;
8997 * struct {
8998 * ServerName server_name_list<1..2^16-1>
8999 * } ServerNameList;
9000 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009001MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009002int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9003 const unsigned char *buf,
9004 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009005{
9006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9007 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009008 size_t server_name_list_len, hostname_len;
9009 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009010
XiaokangQianf2a94202022-05-20 06:44:24 +00009011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009012
9013 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009014 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009015 p += 2;
9016
XiaokangQian9b2b7712022-05-17 02:57:00 +00009017 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9018 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009019 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009020 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009021 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009022 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009023 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9024 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009025
9026 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9027 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009028 /* sni_name is intended to be used only during the parsing of the
9029 * ClientHello message (it is reset to NULL before the end of
9030 * the message parsing). Thus it is ok to just point to the
9031 * reception buffer and not make a copy of it.
9032 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009033 ssl->handshake->sni_name = p + 3;
9034 ssl->handshake->sni_name_len = hostname_len;
9035 if( ssl->conf->f_sni == NULL )
9036 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009037 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009038 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009039 if( ret != 0 )
9040 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009041 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009042 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9043 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009044 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9045 }
9046 return( 0 );
9047 }
9048
9049 p += hostname_len + 3;
9050 }
9051
9052 return( 0 );
9053}
9054#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9055
XiaokangQianacb39922022-06-17 10:18:48 +00009056#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009057MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009058int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9059 const unsigned char *buf,
9060 const unsigned char *end )
9061{
9062 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009063 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009064 const unsigned char *protocol_name_list;
9065 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009066 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009067
9068 /* If ALPN not configured, just ignore the extension */
9069 if( ssl->conf->alpn_list == NULL )
9070 return( 0 );
9071
9072 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009073 * RFC7301, section 3.1
9074 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009075 *
XiaokangQianc7403452022-06-23 03:24:12 +00009076 * struct {
9077 * ProtocolName protocol_name_list<2..2^16-1>
9078 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009079 */
9080
XiaokangQianc7403452022-06-23 03:24:12 +00009081 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009082 * protocol_name_list_len 2 bytes
9083 * protocol_name_len 1 bytes
9084 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009085 */
XiaokangQianacb39922022-06-17 10:18:48 +00009086 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9087
XiaokangQianc7403452022-06-23 03:24:12 +00009088 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009089 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009090 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009091 protocol_name_list = p;
9092 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009093
9094 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009095 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009096 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009097 protocol_name_len = *p++;
9098 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9099 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009100 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009101 {
9102 MBEDTLS_SSL_PEND_FATAL_ALERT(
9103 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9104 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009105 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009106 }
9107
9108 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009109 }
9110
9111 /* Use our order of preference */
9112 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9113 {
9114 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009115 p = protocol_name_list;
9116 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009117 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009118 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009119 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009120 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009121 {
9122 ssl->alpn_chosen = *alpn;
9123 return( 0 );
9124 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009125
9126 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009127 }
9128 }
9129
XiaokangQian95d5f542022-06-24 02:29:26 +00009130 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009131 MBEDTLS_SSL_PEND_FATAL_ALERT(
9132 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9133 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9134 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9135}
9136
9137int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9138 unsigned char *buf,
9139 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009140 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009141{
9142 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009143 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009144 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009145
9146 if( ssl->alpn_chosen == NULL )
9147 {
9148 return( 0 );
9149 }
9150
XiaokangQian95d5f542022-06-24 02:29:26 +00009151 protocol_name_len = strlen( ssl->alpn_chosen );
9152 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009153
9154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9155 /*
9156 * 0 . 1 ext identifier
9157 * 2 . 3 ext length
9158 * 4 . 5 protocol list length
9159 * 6 . 6 protocol name length
9160 * 7 . 7+n protocol name
9161 */
9162 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9163
XiaokangQian95d5f542022-06-24 02:29:26 +00009164 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009165
XiaokangQian95d5f542022-06-24 02:29:26 +00009166 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9167 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009168 /* Note: the length of the chosen protocol has been checked to be less
9169 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9170 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009171 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009172
XiaokangQian95d5f542022-06-24 02:29:26 +00009173 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009174 return ( 0 );
9175}
9176#endif /* MBEDTLS_SSL_ALPN */
9177
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009178#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009179 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009180 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009181 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009182int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9183 const char *hostname )
9184{
9185 /* Initialize to suppress unnecessary compiler warning */
9186 size_t hostname_len = 0;
9187
9188 /* Check if new hostname is valid before
9189 * making any change to current one */
9190 if( hostname != NULL )
9191 {
9192 hostname_len = strlen( hostname );
9193
9194 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9196 }
9197
9198 /* Now it's clear that we will overwrite the old hostname,
9199 * so we can free it safely */
9200 if( session->hostname != NULL )
9201 {
9202 mbedtls_platform_zeroize( session->hostname,
9203 strlen( session->hostname ) );
9204 mbedtls_free( session->hostname );
9205 }
9206
9207 /* Passing NULL as hostname shall clear the old one */
9208 if( hostname == NULL )
9209 {
9210 session->hostname = NULL;
9211 }
9212 else
9213 {
9214 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9215 if( session->hostname == NULL )
9216 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9217
9218 memcpy( session->hostname, hostname, hostname_len );
9219 }
9220
9221 return( 0 );
9222}
Xiaokang Qian03409292022-10-12 02:49:52 +00009223#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9224 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009225 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009226 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228#endif /* MBEDTLS_SSL_TLS_C */