blob: 35565dee4acc16381524015b5a4b65f8ccb6f9a7 [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 ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 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
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 handshake->psa_pake_ctx = psa_pake_operation_init();
912 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
913#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200914 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +0200915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200916#if defined(MBEDTLS_SSL_CLI_C)
917 handshake->ecjpake_cache = NULL;
918 handshake->ecjpake_cache_len = 0;
919#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200920#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921
Gilles Peskineeccd8882020-03-10 12:19:08 +0100922#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200923 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200924#endif
925
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
927 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
928#endif
Hanno Becker75173122019-02-06 16:18:31 +0000929
930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
931 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
932 mbedtls_pk_init( &handshake->peer_pubkey );
933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Hanno Beckera18d1322018-01-03 14:27:32 +0000936void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200939
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
942 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100943#else
944 mbedtls_cipher_init( &transform->cipher_ctx_enc );
945 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#endif
947
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
951 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100952#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_md_init( &transform->md_ctx_enc );
954 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000955#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962}
963
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200964MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000966{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +0000969 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Jerry Yu2e199812022-12-01 18:57:19 +0800971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200975 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200976
Jerry Yu2e199812022-12-01 18:57:19 +0800977#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978 /*
979 * Either the pointers are now NULL or cleared properly and can be freed.
980 * Now allocate missing structures.
981 */
982 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200983 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200984 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 }
Jerry Yu2e199812022-12-01 18:57:19 +0800986#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000987
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200989 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200990 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200991 }
Paul Bakker48916f92012-09-16 19:57:18 +0000992
Paul Bakker82788fb2014-10-20 13:59:19 +0200993 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200994 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200995 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200996 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500997#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
998 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400999
Andrzej Kurek4a063792020-10-21 15:08:44 +02001000 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1001 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001002#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001004 /* All pointers should exist and can be directly freed without issue */
Jerry Yu2e199812022-12-01 18:57:19 +08001005 if( ssl->handshake == NULL ||
1006#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001007 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001008#endif
1009 ssl->session_negotiate == NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00001010 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001014 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001015
1016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1017 mbedtls_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001019#endif
1020
1021 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022 ssl->session_negotiate = NULL;
1023
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001025 }
1026
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001027 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001029 ssl_handshake_params_init( ssl->handshake );
1030
Jerry Yu2e199812022-12-01 18:57:19 +08001031#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1032 mbedtls_ssl_transform_init( ssl->transform_negotiate );
1033#endif
1034
Jerry Yud0766ec2022-09-22 10:46:57 +08001035#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1036 defined(MBEDTLS_SSL_SRV_C) && \
1037 defined(MBEDTLS_SSL_SESSION_TICKETS)
1038 ssl->handshake->new_session_tickets_count =
1039 ssl->conf->new_session_tickets_count ;
1040#endif
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001043 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1044 {
1045 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001046
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001047 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1048 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1049 else
1050 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001051
Hanno Becker0f57a652020-02-05 10:37:26 +00001052 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001053 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001054#endif
1055
Brett Warrene0edc842021-08-17 09:53:13 +01001056/*
1057 * curve_list is translated to IANA TLS group identifiers here because
1058 * mbedtls_ssl_conf_curves returns void and so can't return
1059 * any error codes.
1060 */
1061#if defined(MBEDTLS_ECP_C)
1062#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1063 /* Heap allocate and translate curve_list from internal to IANA group ids */
1064 if ( ssl->conf->curve_list != NULL )
1065 {
1066 size_t length;
1067 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1068
1069 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1070 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1071
1072 /* Leave room for zero termination */
1073 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1074 if ( group_list == NULL )
1075 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1076
1077 for( size_t i = 0; i < length; i++ )
1078 {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001079 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
1080 curve_list[i] );
1081 if ( tls_id == 0 )
Brett Warrene0edc842021-08-17 09:53:13 +01001082 {
1083 mbedtls_free( group_list );
1084 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1085 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001086 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001087 }
1088
1089 group_list[length] = 0;
1090
1091 ssl->handshake->group_list = group_list;
1092 ssl->handshake->group_list_heap_allocated = 1;
1093 }
1094 else
1095 {
1096 ssl->handshake->group_list = ssl->conf->group_list;
1097 ssl->handshake->group_list_heap_allocated = 0;
1098 }
1099#endif /* MBEDTLS_DEPRECATED_REMOVED */
1100#endif /* MBEDTLS_ECP_C */
1101
Ronald Crone68ab4f2022-10-05 12:46:29 +02001102#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001103#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001105 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1106 signature algorithms IANA identifiers. */
1107 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001108 ssl->conf->sig_hashes != NULL )
1109 {
1110 const int *md;
1111 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001112 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001113 uint16_t *p;
1114
Jerry Yub476a442022-01-21 18:14:45 +08001115#if defined(static_assert)
1116 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1117 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1118 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001119#endif
1120
Jerry Yuf017ee42022-01-12 15:49:48 +08001121 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1122 {
1123 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1124 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001125#if defined(MBEDTLS_ECDSA_C)
1126 sig_algs_len += sizeof( uint16_t );
1127#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001128
Jerry Yub476a442022-01-21 18:14:45 +08001129#if defined(MBEDTLS_RSA_C)
1130 sig_algs_len += sizeof( uint16_t );
1131#endif
1132 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1133 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001134 }
1135
Jerry Yub476a442022-01-21 18:14:45 +08001136 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001137 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1138
Jerry Yub476a442022-01-21 18:14:45 +08001139 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1140 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001141 if( ssl->handshake->sig_algs == NULL )
1142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1143
1144 p = (uint16_t *)ssl->handshake->sig_algs;
1145 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1146 {
1147 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1148 if( hash == MBEDTLS_SSL_HASH_NONE )
1149 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001150#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001151 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1152 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001153#endif
1154#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001155 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1156 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001157#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001158 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001159 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001160 ssl->handshake->sig_algs_heap_allocated = 1;
1161 }
1162 else
Jerry Yua69269a2022-01-17 21:06:01 +08001163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 ssl->handshake->sig_algs_heap_allocated = 0;
1166 }
Jerry Yucc539102022-06-27 16:27:35 +08001167#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001168#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001169 return( 0 );
1170}
1171
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001172#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001173/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001174MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001175static int ssl_cookie_write_dummy( void *ctx,
1176 unsigned char **p, unsigned char *end,
1177 const unsigned char *cli_id, size_t cli_id_len )
1178{
1179 ((void) ctx);
1180 ((void) p);
1181 ((void) end);
1182 ((void) cli_id);
1183 ((void) cli_id_len);
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001186}
1187
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001188MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001189static int ssl_cookie_check_dummy( void *ctx,
1190 const unsigned char *cookie, size_t cookie_len,
1191 const unsigned char *cli_id, size_t cli_id_len )
1192{
1193 ((void) ctx);
1194 ((void) cookie);
1195 ((void) cookie_len);
1196 ((void) cli_id);
1197 ((void) cli_id_len);
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001200}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001201#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203/*
1204 * Initialize an SSL context
1205 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001206void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1207{
1208 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1209}
1210
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001211MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001212static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1213{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001214 const mbedtls_ssl_config *conf = ssl->conf;
1215
Ronald Cron6f135e12021-12-08 16:57:54 +01001216#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001217 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1218 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001219 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1222 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1223 }
1224
Jerry Yu60835a82021-08-04 10:13:52 +08001225 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1226 return( 0 );
1227 }
1228#endif
1229
1230#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001231 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001232 {
1233 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1234 return( 0 );
1235 }
1236#endif
1237
Ronald Cron6f135e12021-12-08 16:57:54 +01001238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001239 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001240 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1242 {
1243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1244 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1245 }
1246
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001247 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1248 {
1249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1250 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1251 }
1252
1253
Ronald Crone1d3f062022-02-10 14:50:54 +01001254 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1255 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001256 }
1257#endif
1258
1259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1260 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1261}
1262
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001263MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001264static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1265{
1266 int ret;
1267 ret = ssl_conf_version_check( ssl );
1268 if( ret != 0 )
1269 return( ret );
1270
Jerry Yudef7ae42022-10-30 14:13:19 +08001271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1272 /* RFC 8446 section 4.4.3
1273 *
1274 * If the verification fails, the receiver MUST terminate the handshake with
1275 * a "decrypt_error" alert.
1276 *
1277 * If the client is configured as TLS 1.3 only with optional verify, return
1278 * bad config.
1279 *
1280 */
1281 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1282 (mbedtls_ssl_context *)ssl ) &&
1283 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1284 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1285 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1286 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001289 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001290 "is not available for TLS 1.3 client" ) );
1291 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1292 }
1293#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1294
Jerry Yu60835a82021-08-04 10:13:52 +08001295 /* Space for further checks */
1296
1297 return( 0 );
1298}
1299
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001300/*
1301 * Setup an SSL context
1302 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001303
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001304int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001305 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001306{
Janos Follath865b3eb2019-12-16 11:46:15 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001308 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1309 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001311 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001312
Jerry Yu60835a82021-08-04 10:13:52 +08001313 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1314 return( ret );
1315
Paul Bakker62f2dee2012-09-28 07:31:51 +00001316 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001317 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001318 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001319
1320 /* Set to NULL in case of an error condition */
1321 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001322
Darryl Greenb33cc762019-11-28 14:29:44 +00001323#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1324 ssl->in_buf_len = in_buf_len;
1325#endif
1326 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001327 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001330 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001331 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001332 }
1333
Darryl Greenb33cc762019-11-28 14:29:44 +00001334#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1335 ssl->out_buf_len = out_buf_len;
1336#endif
1337 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001338 if( ssl->out_buf == NULL )
1339 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001341 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001342 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001345 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001346
Johan Pascalb62bb512015-12-03 21:56:45 +01001347#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001348 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001349#endif
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001352 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001355
1356error:
1357 mbedtls_free( ssl->in_buf );
1358 mbedtls_free( ssl->out_buf );
1359
1360 ssl->conf = NULL;
1361
Darryl Greenb33cc762019-11-28 14:29:44 +00001362#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1363 ssl->in_buf_len = 0;
1364 ssl->out_buf_len = 0;
1365#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001366 ssl->in_buf = NULL;
1367 ssl->out_buf = NULL;
1368
1369 ssl->in_hdr = NULL;
1370 ssl->in_ctr = NULL;
1371 ssl->in_len = NULL;
1372 ssl->in_iv = NULL;
1373 ssl->in_msg = NULL;
1374
1375 ssl->out_hdr = NULL;
1376 ssl->out_ctr = NULL;
1377 ssl->out_len = NULL;
1378 ssl->out_iv = NULL;
1379 ssl->out_msg = NULL;
1380
k-stachowiak9f7798e2018-07-31 16:52:32 +02001381 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001382}
1383
1384/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001385 * Reset an initialized and used SSL context for re-use while retaining
1386 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001387 *
1388 * If partial is non-zero, keep data in the input buffer and client ID.
1389 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001390 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001391void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1392 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001393{
Darryl Greenb33cc762019-11-28 14:29:44 +00001394#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1395 size_t in_buf_len = ssl->in_buf_len;
1396 size_t out_buf_len = ssl->out_buf_len;
1397#else
1398 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1399 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1400#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Hanno Beckerb0302c42021-08-03 09:39:42 +01001402#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1403 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001404#endif
1405
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001406 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001407 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001408
Hanno Beckerb0302c42021-08-03 09:39:42 +01001409 mbedtls_ssl_reset_in_out_pointers( ssl );
1410
1411 /* Reset incoming message parsing */
1412 ssl->in_offt = NULL;
1413 ssl->nb_zero = 0;
1414 ssl->in_msgtype = 0;
1415 ssl->in_msglen = 0;
1416 ssl->in_hslen = 0;
1417 ssl->keep_current_message = 0;
1418 ssl->transform_in = NULL;
1419
1420#if defined(MBEDTLS_SSL_PROTO_DTLS)
1421 ssl->next_record_offset = 0;
1422 ssl->in_epoch = 0;
1423#endif
1424
1425 /* Keep current datagram if partial == 1 */
1426 if( partial == 0 )
1427 {
1428 ssl->in_left = 0;
1429 memset( ssl->in_buf, 0, in_buf_len );
1430 }
1431
Ronald Cronad8c17b2022-06-10 17:18:09 +02001432 ssl->send_alert = 0;
1433
Hanno Beckerb0302c42021-08-03 09:39:42 +01001434 /* Reset outgoing message writing */
1435 ssl->out_msgtype = 0;
1436 ssl->out_msglen = 0;
1437 ssl->out_left = 0;
1438 memset( ssl->out_buf, 0, out_buf_len );
1439 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1440 ssl->transform_out = NULL;
1441
1442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1443 mbedtls_ssl_dtls_replay_reset( ssl );
1444#endif
1445
XiaokangQian2b01dc32022-01-21 02:53:13 +00001446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001447 if( ssl->transform )
1448 {
1449 mbedtls_ssl_transform_free( ssl->transform );
1450 mbedtls_free( ssl->transform );
1451 ssl->transform = NULL;
1452 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1454
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1456 mbedtls_ssl_transform_free( ssl->transform_application );
1457 mbedtls_free( ssl->transform_application );
1458 ssl->transform_application = NULL;
1459
1460 if( ssl->handshake != NULL )
1461 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001462#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001463 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1464 mbedtls_free( ssl->handshake->transform_earlydata );
1465 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001466#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001467
1468 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1469 mbedtls_free( ssl->handshake->transform_handshake );
1470 ssl->handshake->transform_handshake = NULL;
1471 }
1472
XiaokangQian2b01dc32022-01-21 02:53:13 +00001473#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001474}
1475
1476int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1477{
1478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1479
1480 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1481
XiaokangQian78b1fa72022-01-19 06:56:30 +00001482 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483
1484 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_RENEGOTIATION)
1486 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001487 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001488
1489 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1491 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001494
Hanno Beckerb0302c42021-08-03 09:39:42 +01001495 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001496 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001497 if( ssl->session )
1498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_ssl_session_free( ssl->session );
1500 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001501 ssl->session = NULL;
1502 }
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001505 ssl->alpn_chosen = NULL;
1506#endif
1507
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001508#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001509 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001510#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001511 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001512#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001513 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001514 {
1515 mbedtls_free( ssl->cli_id );
1516 ssl->cli_id = NULL;
1517 ssl->cli_id_len = 0;
1518 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001519#endif
1520
Paul Bakker48916f92012-09-16 19:57:18 +00001521 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1522 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001523
1524 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001525}
1526
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001527/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001528 * Reset an initialized and used SSL context for re-use while retaining
1529 * all application-set variables, function pointers and data.
1530 */
1531int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1532{
Hanno Becker43aefe22020-02-05 10:44:56 +00001533 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001534}
1535
1536/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 * SSL set accessors
1538 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001539void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001541 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001542}
1543
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001544void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001547}
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001550void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001552 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001553}
1554#endif
1555
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001556void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001559}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001562
Hanno Becker1841b0a2018-08-24 11:13:57 +01001563void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1564 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001565{
1566 ssl->disable_datagram_packing = !allow_packing;
1567}
1568
1569void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1570 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001572 conf->hs_timeout_min = min;
1573 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001574}
1575#endif
1576
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001577void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001579 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001580}
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001583void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001584 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001585 void *p_vrfy )
1586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001587 conf->f_vrfy = f_vrfy;
1588 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001591
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001592void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001593 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 void *p_rng )
1595{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 conf->f_rng = f_rng;
1597 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001598}
1599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001600void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001601 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 void *p_dbg )
1603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->f_dbg = f_dbg;
1605 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001606}
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001609 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001610 mbedtls_ssl_send_t *f_send,
1611 mbedtls_ssl_recv_t *f_recv,
1612 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001613{
1614 ssl->p_bio = p_bio;
1615 ssl->f_send = f_send;
1616 ssl->f_recv = f_recv;
1617 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001618}
1619
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001620#if defined(MBEDTLS_SSL_PROTO_DTLS)
1621void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1622{
1623 ssl->mtu = mtu;
1624}
1625#endif
1626
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001627void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001628{
1629 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001630}
1631
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001632void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1633 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001634 mbedtls_ssl_set_timer_t *f_set_timer,
1635 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001636{
1637 ssl->p_timer = p_timer;
1638 ssl->f_set_timer = f_set_timer;
1639 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001640
1641 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001642 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001643}
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001646void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001647 void *p_cache,
1648 mbedtls_ssl_cache_get_t *f_get_cache,
1649 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001651 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001652 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001653 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_SSL_CLI_C)
1658int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001659{
Janos Follath865b3eb2019-12-16 11:46:15 +00001660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001661
1662 if( ssl == NULL ||
1663 session == NULL ||
1664 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001665 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001668 }
1669
Hanno Beckere810bbc2021-05-14 16:01:05 +01001670 if( ssl->handshake->resume == 1 )
1671 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1672
Jerry Yu21092062022-10-10 21:21:31 +08001673#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1674 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001675 {
Jerry Yu21092062022-10-10 21:21:31 +08001676 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1677 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1678
1679 if( mbedtls_ssl_validate_ciphersuite(
1680 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1681 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1682 {
1683 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1684 session->ciphersuite ) );
1685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1686 }
Jerry Yu40afab62022-10-08 10:42:13 +08001687 }
Jerry Yu21092062022-10-10 21:21:31 +08001688#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001689
Hanno Becker52055ae2019-02-06 14:30:46 +00001690 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1691 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001692 return( ret );
1693
Paul Bakker0a597072012-09-25 21:55:46 +00001694 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001695
1696 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001700void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1701 const int *ciphersuites )
1702{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001703 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001704}
1705
Ronald Cron6f135e12021-12-08 16:57:54 +01001706#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001707void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001708 const int kex_modes )
1709{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001710 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001711}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001712
1713#if defined(MBEDTLS_SSL_EARLY_DATA)
1714void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001715 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001716{
1717 conf->early_data_enabled = early_data_enabled;
1718}
Jerry Yucc4e0072022-11-22 17:22:22 +08001719
1720#if defined(MBEDTLS_SSL_SRV_C)
1721void mbedtls_ssl_tls13_conf_max_early_data_size(
1722 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1723{
Jerry Yu39da9852022-12-06 16:58:36 +08001724 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001725}
1726#endif /* MBEDTLS_SSL_SRV_C */
1727
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001728#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001729#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001732void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001733 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001734{
1735 conf->cert_profile = profile;
1736}
1737
Glenn Strauss36872db2022-01-22 05:06:31 -05001738static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1739{
1740 mbedtls_ssl_key_cert *cur = key_cert, *next;
1741
1742 while( cur != NULL )
1743 {
1744 next = cur->next;
1745 mbedtls_free( cur );
1746 cur = next;
1747 }
1748}
1749
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001750/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001751MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001752static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1753 mbedtls_x509_crt *cert,
1754 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001755{
niisato8ee24222018-06-25 19:05:48 +09001756 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001757
Glenn Strauss36872db2022-01-22 05:06:31 -05001758 if( cert == NULL )
1759 {
1760 /* Free list if cert is null */
1761 ssl_key_cert_free( *head );
1762 *head = NULL;
1763 return( 0 );
1764 }
1765
niisato8ee24222018-06-25 19:05:48 +09001766 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1767 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001768 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001769
niisato8ee24222018-06-25 19:05:48 +09001770 new_cert->cert = cert;
1771 new_cert->key = key;
1772 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001773
Glenn Strauss36872db2022-01-22 05:06:31 -05001774 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001775 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001776 {
niisato8ee24222018-06-25 19:05:48 +09001777 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001778 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001779 else
1780 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001781 mbedtls_ssl_key_cert *cur = *head;
1782 while( cur->next != NULL )
1783 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001784 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001785 }
1786
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001790int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001791 mbedtls_x509_crt *own_cert,
1792 mbedtls_pk_context *pk_key )
1793{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001794 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001795}
1796
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001797void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001798 mbedtls_x509_crt *ca_chain,
1799 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001800{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001801 conf->ca_chain = ca_chain;
1802 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001803
1804#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1805 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1806 * cannot be used together. */
1807 conf->f_ca_cb = NULL;
1808 conf->p_ca_cb = NULL;
1809#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001810}
Hanno Becker5adaad92019-03-27 16:54:37 +00001811
1812#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1813void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001814 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001815 void *p_ca_cb )
1816{
1817 conf->f_ca_cb = f_ca_cb;
1818 conf->p_ca_cb = p_ca_cb;
1819
1820 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1821 * cannot be used together. */
1822 conf->ca_chain = NULL;
1823 conf->ca_crl = NULL;
1824}
1825#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001827
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001828#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001829const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1830 size_t *name_len )
1831{
1832 *name_len = ssl->handshake->sni_name_len;
1833 return( ssl->handshake->sni_name );
1834}
1835
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001836int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1837 mbedtls_x509_crt *own_cert,
1838 mbedtls_pk_context *pk_key )
1839{
1840 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1841 own_cert, pk_key ) );
1842}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001843
1844void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1845 mbedtls_x509_crt *ca_chain,
1846 mbedtls_x509_crl *ca_crl )
1847{
1848 ssl->handshake->sni_ca_chain = ca_chain;
1849 ssl->handshake->sni_ca_crl = ca_crl;
1850}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001851
Glenn Strauss999ef702022-03-11 01:37:23 -05001852#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1853void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1854 const mbedtls_x509_crt *crt)
1855{
1856 ssl->handshake->dn_hints = crt;
1857}
1858#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1859
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001860void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1861 int authmode )
1862{
1863 ssl->handshake->sni_authmode = authmode;
1864}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001865#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1866
Hanno Becker8927c832019-04-03 12:52:50 +01001867#if defined(MBEDTLS_X509_CRT_PARSE_C)
1868void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1869 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1870 void *p_vrfy )
1871{
1872 ssl->f_vrfy = f_vrfy;
1873 ssl->p_vrfy = p_vrfy;
1874}
1875#endif
1876
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001877#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001878
Valerio Setti016f6822022-12-09 14:17:50 +01001879#if defined(MBEDTLS_USE_PSA_CRYPTO)
1880static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
1881 mbedtls_ssl_context *ssl,
1882 mbedtls_svc_key_id_t pwd )
1883{
1884 psa_status_t status;
1885 psa_pake_role_t psa_role;
1886 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1887
1888 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1889 psa_pake_cs_set_primitive( &cipher_suite,
1890 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1891 PSA_ECC_FAMILY_SECP_R1,
1892 256) );
1893 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1894
1895 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1896 if( status != PSA_SUCCESS )
1897 return status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001898
Neil Armstrongca7d5062022-05-31 14:43:23 +02001899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1900 psa_role = PSA_PAKE_ROLE_SERVER;
1901 else
1902 psa_role = PSA_PAKE_ROLE_CLIENT;
1903
Valerio Setti016f6822022-12-09 14:17:50 +01001904 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1905 if( status != PSA_SUCCESS )
1906 return status;
1907
1908 status = psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, pwd );
1909 if( status != PSA_SUCCESS )
1910 return status;
1911
1912 ssl->handshake->psa_pake_ctx_is_ok = 1;
1913
1914 return ( PSA_SUCCESS );
1915}
1916
1917int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1918 const unsigned char *pw,
1919 size_t pw_len )
1920{
1921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1922 psa_status_t status;
1923
1924 if( ssl->handshake == NULL || ssl->conf == NULL )
1925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1926
Valerio Settiaca21b72022-11-17 18:17:01 +01001927 /* Empty password is not valid */
1928 if( ( pw == NULL) || ( pw_len == 0 ) )
1929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001930
Valerio Settiaca21b72022-11-17 18:17:01 +01001931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1932 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1933 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001934
Valerio Settiaca21b72022-11-17 18:17:01 +01001935 status = psa_import_key( &attributes, pw, pw_len,
1936 &ssl->handshake->psa_pake_password );
1937 if( status != PSA_SUCCESS )
1938 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001939
Valerio Setti016f6822022-12-09 14:17:50 +01001940 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl,
1941 ssl->handshake->psa_pake_password );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001942 if( status != PSA_SUCCESS )
1943 {
1944 psa_destroy_key( ssl->handshake->psa_pake_password );
1945 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1946 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1947 }
1948
Neil Armstrongca7d5062022-05-31 14:43:23 +02001949 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001950}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001951
1952int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl,
1953 mbedtls_svc_key_id_t pwd )
1954{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001955 psa_status_t status;
1956
1957 if( ssl->handshake == NULL || ssl->conf == NULL )
1958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1959
Valerio Settia9a97dc2022-11-28 18:26:16 +01001960 if( mbedtls_svc_key_id_is_null( pwd ) )
1961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Valerio Settia9a97dc2022-11-28 18:26:16 +01001962
Valerio Setti016f6822022-12-09 14:17:50 +01001963 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl, pwd );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001964 if( status != PSA_SUCCESS )
1965 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001966 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1967 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1968 }
1969
Neil Armstrongca7d5062022-05-31 14:43:23 +02001970 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001971}
1972#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001973int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1974 const unsigned char *pw,
1975 size_t pw_len )
1976{
1977 mbedtls_ecjpake_role role;
1978
1979 if( ssl->handshake == NULL || ssl->conf == NULL )
1980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1981
Valerio Settic689ed82022-12-07 14:40:38 +01001982 /* Empty password is not valid */
1983 if( ( pw == NULL) || ( pw_len == 0 ) )
1984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1985
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001986 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1987 role = MBEDTLS_ECJPAKE_SERVER;
1988 else
1989 role = MBEDTLS_ECJPAKE_CLIENT;
1990
1991 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1992 role,
1993 MBEDTLS_MD_SHA256,
1994 MBEDTLS_ECP_DP_SECP256R1,
1995 pw, pw_len ) );
1996}
Valerio Setti02c25b52022-11-15 14:08:42 +01001997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001998#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001999
Ronald Cron73fe8df2022-10-05 14:31:43 +02002000#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02002001int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002002{
Ronald Crond29e13e2022-10-19 10:33:48 +02002003 if( conf->psk_identity == NULL ||
2004 conf->psk_identity_len == 0 )
2005 {
2006 return( 0 );
2007 }
2008
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002009#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02002010 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002011 return( 1 );
2012#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002013
2014 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002015 return( 1 );
2016
2017 return( 0 );
2018}
2019
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002020static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
2021{
2022 /* Remove reference to existing PSK, if any. */
2023#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002024 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002025 {
2026 /* The maintenance of the PSK key slot is the
2027 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002028 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002029 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002030#endif /* MBEDTLS_USE_PSA_CRYPTO */
2031 if( conf->psk != NULL )
2032 {
2033 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2034
2035 mbedtls_free( conf->psk );
2036 conf->psk = NULL;
2037 conf->psk_len = 0;
2038 }
2039
2040 /* Remove reference to PSK identity, if any. */
2041 if( conf->psk_identity != NULL )
2042 {
2043 mbedtls_free( conf->psk_identity );
2044 conf->psk_identity = NULL;
2045 conf->psk_identity_len = 0;
2046 }
2047}
2048
Hanno Becker7390c712018-11-15 13:33:04 +00002049/* This function assumes that PSK identity in the SSL config is unset.
2050 * It checks that the provided identity is well-formed and attempts
2051 * to make a copy of it in the SSL config.
2052 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002053MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002054static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2055 unsigned char const *psk_identity,
2056 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002057{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002058 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002059 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002060 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002061 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002062 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002063 {
2064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2065 }
2066
Hanno Becker7390c712018-11-15 13:33:04 +00002067 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2068 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002069 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002070
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002071 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002072 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002073
2074 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002075}
2076
Hanno Becker7390c712018-11-15 13:33:04 +00002077int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2078 const unsigned char *psk, size_t psk_len,
2079 const unsigned char *psk_identity, size_t psk_identity_len )
2080{
Janos Follath865b3eb2019-12-16 11:46:15 +00002081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002082
2083 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002084 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002085 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002086
2087 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002088 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002090 if( psk_len == 0 )
2091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2092 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2094
Hanno Becker7390c712018-11-15 13:33:04 +00002095 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2096 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2097 conf->psk_len = psk_len;
2098 memcpy( conf->psk, psk, conf->psk_len );
2099
2100 /* Check and set PSK Identity */
2101 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2102 if( ret != 0 )
2103 ssl_conf_remove_psk( conf );
2104
2105 return( ret );
2106}
2107
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2109{
2110#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002111 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002113 /* The maintenance of the external PSK key slot is the
2114 * user's responsibility. */
2115 if( ssl->handshake->psk_opaque_is_internal )
2116 {
2117 psa_destroy_key( ssl->handshake->psk_opaque );
2118 ssl->handshake->psk_opaque_is_internal = 0;
2119 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002120 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002121 }
Neil Armstronge952a302022-05-03 10:22:14 +02002122#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002123 if( ssl->handshake->psk != NULL )
2124 {
2125 mbedtls_platform_zeroize( ssl->handshake->psk,
2126 ssl->handshake->psk_len );
2127 mbedtls_free( ssl->handshake->psk );
2128 ssl->handshake->psk_len = 0;
2129 }
Neil Armstronge952a302022-05-03 10:22:14 +02002130#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131}
2132
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002133int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2134 const unsigned char *psk, size_t psk_len )
2135{
Neil Armstrong501c9322022-05-03 09:35:09 +02002136#if defined(MBEDTLS_USE_PSA_CRYPTO)
2137 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002139 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002141#endif /* MBEDTLS_USE_PSA_CRYPTO */
2142
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002143 if( psk == NULL || ssl->handshake == NULL )
2144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2145
2146 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2148
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002149 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002150
Neil Armstrong501c9322022-05-03 09:35:09 +02002151#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2153 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2154 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002155 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002156 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2157 else
2158 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2159 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2160 }
2161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002162
Jerry Yu568ec252022-07-22 21:27:34 +08002163#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002164 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2165 {
2166 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2167 psa_set_key_usage_flags( &key_attributes,
2168 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2169 }
2170#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2171
Neil Armstrong501c9322022-05-03 09:35:09 +02002172 psa_set_key_algorithm( &key_attributes, alg );
2173 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2174
2175 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2176 if( status != PSA_SUCCESS )
2177 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2178
2179 /* Allow calling psa_destroy_key() on psk remove */
2180 ssl->handshake->psk_opaque_is_internal = 1;
2181 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2182#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002183 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002184 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002185
2186 ssl->handshake->psk_len = psk_len;
2187 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2188
2189 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002190#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002191}
2192
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002193#if defined(MBEDTLS_USE_PSA_CRYPTO)
2194int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002195 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196 const unsigned char *psk_identity,
2197 size_t psk_identity_len )
2198{
Janos Follath865b3eb2019-12-16 11:46:15 +00002199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002200
2201 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002202 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002203 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002204
Hanno Becker7390c712018-11-15 13:33:04 +00002205 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002206 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002208 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002209
2210 /* Check and set PSK Identity */
2211 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2212 psk_identity_len );
2213 if( ret != 0 )
2214 ssl_conf_remove_psk( conf );
2215
2216 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002217}
2218
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002219int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2220 mbedtls_svc_key_id_t psk )
2221{
2222 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2223 ( ssl->handshake == NULL ) )
2224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2225
2226 ssl_remove_psk( ssl );
2227 ssl->handshake->psk_opaque = psk;
2228 return( 0 );
2229}
2230#endif /* MBEDTLS_USE_PSA_CRYPTO */
2231
Jerry Yu8897c072022-08-12 13:56:53 +08002232#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002233void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2234 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2235 size_t),
2236 void *p_psk )
2237{
2238 conf->f_psk = f_psk;
2239 conf->p_psk = p_psk;
2240}
Jerry Yu8897c072022-08-12 13:56:53 +08002241#endif /* MBEDTLS_SSL_SRV_C */
2242
Ronald Cron73fe8df2022-10-05 14:31:43 +02002243#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002244
2245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002246static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2247 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002248{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002249#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002250 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002251 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002252#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002253 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002254 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002255 return( MBEDTLS_SSL_MODE_STREAM );
2256}
2257
2258#else /* MBEDTLS_USE_PSA_CRYPTO */
2259
2260static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2261 mbedtls_cipher_mode_t mode )
2262{
2263#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2264 if( mode == MBEDTLS_MODE_CBC )
2265 return( MBEDTLS_SSL_MODE_CBC );
2266#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2267
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002268#if defined(MBEDTLS_GCM_C) || \
2269 defined(MBEDTLS_CCM_C) || \
2270 defined(MBEDTLS_CHACHAPOLY_C)
2271 if( mode == MBEDTLS_MODE_GCM ||
2272 mode == MBEDTLS_MODE_CCM ||
2273 mode == MBEDTLS_MODE_CHACHAPOLY )
2274 return( MBEDTLS_SSL_MODE_AEAD );
2275#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002276
2277 return( MBEDTLS_SSL_MODE_STREAM );
2278}
Gilles Peskine301711e2022-04-26 16:57:05 +02002279#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002280
Gilles Peskinee108d982022-04-26 16:50:40 +02002281static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2282 mbedtls_ssl_mode_t base_mode,
2283 int encrypt_then_mac )
2284{
2285#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2286 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2287 base_mode == MBEDTLS_SSL_MODE_CBC )
2288 {
2289 return( MBEDTLS_SSL_MODE_CBC_ETM );
2290 }
2291#else
2292 (void) encrypt_then_mac;
2293#endif
2294 return( base_mode );
2295}
2296
Neil Armstrongab555e02022-04-04 11:07:59 +02002297mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002298 const mbedtls_ssl_transform *transform )
2299{
Gilles Peskinee108d982022-04-26 16:50:40 +02002300 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002302 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002303#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002304 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2305#endif
2306 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002307
Gilles Peskinee108d982022-04-26 16:50:40 +02002308 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002309#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002310 encrypt_then_mac = transform->encrypt_then_mac;
2311#endif
2312 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002313}
2314
Neil Armstrongab555e02022-04-04 11:07:59 +02002315mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002316#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002317 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002318#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002319 const mbedtls_ssl_ciphersuite_t *suite )
2320{
Gilles Peskinee108d982022-04-26 16:50:40 +02002321 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2322
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002323#if defined(MBEDTLS_USE_PSA_CRYPTO)
2324 psa_status_t status;
2325 psa_algorithm_t alg;
2326 psa_key_type_t type;
2327 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002328 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2329 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002330 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002331#else
2332 const mbedtls_cipher_info_t *cipher =
2333 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002334 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002335 {
2336 base_mode =
2337 mbedtls_ssl_get_base_mode(
2338 mbedtls_cipher_info_get_mode( cipher ) );
2339 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002340#endif /* MBEDTLS_USE_PSA_CRYPTO */
2341
Gilles Peskinee108d982022-04-26 16:50:40 +02002342#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2343 int encrypt_then_mac = 0;
2344#endif
2345 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002346}
2347
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002348#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002349
2350#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002351/* Serialization of TLS 1.3 sessions:
2352 *
2353 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002354 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002355 * uint64 ticket_received;
2356 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002357 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002358 * } ClientOnlyData;
2359 *
2360 * struct {
2361 * uint8 endpoint;
2362 * uint8 ciphersuite[2];
2363 * uint32 ticket_age_add;
2364 * uint8 ticket_flags;
2365 * opaque resumption_key<0..255>;
2366 * select ( endpoint ) {
2367 * case client: ClientOnlyData;
2368 * case server: uint64 start_time;
2369 * };
2370 * } serialized_session_tls13;
2371 *
2372 */
2373#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002374MBEDTLS_CHECK_RETURN_CRITICAL
2375static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2376 unsigned char *buf,
2377 size_t buf_len,
2378 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002379{
2380 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002381#if defined(MBEDTLS_SSL_CLI_C) && \
2382 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2383 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002384 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002385#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002386 size_t needed = 1 /* endpoint */
2387 + 2 /* ciphersuite */
2388 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002389 + 1 /* ticket_flags */
2390 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002391 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002392
2393 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2395 needed += session->resumption_key_len; /* resumption_key */
2396
Jerry Yu438ddd82022-07-07 06:55:50 +00002397#if defined(MBEDTLS_HAVE_TIME)
2398 needed += 8; /* start_time or ticket_received */
2399#endif
2400
2401#if defined(MBEDTLS_SSL_CLI_C)
2402 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2403 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002404#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002405 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002406 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002407#endif
2408
Jerry Yu438ddd82022-07-07 06:55:50 +00002409 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002410 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002411
2412 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002413 if( session->ticket_len > SIZE_MAX - needed )
2414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002415
Jerry Yue28d9742022-08-18 15:44:03 +08002416 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002417 }
2418#endif /* MBEDTLS_SSL_CLI_C */
2419
Jerry Yue36fdd62022-08-17 21:31:36 +08002420 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002421 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002422 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002423
2424 p[0] = session->endpoint;
2425 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2426 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2427 p[7] = session->ticket_flags;
2428
2429 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002430 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002431 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002432 memcpy( p, session->resumption_key, session->resumption_key_len );
2433 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002434
2435#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2436 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2437 {
2438 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2439 p += 8;
2440 }
2441#endif /* MBEDTLS_HAVE_TIME */
2442
2443#if defined(MBEDTLS_SSL_CLI_C)
2444 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2445 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002446#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2447 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2448 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002449 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002450 {
2451 /* save host name */
2452 memcpy( p, session->hostname, hostname_len );
2453 p += hostname_len;
2454 }
2455#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2456
Jerry Yu438ddd82022-07-07 06:55:50 +00002457#if defined(MBEDTLS_HAVE_TIME)
2458 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2459 p += 8;
2460#endif
2461 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2462 p += 4;
2463
2464 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2465 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002466
2467 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002468 {
2469 memcpy( p, session->ticket, session->ticket_len );
2470 p += session->ticket_len;
2471 }
2472 }
2473#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002474 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002475}
2476
2477MBEDTLS_CHECK_RETURN_CRITICAL
2478static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2479 const unsigned char *buf,
2480 size_t len )
2481{
2482 const unsigned char *p = buf;
2483 const unsigned char *end = buf + len;
2484
2485 if( end - p < 9 )
2486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2487 session->endpoint = p[0];
2488 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2489 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2490 session->ticket_flags = p[7];
2491
2492 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002493 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002494 p += 9;
2495
Jerry Yubc7c1a42022-07-21 22:57:37 +08002496 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2498
Jerry Yubc7c1a42022-07-21 22:57:37 +08002499 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002501 memcpy( session->resumption_key, p, session->resumption_key_len );
2502 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002503
2504#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2505 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2506 {
2507 if( end - p < 8 )
2508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2509 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2510 p += 8;
2511 }
2512#endif /* MBEDTLS_HAVE_TIME */
2513
2514#if defined(MBEDTLS_SSL_CLI_C)
2515 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2516 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002517#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2518 defined(MBEDTLS_SSL_SESSION_TICKETS)
2519 size_t hostname_len;
2520 /* load host name */
2521 if( end - p < 2 )
2522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2523 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2524 p += 2;
2525
2526 if( end - p < ( long int )hostname_len )
2527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 if( hostname_len > 0 )
2529 {
2530 session->hostname = mbedtls_calloc( 1, hostname_len );
2531 if( session->hostname == NULL )
2532 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2533 memcpy( session->hostname, p, hostname_len );
2534 p += hostname_len;
2535 }
2536#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2537 MBEDTLS_SSL_SESSION_TICKETS */
2538
Jerry Yu438ddd82022-07-07 06:55:50 +00002539#if defined(MBEDTLS_HAVE_TIME)
2540 if( end - p < 8 )
2541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2542 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2543 p += 8;
2544#endif
2545 if( end - p < 4 )
2546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2547 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2548 p += 4;
2549
2550 if( end - p < 2 )
2551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2552 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2553 p += 2;
2554
2555 if( end - p < ( long int )session->ticket_len )
2556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2557 if( session->ticket_len > 0 )
2558 {
2559 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2560 if( session->ticket == NULL )
2561 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2562 memcpy( session->ticket, p, session->ticket_len );
2563 p += session->ticket_len;
2564 }
2565 }
2566#endif /* MBEDTLS_SSL_CLI_C */
2567
2568 return( 0 );
2569
2570}
2571#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002572MBEDTLS_CHECK_RETURN_CRITICAL
2573static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2574 unsigned char *buf,
2575 size_t buf_len,
2576 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002577{
2578 ((void) session);
2579 ((void) buf);
2580 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002581 *olen = 0;
2582 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002583}
Jerry Yu438ddd82022-07-07 06:55:50 +00002584
2585static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2586 unsigned char *buf,
2587 size_t buf_len )
2588{
2589 ((void) session);
2590 ((void) buf);
2591 ((void) buf_len);
2592 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2593}
2594#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002595#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2596
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002597psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002598 size_t taglen,
2599 psa_algorithm_t *alg,
2600 psa_key_type_t *key_type,
2601 size_t *key_size )
2602{
2603 switch ( mbedtls_cipher_type )
2604 {
2605 case MBEDTLS_CIPHER_AES_128_CBC:
2606 *alg = PSA_ALG_CBC_NO_PADDING;
2607 *key_type = PSA_KEY_TYPE_AES;
2608 *key_size = 128;
2609 break;
2610 case MBEDTLS_CIPHER_AES_128_CCM:
2611 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2612 *key_type = PSA_KEY_TYPE_AES;
2613 *key_size = 128;
2614 break;
2615 case MBEDTLS_CIPHER_AES_128_GCM:
2616 *alg = PSA_ALG_GCM;
2617 *key_type = PSA_KEY_TYPE_AES;
2618 *key_size = 128;
2619 break;
2620 case MBEDTLS_CIPHER_AES_192_CCM:
2621 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2622 *key_type = PSA_KEY_TYPE_AES;
2623 *key_size = 192;
2624 break;
2625 case MBEDTLS_CIPHER_AES_192_GCM:
2626 *alg = PSA_ALG_GCM;
2627 *key_type = PSA_KEY_TYPE_AES;
2628 *key_size = 192;
2629 break;
2630 case MBEDTLS_CIPHER_AES_256_CBC:
2631 *alg = PSA_ALG_CBC_NO_PADDING;
2632 *key_type = PSA_KEY_TYPE_AES;
2633 *key_size = 256;
2634 break;
2635 case MBEDTLS_CIPHER_AES_256_CCM:
2636 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2637 *key_type = PSA_KEY_TYPE_AES;
2638 *key_size = 256;
2639 break;
2640 case MBEDTLS_CIPHER_AES_256_GCM:
2641 *alg = PSA_ALG_GCM;
2642 *key_type = PSA_KEY_TYPE_AES;
2643 *key_size = 256;
2644 break;
2645 case MBEDTLS_CIPHER_ARIA_128_CBC:
2646 *alg = PSA_ALG_CBC_NO_PADDING;
2647 *key_type = PSA_KEY_TYPE_ARIA;
2648 *key_size = 128;
2649 break;
2650 case MBEDTLS_CIPHER_ARIA_128_CCM:
2651 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2652 *key_type = PSA_KEY_TYPE_ARIA;
2653 *key_size = 128;
2654 break;
2655 case MBEDTLS_CIPHER_ARIA_128_GCM:
2656 *alg = PSA_ALG_GCM;
2657 *key_type = PSA_KEY_TYPE_ARIA;
2658 *key_size = 128;
2659 break;
2660 case MBEDTLS_CIPHER_ARIA_192_CCM:
2661 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2662 *key_type = PSA_KEY_TYPE_ARIA;
2663 *key_size = 192;
2664 break;
2665 case MBEDTLS_CIPHER_ARIA_192_GCM:
2666 *alg = PSA_ALG_GCM;
2667 *key_type = PSA_KEY_TYPE_ARIA;
2668 *key_size = 192;
2669 break;
2670 case MBEDTLS_CIPHER_ARIA_256_CBC:
2671 *alg = PSA_ALG_CBC_NO_PADDING;
2672 *key_type = PSA_KEY_TYPE_ARIA;
2673 *key_size = 256;
2674 break;
2675 case MBEDTLS_CIPHER_ARIA_256_CCM:
2676 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2677 *key_type = PSA_KEY_TYPE_ARIA;
2678 *key_size = 256;
2679 break;
2680 case MBEDTLS_CIPHER_ARIA_256_GCM:
2681 *alg = PSA_ALG_GCM;
2682 *key_type = PSA_KEY_TYPE_ARIA;
2683 *key_size = 256;
2684 break;
2685 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2686 *alg = PSA_ALG_CBC_NO_PADDING;
2687 *key_type = PSA_KEY_TYPE_CAMELLIA;
2688 *key_size = 128;
2689 break;
2690 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2691 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2692 *key_type = PSA_KEY_TYPE_CAMELLIA;
2693 *key_size = 128;
2694 break;
2695 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2696 *alg = PSA_ALG_GCM;
2697 *key_type = PSA_KEY_TYPE_CAMELLIA;
2698 *key_size = 128;
2699 break;
2700 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2701 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2702 *key_type = PSA_KEY_TYPE_CAMELLIA;
2703 *key_size = 192;
2704 break;
2705 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2706 *alg = PSA_ALG_GCM;
2707 *key_type = PSA_KEY_TYPE_CAMELLIA;
2708 *key_size = 192;
2709 break;
2710 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2711 *alg = PSA_ALG_CBC_NO_PADDING;
2712 *key_type = PSA_KEY_TYPE_CAMELLIA;
2713 *key_size = 256;
2714 break;
2715 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2716 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2717 *key_type = PSA_KEY_TYPE_CAMELLIA;
2718 *key_size = 256;
2719 break;
2720 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2721 *alg = PSA_ALG_GCM;
2722 *key_type = PSA_KEY_TYPE_CAMELLIA;
2723 *key_size = 256;
2724 break;
2725 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2726 *alg = PSA_ALG_CHACHA20_POLY1305;
2727 *key_type = PSA_KEY_TYPE_CHACHA20;
2728 *key_size = 256;
2729 break;
2730 case MBEDTLS_CIPHER_NULL:
2731 *alg = MBEDTLS_SSL_NULL_CIPHER;
2732 *key_type = 0;
2733 *key_size = 0;
2734 break;
2735 default:
2736 return PSA_ERROR_NOT_SUPPORTED;
2737 }
2738
2739 return PSA_SUCCESS;
2740}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002741#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002742
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002743#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002744int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2745 const unsigned char *dhm_P, size_t P_len,
2746 const unsigned char *dhm_G, size_t G_len )
2747{
Janos Follath865b3eb2019-12-16 11:46:15 +00002748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002749
Glenn Strausscee11292021-12-20 01:43:17 -05002750 mbedtls_mpi_free( &conf->dhm_P );
2751 mbedtls_mpi_free( &conf->dhm_G );
2752
Hanno Beckera90658f2017-10-04 15:29:08 +01002753 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2754 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2755 {
2756 mbedtls_mpi_free( &conf->dhm_P );
2757 mbedtls_mpi_free( &conf->dhm_G );
2758 return( ret );
2759 }
2760
2761 return( 0 );
2762}
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002764int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002765{
Janos Follath865b3eb2019-12-16 11:46:15 +00002766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002767
Glenn Strausscee11292021-12-20 01:43:17 -05002768 mbedtls_mpi_free( &conf->dhm_P );
2769 mbedtls_mpi_free( &conf->dhm_G );
2770
Gilles Peskinee5702482021-06-11 21:59:08 +02002771 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2772 &conf->dhm_P ) ) != 0 ||
2773 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2774 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002775 {
2776 mbedtls_mpi_free( &conf->dhm_P );
2777 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002778 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002779 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002780
2781 return( 0 );
2782}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002783#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002784
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002785#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2786/*
2787 * Set the minimum length for Diffie-Hellman parameters
2788 */
2789void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2790 unsigned int bitlen )
2791{
2792 conf->dhm_min_bitlen = bitlen;
2793}
2794#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2795
Ronald Crone68ab4f2022-10-05 12:46:29 +02002796#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002797#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002798/*
2799 * Set allowed/preferred hashes for handshake signatures
2800 */
2801void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2802 const int *hashes )
2803{
2804 conf->sig_hashes = hashes;
2805}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002806#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002807
Jerry Yuf017ee42022-01-12 15:49:48 +08002808/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002809void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2810 const uint16_t* sig_algs )
2811{
Jerry Yuf017ee42022-01-12 15:49:48 +08002812#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2813 conf->sig_hashes = NULL;
2814#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2815 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002816}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002817#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002818
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002819#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002820#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002821/*
2822 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002823 *
2824 * mbedtls_ssl_setup() takes the provided list
2825 * and translates it to a list of IANA TLS group identifiers,
2826 * stored in ssl->handshake->group_list.
2827 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002828 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002829void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002830 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002831{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002832 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002833 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002834}
Brett Warrene0edc842021-08-17 09:53:13 +01002835#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002836#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002837
Brett Warrene0edc842021-08-17 09:53:13 +01002838/*
2839 * Set the allowed groups
2840 */
2841void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2842 const uint16_t *group_list )
2843{
2844#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2845 conf->curve_list = NULL;
2846#endif
2847 conf->group_list = group_list;
2848}
2849
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002850#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852{
Hanno Becker947194e2017-04-07 13:25:49 +01002853 /* Initialize to suppress unnecessary compiler warning */
2854 size_t hostname_len = 0;
2855
2856 /* Check if new hostname is valid before
2857 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002858 if( hostname != NULL )
2859 {
2860 hostname_len = strlen( hostname );
2861
2862 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2864 }
2865
2866 /* Now it's clear that we will overwrite the old hostname,
2867 * so we can free it safely */
2868
2869 if( ssl->hostname != NULL )
2870 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002871 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002872 mbedtls_free( ssl->hostname );
2873 }
2874
2875 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002876
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002878 {
2879 ssl->hostname = NULL;
2880 }
2881 else
2882 {
2883 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002884 if( ssl->hostname == NULL )
2885 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002886
Hanno Becker947194e2017-04-07 13:25:49 +01002887 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002888
Hanno Becker947194e2017-04-07 13:25:49 +01002889 ssl->hostname[hostname_len] = '\0';
2890 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
2892 return( 0 );
2893}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002894#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002896#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002897void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002899 const unsigned char *, size_t),
2900 void *p_sni )
2901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002902 conf->f_sni = f_sni;
2903 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002908int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002909{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002910 size_t cur_len, tot_len;
2911 const char **p;
2912
2913 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002914 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2915 * MUST NOT be truncated."
2916 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002917 */
2918 tot_len = 0;
2919 for( p = protos; *p != NULL; p++ )
2920 {
2921 cur_len = strlen( *p );
2922 tot_len += cur_len;
2923
Ronald Cron8216dd32020-04-23 16:41:44 +02002924 if( ( cur_len == 0 ) ||
2925 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2926 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002928 }
2929
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002930 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002931
2932 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002933}
2934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002936{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002937 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002940
Johan Pascalb62bb512015-12-03 21:56:45 +01002941#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002942void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2943 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002944{
2945 conf->dtls_srtp_mki_support = support_mki_value;
2946}
2947
Ron Eldoref72faf2018-07-12 11:54:20 +03002948int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2949 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002950 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002951{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002952 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002953 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002955 }
Ron Eldor591f1622018-01-22 12:30:04 +02002956
2957 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002958 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002959 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002960 }
Ron Eldor591f1622018-01-22 12:30:04 +02002961
2962 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2963 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002964 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002965}
2966
Ron Eldoref72faf2018-07-12 11:54:20 +03002967int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002968 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002969{
Johan Pascal253d0262020-09-22 13:04:45 +02002970 const mbedtls_ssl_srtp_profile *p;
2971 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002972
Johan Pascal253d0262020-09-22 13:04:45 +02002973 /* check the profiles list: all entry must be valid,
2974 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002975 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2976 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2977 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002978 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002979 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002980 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002981 list_size++;
2982 }
2983 else
2984 {
2985 /* unsupported value, stop parsing and set the size to an error value */
2986 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002987 }
2988 }
2989
Johan Pascal5ef72d22020-10-28 17:05:47 +01002990 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002991 {
Johan Pascal253d0262020-09-22 13:04:45 +02002992 conf->dtls_srtp_profile_list = NULL;
2993 conf->dtls_srtp_profile_list_len = 0;
2994 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2995 }
2996
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002997 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002998 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002999
3000 return( 0 );
3001}
3002
Johan Pascal5ef72d22020-10-28 17:05:47 +01003003void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
3004 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01003005{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003006 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3007 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01003008 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003009 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003010 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01003011 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01003012 else
3013 {
3014 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01003015 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3016 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01003017 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003018}
Johan Pascalb62bb512015-12-03 21:56:45 +01003019#endif /* MBEDTLS_SSL_DTLS_SRTP */
3020
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303021#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003022void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003023{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003024 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003025}
3026
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003027void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00003028{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003029 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003030}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303031#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003032
Janos Follath088ce432017-04-10 12:42:31 +01003033#if defined(MBEDTLS_SSL_SRV_C)
3034void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3035 char cert_req_ca_list )
3036{
3037 conf->cert_req_ca_list = cert_req_ca_list;
3038}
3039#endif
3040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003042void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003043{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003044 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003045}
3046#endif
3047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003049void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003051 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003052}
3053#endif
3054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003056int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003059 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003062 }
3063
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003064 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003065
3066 return( 0 );
3067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003069
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003070void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003071{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003072 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003073}
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003076void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003078 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003079}
3080
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003081void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003082{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003083 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003084}
3085
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003086void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003087 const unsigned char period[8] )
3088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003089 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003094#if defined(MBEDTLS_SSL_CLI_C)
3095void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003096{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003097 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003098}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003099#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003100
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003101#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003102
Jerry Yud0766ec2022-09-22 10:46:57 +08003103#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003104void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3105 uint16_t num_tickets )
3106{
Jerry Yud0766ec2022-09-22 10:46:57 +08003107 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003108}
3109#endif
3110
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003111void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3112 mbedtls_ssl_ticket_write_t *f_ticket_write,
3113 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3114 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003115{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003116 conf->f_ticket_write = f_ticket_write;
3117 conf->f_ticket_parse = f_ticket_parse;
3118 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003119}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003122
Hanno Becker7e6c1782021-06-08 09:24:55 +01003123void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3124 mbedtls_ssl_export_keys_t *f_export_keys,
3125 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003126{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003127 ssl->f_export_keys = f_export_keys;
3128 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003129}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003130
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003131#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003132void mbedtls_ssl_conf_async_private_cb(
3133 mbedtls_ssl_config *conf,
3134 mbedtls_ssl_async_sign_t *f_async_sign,
3135 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3136 mbedtls_ssl_async_resume_t *f_async_resume,
3137 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003138 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003139{
3140 conf->f_async_sign_start = f_async_sign;
3141 conf->f_async_decrypt_start = f_async_decrypt;
3142 conf->f_async_resume = f_async_resume;
3143 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003144 conf->p_async_config_data = async_config_data;
3145}
3146
Gilles Peskine8f97af72018-04-26 11:46:10 +02003147void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3148{
3149 return( conf->p_async_config_data );
3150}
3151
Gilles Peskine1febfef2018-04-30 11:54:39 +02003152void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003153{
3154 if( ssl->handshake == NULL )
3155 return( NULL );
3156 else
3157 return( ssl->handshake->user_async_ctx );
3158}
3159
Gilles Peskine1febfef2018-04-30 11:54:39 +02003160void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003161 void *ctx )
3162{
3163 if( ssl->handshake != NULL )
3164 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003165}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003166#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003167
Paul Bakker5121ce52009-01-03 21:22:43 +00003168/*
3169 * SSL get accessors
3170 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003171uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003172{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003173 if( ssl->session != NULL )
3174 return( ssl->session->verify_result );
3175
3176 if( ssl->session_negotiate != NULL )
3177 return( ssl->session_negotiate->verify_result );
3178
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003179 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003180}
3181
Glenn Strauss8f526902022-01-13 00:04:49 -05003182int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3183{
3184 if( ssl == NULL || ssl->session == NULL )
3185 return( 0 );
3186
3187 return( ssl->session->ciphersuite );
3188}
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003191{
Paul Bakker926c8e42013-03-06 10:23:34 +01003192 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003193 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003196}
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003202 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003203 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003204 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003205 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003206 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003207 default:
3208 return( "unknown (DTLS)" );
3209 }
3210 }
3211#endif
3212
Glenn Strauss60bfe602022-03-14 19:04:24 -04003213 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003214 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003215 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003217 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003218 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003219 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003220 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003221 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003222}
3223
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003224#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003225size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3226{
David Horstmann95d516f2021-05-04 18:36:56 +01003227 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003228 size_t read_mfl;
3229
Jerry Yuddda0502022-12-01 19:43:12 +08003230#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003231 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3232 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3233 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3234 {
3235 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3236 }
Jerry Yuddda0502022-12-01 19:43:12 +08003237#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003238
3239 /* Check if a smaller max length was negotiated */
3240 if( ssl->session_out != NULL )
3241 {
3242 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3243 if( read_mfl < max_len )
3244 {
3245 max_len = read_mfl;
3246 }
3247 }
3248
Jerry Yuddda0502022-12-01 19:43:12 +08003249 /* During a handshake, use the value being negotiated */
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003250 if( ssl->session_negotiate != NULL )
3251 {
3252 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3253 if( read_mfl < max_len )
3254 {
3255 max_len = read_mfl;
3256 }
3257 }
3258
3259 return( max_len );
3260}
3261
3262size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003263{
3264 size_t max_len;
3265
3266 /*
3267 * Assume mfl_code is correct since it was checked when set
3268 */
Angus Grattond8213d02016-05-25 20:56:48 +10003269 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003270
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003271 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003272 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003273 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003274 {
Angus Grattond8213d02016-05-25 20:56:48 +10003275 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003276 }
3277
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003278 /* During a handshake, use the value being negotiated */
3279 if( ssl->session_negotiate != NULL &&
3280 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3281 {
3282 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3283 }
3284
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003285 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003286}
3287#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3288
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003290size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003291{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003292 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3294 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3295 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3296 return ( 0 );
3297
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003298 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3299 return( ssl->mtu );
3300
3301 if( ssl->mtu == 0 )
3302 return( ssl->handshake->mtu );
3303
3304 return( ssl->mtu < ssl->handshake->mtu ?
3305 ssl->mtu : ssl->handshake->mtu );
3306}
3307#endif /* MBEDTLS_SSL_PROTO_DTLS */
3308
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003309int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3310{
3311 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3312
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003313#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3314 !defined(MBEDTLS_SSL_PROTO_DTLS)
3315 (void) ssl;
3316#endif
3317
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003318#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003319 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320
3321 if( max_len > mfl )
3322 max_len = mfl;
3323#endif
3324
3325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003326 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003327 {
Hanno Becker89490712020-02-05 10:50:12 +00003328 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003329 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3330 const size_t overhead = (size_t) ret;
3331
3332 if( ret < 0 )
3333 return( ret );
3334
3335 if( mtu <= overhead )
3336 {
3337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3338 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3339 }
3340
3341 if( max_len > mtu - overhead )
3342 max_len = mtu - overhead;
3343 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003344#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003345
Hanno Becker0defedb2018-08-10 12:35:02 +01003346#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3347 !defined(MBEDTLS_SSL_PROTO_DTLS)
3348 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003349#endif
3350
3351 return( (int) max_len );
3352}
3353
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003354int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3355{
3356 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3357
3358#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3359 (void) ssl;
3360#endif
3361
3362#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3363 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3364
3365 if( max_len > mfl )
3366 max_len = mfl;
3367#endif
3368
3369 return( (int) max_len );
3370}
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_X509_CRT_PARSE_C)
3373const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003374{
3375 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003376 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003377
Hanno Beckere6824572019-02-07 13:18:46 +00003378#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003379 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003380#else
3381 return( NULL );
3382#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003387int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3388 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003389{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003390 int ret;
3391
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003392 if( ssl == NULL ||
3393 dst == NULL ||
3394 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003398 }
3399
Hanno Beckere810bbc2021-05-14 16:01:05 +01003400 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3401 * idempotent: Each session can only be exported once.
3402 *
3403 * (This is in preparation for TLS 1.3 support where we will
3404 * need the ability to export multiple sessions (aka tickets),
3405 * which will be achieved by calling mbedtls_ssl_get_session()
3406 * multiple times until it fails.)
3407 *
3408 * Check whether we have already exported the current session,
3409 * and fail if so.
3410 */
3411 if( ssl->session->exported == 1 )
3412 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3413
3414 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3415 if( ret != 0 )
3416 return( ret );
3417
3418 /* Remember that we've exported the session. */
3419 ssl->session->exported = 1;
3420 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003423
Paul Bakker5121ce52009-01-03 21:22:43 +00003424/*
Hanno Beckera835da52019-05-16 12:39:07 +01003425 * Define ticket header determining Mbed TLS version
3426 * and structure of the ticket.
3427 */
3428
Hanno Becker94ef3b32019-05-16 12:50:45 +01003429/*
Hanno Becker50b59662019-05-28 14:30:45 +01003430 * Define bitflag determining compile-time settings influencing
3431 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003432 */
3433
Hanno Becker50b59662019-05-28 14:30:45 +01003434#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003435#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003436#else
Hanno Becker3e088662019-05-29 11:10:18 +01003437#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003438#endif /* MBEDTLS_HAVE_TIME */
3439
3440#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003441#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003442#else
Hanno Becker3e088662019-05-29 11:10:18 +01003443#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444#endif /* MBEDTLS_X509_CRT_PARSE_C */
3445
3446#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003447#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003448#else
Hanno Becker3e088662019-05-29 11:10:18 +01003449#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003450#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3451
3452#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003453#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003454#else
Hanno Becker3e088662019-05-29 11:10:18 +01003455#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003456#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3457
Hanno Becker94ef3b32019-05-16 12:50:45 +01003458#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003459#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003460#else
Hanno Becker3e088662019-05-29 11:10:18 +01003461#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003462#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3463
Hanno Becker94ef3b32019-05-16 12:50:45 +01003464#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3465#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3466#else
3467#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3468#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3469
Hanno Becker3e088662019-05-29 11:10:18 +01003470#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3471#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3472#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3473#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003474#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3475#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003476
Hanno Becker50b59662019-05-28 14:30:45 +01003477#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003478 ( (uint16_t) ( \
3479 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3480 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3481 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3482 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003483 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003484 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003485
Hanno Beckerf8787072019-05-16 12:41:07 +01003486static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003487 MBEDTLS_VERSION_MAJOR,
3488 MBEDTLS_VERSION_MINOR,
3489 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003490 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3491 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003492};
Hanno Beckera835da52019-05-16 12:39:07 +01003493
3494/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003495 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003496 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003497 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003498 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003499 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003500 * opaque mbedtls_version[3]; // library version: major, minor, patch
3501 * opaque session_format[2]; // library-version specific 16-bit field
3502 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003504 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003505 * Note: When updating the format, remember to keep
3506 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003507 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003508 * // In this version, `session_format` determines
3509 * // the setting of those compile-time
3510 * // configuration options which influence
3511 * // the structure of mbedtls_ssl_session.
3512 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003513 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003514 * // - TLS 1.2 (0x0303)
3515 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003516 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003517 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003518 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003519 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003520 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003521 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003522 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003523 *
3524 * };
3525 *
3526 * } serialized_session;
3527 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003528 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003529
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003530MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003531static int ssl_session_save( const mbedtls_ssl_session *session,
3532 unsigned char omit_header,
3533 unsigned char *buf,
3534 size_t buf_len,
3535 size_t *olen )
3536{
3537 unsigned char *p = buf;
3538 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003539 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003540#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3541 size_t out_len;
3542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3543#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003544 if( session == NULL )
3545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3546
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003547 if( !omit_header )
3548 {
3549 /*
3550 * Add Mbed TLS version identifier
3551 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003552 used += sizeof( ssl_serialized_session_header );
3553
3554 if( used <= buf_len )
3555 {
3556 memcpy( p, ssl_serialized_session_header,
3557 sizeof( ssl_serialized_session_header ) );
3558 p += sizeof( ssl_serialized_session_header );
3559 }
3560 }
3561
3562 /*
3563 * TLS version identifier
3564 */
3565 used += 1;
3566 if( used <= buf_len )
3567 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003568 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003569 }
3570
3571 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003572 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003573 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003574 {
3575#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003576 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003577 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003578 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3580
Jerry Yu251a12e2022-07-13 15:15:48 +08003581#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3582 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003583 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3584 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003585 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003586 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003587 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003588#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3589
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003590 default:
3591 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3592 }
3593
3594 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003595 if( used > buf_len )
3596 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003597
3598 return( 0 );
3599}
3600
3601/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003602 * Public wrapper for ssl_session_save()
3603 */
3604int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3605 unsigned char *buf,
3606 size_t buf_len,
3607 size_t *olen )
3608{
3609 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3610}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003611
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003612/*
3613 * Deserialize session, see mbedtls_ssl_session_save() for format.
3614 *
3615 * This internal version is wrapped by a public function that cleans up in
3616 * case of error, and has an extra option omit_header.
3617 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003618MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003619static int ssl_session_load( mbedtls_ssl_session *session,
3620 unsigned char omit_header,
3621 const unsigned char *buf,
3622 size_t len )
3623{
3624 const unsigned char *p = buf;
3625 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003626 size_t remaining_len;
3627
3628
3629 if( session == NULL )
3630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003631
3632 if( !omit_header )
3633 {
3634 /*
3635 * Check Mbed TLS version identifier
3636 */
3637
3638 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3640
3641 if( memcmp( p, ssl_serialized_session_header,
3642 sizeof( ssl_serialized_session_header ) ) != 0 )
3643 {
3644 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3645 }
3646 p += sizeof( ssl_serialized_session_header );
3647 }
3648
3649 /*
3650 * TLS version identifier
3651 */
3652 if( 1 > (size_t)( end - p ) )
3653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003654 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003655
3656 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003657 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003658 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003659 {
3660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003661 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003662 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3664
Jerry Yu438ddd82022-07-07 06:55:50 +00003665#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3666 case MBEDTLS_SSL_VERSION_TLS1_3:
3667 return( ssl_tls13_session_load( session, p, remaining_len ) );
3668#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3669
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003670 default:
3671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3672 }
3673}
3674
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003675/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003676 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003677 */
3678int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3679 const unsigned char *buf,
3680 size_t len )
3681{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003682 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003683
3684 if( ret != 0 )
3685 mbedtls_ssl_session_free( session );
3686
3687 return( ret );
3688}
3689
3690/*
Paul Bakker1961b702013-01-25 14:49:24 +01003691 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003693MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003694static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3695{
3696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3697
Ronald Cron66dbf912022-02-02 15:33:46 +01003698 /*
3699 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003700 * that were written into the output buffer by the previous handshake step,
3701 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003702 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3703 * We proceed to the next handshake step only when all data from the
3704 * previous one have been sent to the peer, thus we make sure that this is
3705 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3706 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3707 * we have to wait before to go ahead.
3708 * In the case of TLS 1.3, handshake step handlers do not send data to the
3709 * peer. Data are only sent here and through
3710 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003711 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003712 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003713 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3714 return( ret );
3715
3716#if defined(MBEDTLS_SSL_PROTO_DTLS)
3717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3718 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3719 {
3720 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3721 return( ret );
3722 }
3723#endif /* MBEDTLS_SSL_PROTO_DTLS */
3724
3725 return( ret );
3726}
3727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003729{
Hanno Becker41934dd2021-08-07 19:13:43 +01003730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Hanno Becker41934dd2021-08-07 19:13:43 +01003732 if( ssl == NULL ||
3733 ssl->conf == NULL ||
3734 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003735 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003736 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003738 }
3739
3740 ret = ssl_prepare_handshake_step( ssl );
3741 if( ret != 0 )
3742 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003743
Jerry Yue7047812021-09-13 19:26:39 +08003744 ret = mbedtls_ssl_handle_pending_alert( ssl );
3745 if( ret != 0 )
3746 goto cleanup;
3747
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003748 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3749 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3750 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003753 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003754 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3756 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003757
Ronald Cron9f0fba32022-02-10 16:45:15 +01003758 switch( ssl->state )
3759 {
3760 case MBEDTLS_SSL_HELLO_REQUEST:
3761 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003762 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003763 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003764
Ronald Cron9f0fba32022-02-10 16:45:15 +01003765 case MBEDTLS_SSL_CLIENT_HELLO:
3766 ret = mbedtls_ssl_write_client_hello( ssl );
3767 break;
3768
3769 default:
3770#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003771 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003772 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3773 else
3774 ret = mbedtls_ssl_handshake_client_step( ssl );
3775#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3776 ret = mbedtls_ssl_handshake_client_step( ssl );
3777#else
3778 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3779#endif
3780 }
Jerry Yub9930e72021-08-06 17:11:51 +08003781 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003784 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003785 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003786#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003787 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003788 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003790
3791#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3792 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3793 ret = mbedtls_ssl_handshake_server_step( ssl );
3794#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003796#endif
3797
Jerry Yue7047812021-09-13 19:26:39 +08003798 if( ret != 0 )
3799 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003800 /* handshake_step return error. And it is same
3801 * with alert_reason.
3802 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003803 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003804 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003805 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003806 goto cleanup;
3807 }
3808 }
3809
3810cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003811 return( ret );
3812}
3813
3814/*
3815 * Perform the SSL handshake
3816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003818{
3819 int ret = 0;
3820
Hanno Beckera817ea42020-10-20 15:20:23 +01003821 /* Sanity checks */
3822
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003823 if( ssl == NULL || ssl->conf == NULL )
3824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3825
Hanno Beckera817ea42020-10-20 15:20:23 +01003826#if defined(MBEDTLS_SSL_PROTO_DTLS)
3827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3828 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3829 {
3830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3831 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3833 }
3834#endif /* MBEDTLS_SSL_PROTO_DTLS */
3835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003837
Hanno Beckera817ea42020-10-20 15:20:23 +01003838 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003839 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003842
3843 if( ret != 0 )
3844 break;
3845 }
3846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
3849 return( ret );
3850}
3851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_SSL_RENEGOTIATION)
3853#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003854/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003855 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003856 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003857MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003859{
Janos Follath865b3eb2019-12-16 11:46:15 +00003860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003863
3864 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3866 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003867
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003868 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003869 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871 return( ret );
3872 }
3873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003875
3876 return( 0 );
3877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003879
3880/*
3881 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 * - any side: calling mbedtls_ssl_renegotiate(),
3883 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3884 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003885 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003886 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003888 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003889int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003890{
Janos Follath865b3eb2019-12-16 11:46:15 +00003891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003894
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003895 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3896 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003897
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003898 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3899 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003901 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003903 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003904 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003905 ssl->handshake->out_msg_seq = 1;
3906 else
3907 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003908 }
3909#endif
3910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3912 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003917 return( ret );
3918 }
3919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003921
3922 return( 0 );
3923}
3924
3925/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003926 * Renegotiate current connection on client,
3927 * or request renegotiation on server
3928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003929int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003930{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003932
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003933 if( ssl == NULL || ssl->conf == NULL )
3934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003937 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003939 {
Jerry Yu6848a612022-10-27 13:03:26 +08003940 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003944
3945 /* Did we already try/start sending HelloRequest? */
3946 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003948
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003950 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003954 /*
3955 * On client, either start the renegotiation process or,
3956 * if already in progress, continue the handshake
3957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003959 {
Jerry Yu6848a612022-10-27 13:03:26 +08003960 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003962
Hanno Becker40cdaa12020-02-05 10:48:27 +00003963 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003964 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003966 return( ret );
3967 }
3968 }
3969 else
3970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003974 return( ret );
3975 }
3976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003978
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003979 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982
Gilles Peskine9b562d52018-04-25 20:32:43 +02003983void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003984{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003985 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3986
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003987 if( handshake == NULL )
3988 return;
3989
Brett Warrene0edc842021-08-17 09:53:13 +01003990#if defined(MBEDTLS_ECP_C)
3991#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3992 if ( ssl->handshake->group_list_heap_allocated )
3993 mbedtls_free( (void*) handshake->group_list );
3994 handshake->group_list = NULL;
3995#endif /* MBEDTLS_DEPRECATED_REMOVED */
3996#endif /* MBEDTLS_ECP_C */
3997
Ronald Crone68ab4f2022-10-05 12:46:29 +02003998#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003999#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4000 if ( ssl->handshake->sig_algs_heap_allocated )
4001 mbedtls_free( (void*) handshake->sig_algs );
4002 handshake->sig_algs = NULL;
4003#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004004#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4005 if( ssl->handshake->certificate_request_context )
4006 {
4007 mbedtls_free( (void*) handshake->certificate_request_context );
4008 }
4009#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004010#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004011
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004012#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
4013 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
4014 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02004015 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004016 handshake->async_in_progress = 0;
4017 }
4018#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4019
Andrzej Kurek25f27152022-08-17 16:09:31 -04004020#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004021#if defined(MBEDTLS_USE_PSA_CRYPTO)
4022 psa_hash_abort( &handshake->fin_sha256_psa );
4023#else
Jerry Yu2e199812022-12-01 18:57:19 +08004024 mbedtls_sha256_free( &handshake->fin_sha256 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004025#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004026#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004027#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05004029 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004030#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004031 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004032#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004033#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035#if defined(MBEDTLS_DHM_C)
4036 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004037#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004038#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004040#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004041
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004042#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004043#if defined(MBEDTLS_USE_PSA_CRYPTO)
4044 psa_pake_abort( &handshake->psa_pake_ctx );
Valerio Settieb3f7882022-12-08 18:42:58 +01004045 /*
4046 * Opaque keys are not stored in the handshake's data and it's the user
4047 * responsibility to destroy them. Clear ones, instead, are created by
4048 * the TLS library and should be destroyed at the same level
4049 */
4050 if( ! mbedtls_svc_key_id_is_null( handshake->psa_pake_password ) )
4051 {
4052 psa_destroy_key( handshake->psa_pake_password );
4053 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004054 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4055#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004056 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004057#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004058#if defined(MBEDTLS_SSL_CLI_C)
4059 mbedtls_free( handshake->ecjpake_cache );
4060 handshake->ecjpake_cache = NULL;
4061 handshake->ecjpake_cache_len = 0;
4062#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004063#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004064
Janos Follath4ae5c292016-02-10 11:27:43 +00004065#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4066 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004067 /* explicit void pointer cast for buggy MS compiler */
Valerio Setti18c9fed2022-12-30 17:44:24 +01004068 mbedtls_free( (void *) handshake->curves_tls_id );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004069#endif
4070
Ronald Cron73fe8df2022-10-05 14:31:43 +02004071#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004072#if defined(MBEDTLS_USE_PSA_CRYPTO)
4073 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4074 {
4075 /* The maintenance of the external PSK key slot is the
4076 * user's responsibility. */
4077 if( ssl->handshake->psk_opaque_is_internal )
4078 {
4079 psa_destroy_key( ssl->handshake->psk_opaque );
4080 ssl->handshake->psk_opaque_is_internal = 0;
4081 }
4082 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4083 }
Neil Armstronge952a302022-05-03 10:22:14 +02004084#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004085 if( handshake->psk != NULL )
4086 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004087 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004088 mbedtls_free( handshake->psk );
4089 }
Neil Armstronge952a302022-05-03 10:22:14 +02004090#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004091#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4094 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004095 /*
4096 * Free only the linked list wrapper, not the keys themselves
4097 * since the belong to the SNI callback
4098 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004099 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004101
Gilles Peskineeccd8882020-03-10 12:19:08 +01004102#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004103 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004104 if( handshake->ecrs_peer_cert != NULL )
4105 {
4106 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4107 mbedtls_free( handshake->ecrs_peer_cert );
4108 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004109#endif
4110
Hanno Becker75173122019-02-06 16:18:31 +00004111#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4112 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4113 mbedtls_pk_free( &handshake->peer_pubkey );
4114#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4115
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004116#if defined(MBEDTLS_SSL_CLI_C) && \
4117 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4118 mbedtls_free( handshake->cookie );
4119#endif /* MBEDTLS_SSL_CLI_C &&
4120 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004121
4122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004123 mbedtls_ssl_flight_free( handshake->flight );
4124 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004126
Ronald Cronf12b81d2022-03-15 10:42:41 +01004127#if defined(MBEDTLS_ECDH_C) && \
4128 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004129 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004130 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004131#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4132
Ronald Cron6f135e12021-12-08 16:57:54 +01004133#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004134 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004135 mbedtls_free( handshake->transform_handshake );
4136#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004137 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004138 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004139#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004140#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004141
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004142
4143#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4144 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4145 * processes datagrams and the fact that a datagram is allowed to have
4146 * several records in it, it is possible that the I/O buffers are not
4147 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004148 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4149 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004150#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004151
Jerry Yuba9c7272021-10-30 11:54:10 +08004152 /* mbedtls_platform_zeroize MUST be last one in this function */
4153 mbedtls_platform_zeroize( handshake,
4154 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004155}
4156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004158{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004159 if( session == NULL )
4160 return;
4161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004163 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004164#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004165
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004166#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004167#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4168 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004169 mbedtls_free( session->hostname );
4170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004172#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004174 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004175}
4176
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004177#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004178
4179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4180#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4181#else
4182#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4183#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4184
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004185#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004186
4187#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4188#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4189#else
4190#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4191#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4192
4193#if defined(MBEDTLS_SSL_ALPN)
4194#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4195#else
4196#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4197#endif /* MBEDTLS_SSL_ALPN */
4198
4199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4200#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4202#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4203
4204#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4205 ( (uint32_t) ( \
4206 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4207 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4208 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4209 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4210 0u ) )
4211
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004212static unsigned char ssl_serialized_context_header[] = {
4213 MBEDTLS_VERSION_MAJOR,
4214 MBEDTLS_VERSION_MINOR,
4215 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004216 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4217 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4218 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4219 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4220 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004221};
4222
Paul Bakker5121ce52009-01-03 21:22:43 +00004223/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004224 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004225 *
4226 * The format of the serialized data is:
4227 * (in the presentation language of TLS, RFC 8446 section 3)
4228 *
4229 * // header
4230 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004231 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004232 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004233 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004234 * Note: When updating the format, remember to keep these
4235 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004236 *
4237 * // session sub-structure
4238 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4239 * // transform sub-structure
4240 * uint8 random[64]; // ServerHello.random+ClientHello.random
4241 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4242 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4243 * // fields from ssl_context
4244 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4245 * uint64 in_window_top; // DTLS: last validated record seq_num
4246 * uint64 in_window; // DTLS: bitmask for replay protection
4247 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4248 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4249 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4250 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4251 *
4252 * Note that many fields of the ssl_context or sub-structures are not
4253 * serialized, as they fall in one of the following categories:
4254 *
4255 * 1. forced value (eg in_left must be 0)
4256 * 2. pointer to dynamically-allocated memory (eg session, transform)
4257 * 3. value can be re-derived from other data (eg session keys from MS)
4258 * 4. value was temporary (eg content of input buffer)
4259 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004260 */
4261int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4262 unsigned char *buf,
4263 size_t buf_len,
4264 size_t *olen )
4265{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004266 unsigned char *p = buf;
4267 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004268 size_t session_len;
4269 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004270
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004271 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004272 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4273 * this function's documentation.
4274 *
4275 * These are due to assumptions/limitations in the implementation. Some of
4276 * them are likely to stay (no handshake in progress) some might go away
4277 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004278 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004279 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004280 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004281 {
4282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004284 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004285 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004286 {
4287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004289 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004290 /* Double-check that sub-structures are indeed ready */
4291 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004292 {
4293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004295 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004296 /* There must be no pending incoming or outgoing data */
4297 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 {
4299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004301 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004302 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004306 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004307 /* Protocol must be DTLS, not TLS */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004308 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004309 {
4310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004313 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004314 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004315 {
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004318 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004319 /* We must be using an AEAD ciphersuite */
4320 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004321 {
4322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004324 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004325 /* Renegotiation must not be enabled */
4326#if defined(MBEDTLS_SSL_RENEGOTIATION)
4327 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004328 {
4329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004331 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004332#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004333
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004334 /*
4335 * Version and format identifier
4336 */
4337 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004338
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004339 if( used <= buf_len )
4340 {
4341 memcpy( p, ssl_serialized_context_header,
4342 sizeof( ssl_serialized_context_header ) );
4343 p += sizeof( ssl_serialized_context_header );
4344 }
4345
4346 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004347 * Session (length + data)
4348 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004349 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004350 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4351 return( ret );
4352
4353 used += 4 + session_len;
4354 if( used <= buf_len )
4355 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004356 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4357 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004358
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004359 ret = ssl_session_save( ssl->session, 1,
4360 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004361 if( ret != 0 )
4362 return( ret );
4363
4364 p += session_len;
4365 }
4366
4367 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004368 * Transform
4369 */
4370 used += sizeof( ssl->transform->randbytes );
4371 if( used <= buf_len )
4372 {
4373 memcpy( p, ssl->transform->randbytes,
4374 sizeof( ssl->transform->randbytes ) );
4375 p += sizeof( ssl->transform->randbytes );
4376 }
4377
4378#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4379 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4380 if( used <= buf_len )
4381 {
4382 *p++ = ssl->transform->in_cid_len;
4383 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4384 p += ssl->transform->in_cid_len;
4385
4386 *p++ = ssl->transform->out_cid_len;
4387 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4388 p += ssl->transform->out_cid_len;
4389 }
4390#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4391
4392 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004393 * Saved fields from top-level ssl_context structure
4394 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004395 used += 4;
4396 if( used <= buf_len )
4397 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004398 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4399 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004400 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004401
4402#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4403 used += 16;
4404 if( used <= buf_len )
4405 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004406 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4407 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004408
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004409 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4410 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004411 }
4412#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4413
4414#if defined(MBEDTLS_SSL_PROTO_DTLS)
4415 used += 1;
4416 if( used <= buf_len )
4417 {
4418 *p++ = ssl->disable_datagram_packing;
4419 }
4420#endif /* MBEDTLS_SSL_PROTO_DTLS */
4421
Jerry Yuae0b2e22021-10-08 15:21:19 +08004422 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004423 if( used <= buf_len )
4424 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004425 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4426 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004427 }
4428
4429#if defined(MBEDTLS_SSL_PROTO_DTLS)
4430 used += 2;
4431 if( used <= buf_len )
4432 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004433 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4434 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004435 }
4436#endif /* MBEDTLS_SSL_PROTO_DTLS */
4437
4438#if defined(MBEDTLS_SSL_ALPN)
4439 {
4440 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004441 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004442 : 0;
4443
4444 used += 1 + alpn_len;
4445 if( used <= buf_len )
4446 {
4447 *p++ = alpn_len;
4448
4449 if( ssl->alpn_chosen != NULL )
4450 {
4451 memcpy( p, ssl->alpn_chosen, alpn_len );
4452 p += alpn_len;
4453 }
4454 }
4455 }
4456#endif /* MBEDTLS_SSL_ALPN */
4457
4458 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004459 * Done
4460 */
4461 *olen = used;
4462
4463 if( used > buf_len )
4464 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004465
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004466 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4467
Hanno Becker43aefe22020-02-05 10:44:56 +00004468 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004469}
4470
4471/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004472 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004473 *
4474 * This internal version is wrapped by a public function that cleans up in
4475 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004476 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004477MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004478static int ssl_context_load( mbedtls_ssl_context *ssl,
4479 const unsigned char *buf,
4480 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004481{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004482 const unsigned char *p = buf;
4483 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004484 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004487 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004488#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004489
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004490 /*
4491 * The context should have been freshly setup or reset.
4492 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004493 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004494 * renegotiating, or if the user mistakenly loaded a session first.)
4495 */
4496 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4497 ssl->session != NULL )
4498 {
4499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4500 }
4501
4502 /*
4503 * We can't check that the config matches the initial one, but we can at
4504 * least check it matches the requirements for serializing.
4505 */
4506 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004507 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4508 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004509#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004510 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004511#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004512 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004513 {
4514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4515 }
4516
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004517 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4518
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004519 /*
4520 * Check version identifier
4521 */
4522 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4524
4525 if( memcmp( p, ssl_serialized_context_header,
4526 sizeof( ssl_serialized_context_header ) ) != 0 )
4527 {
4528 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4529 }
4530 p += sizeof( ssl_serialized_context_header );
4531
4532 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533 * Session
4534 */
4535 if( (size_t)( end - p ) < 4 )
4536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4537
4538 session_len = ( (size_t) p[0] << 24 ) |
4539 ( (size_t) p[1] << 16 ) |
4540 ( (size_t) p[2] << 8 ) |
4541 ( (size_t) p[3] );
4542 p += 4;
4543
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004544 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004545 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004546 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004547 ssl->session_in = ssl->session;
4548 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004549 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004550
4551 if( (size_t)( end - p ) < session_len )
4552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4553
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004554 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004555 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004556 {
4557 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004558 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004559 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004560
4561 p += session_len;
4562
4563 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004564 * Transform
4565 */
4566
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004567 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004568 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004570 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004571 ssl->transform_in = ssl->transform;
4572 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004573 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004574#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004575
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004577 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4578 if( prf_func == NULL )
4579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4580
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004581 /* Read random bytes and populate structure */
4582 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004584
Hanno Beckerbd257552021-03-22 06:59:27 +00004585 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004586 ssl->session->ciphersuite,
4587 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004588#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004589 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004590#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004591 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004592 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004593 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004594 ssl->conf->endpoint,
4595 ssl );
4596 if( ret != 0 )
4597 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004598#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004599 p += sizeof( ssl->transform->randbytes );
4600
4601#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4602 /* Read connection IDs and store them */
4603 if( (size_t)( end - p ) < 1 )
4604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4605
4606 ssl->transform->in_cid_len = *p++;
4607
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004608 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4610
4611 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4612 p += ssl->transform->in_cid_len;
4613
4614 ssl->transform->out_cid_len = *p++;
4615
4616 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4618
4619 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4620 p += ssl->transform->out_cid_len;
4621#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4622
4623 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004624 * Saved fields from top-level ssl_context structure
4625 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004626 if( (size_t)( end - p ) < 4 )
4627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4628
4629 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4630 ( (uint32_t) p[1] << 16 ) |
4631 ( (uint32_t) p[2] << 8 ) |
4632 ( (uint32_t) p[3] );
4633 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004634
4635#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4636 if( (size_t)( end - p ) < 16 )
4637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4638
4639 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4640 ( (uint64_t) p[1] << 48 ) |
4641 ( (uint64_t) p[2] << 40 ) |
4642 ( (uint64_t) p[3] << 32 ) |
4643 ( (uint64_t) p[4] << 24 ) |
4644 ( (uint64_t) p[5] << 16 ) |
4645 ( (uint64_t) p[6] << 8 ) |
4646 ( (uint64_t) p[7] );
4647 p += 8;
4648
4649 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4650 ( (uint64_t) p[1] << 48 ) |
4651 ( (uint64_t) p[2] << 40 ) |
4652 ( (uint64_t) p[3] << 32 ) |
4653 ( (uint64_t) p[4] << 24 ) |
4654 ( (uint64_t) p[5] << 16 ) |
4655 ( (uint64_t) p[6] << 8 ) |
4656 ( (uint64_t) p[7] );
4657 p += 8;
4658#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4659
4660#if defined(MBEDTLS_SSL_PROTO_DTLS)
4661 if( (size_t)( end - p ) < 1 )
4662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4663
4664 ssl->disable_datagram_packing = *p++;
4665#endif /* MBEDTLS_SSL_PROTO_DTLS */
4666
Jerry Yud9a94fe2021-09-28 18:58:59 +08004667 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004669 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4670 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004671
4672#if defined(MBEDTLS_SSL_PROTO_DTLS)
4673 if( (size_t)( end - p ) < 2 )
4674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4675
4676 ssl->mtu = ( p[0] << 8 ) | p[1];
4677 p += 2;
4678#endif /* MBEDTLS_SSL_PROTO_DTLS */
4679
4680#if defined(MBEDTLS_SSL_ALPN)
4681 {
4682 uint8_t alpn_len;
4683 const char **cur;
4684
4685 if( (size_t)( end - p ) < 1 )
4686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4687
4688 alpn_len = *p++;
4689
4690 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4691 {
4692 /* alpn_chosen should point to an item in the configured list */
4693 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4694 {
4695 if( strlen( *cur ) == alpn_len &&
4696 memcmp( p, cur, alpn_len ) == 0 )
4697 {
4698 ssl->alpn_chosen = *cur;
4699 break;
4700 }
4701 }
4702 }
4703
4704 /* can only happen on conf mismatch */
4705 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4707
4708 p += alpn_len;
4709 }
4710#endif /* MBEDTLS_SSL_ALPN */
4711
4712 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004713 * Forced fields from top-level ssl_context structure
4714 *
4715 * Most of them already set to the correct value by mbedtls_ssl_init() and
4716 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4717 */
4718 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004719 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004720
Hanno Becker361b10d2019-08-30 10:42:49 +01004721 /* Adjust pointers for header fields of outgoing records to
4722 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004723 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004724
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004725#if defined(MBEDTLS_SSL_PROTO_DTLS)
4726 ssl->in_epoch = 1;
4727#endif
4728
4729 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4730 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004731 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4732 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004733 if( ssl->handshake != NULL )
4734 {
4735 mbedtls_ssl_handshake_free( ssl );
4736 mbedtls_free( ssl->handshake );
4737 ssl->handshake = NULL;
4738 }
4739
4740 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004741 * Done - should have consumed entire buffer
4742 */
4743 if( p != end )
4744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004745
4746 return( 0 );
4747}
4748
4749/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004750 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004751 */
4752int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4753 const unsigned char *buf,
4754 size_t len )
4755{
4756 int ret = ssl_context_load( context, buf, len );
4757
4758 if( ret != 0 )
4759 mbedtls_ssl_free( context );
4760
4761 return( ret );
4762}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004763#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004764
4765/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 * Free an SSL context
4767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004768void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004769{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770 if( ssl == NULL )
4771 return;
4772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004774
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004775 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 {
sander-visserb8aa2072020-05-06 22:05:13 +02004777#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4778 size_t out_buf_len = ssl->out_buf_len;
4779#else
4780 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4781#endif
4782
Darryl Greenb33cc762019-11-28 14:29:44 +00004783 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004785 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 }
4787
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004788 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 {
sander-visserb8aa2072020-05-06 22:05:13 +02004790#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4791 size_t in_buf_len = ssl->in_buf_len;
4792#else
4793 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4794#endif
4795
Darryl Greenb33cc762019-11-28 14:29:44 +00004796 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004798 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 }
4800
Paul Bakker48916f92012-09-16 19:57:18 +00004801 if( ssl->transform )
4802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 mbedtls_ssl_transform_free( ssl->transform );
4804 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004805 }
4806
4807 if( ssl->handshake )
4808 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004809 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 mbedtls_free( ssl->handshake );
Jerry Yu2e199812022-12-01 18:57:19 +08004811
4812#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4813 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 mbedtls_free( ssl->transform_negotiate );
Jerry Yu2e199812022-12-01 18:57:19 +08004815#endif
4816
4817 mbedtls_ssl_session_free( ssl->session_negotiate );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004819 }
4820
Ronald Cron6f135e12021-12-08 16:57:54 +01004821#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004822 mbedtls_ssl_transform_free( ssl->transform_application );
4823 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004824#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004825
Paul Bakkerc0463502013-02-14 11:19:38 +01004826 if( ssl->session )
4827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828 mbedtls_ssl_session_free( ssl->session );
4829 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004830 }
4831
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004832#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004833 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004835 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004840#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004842#endif
4843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004845
Paul Bakker86f04f42013-02-14 11:20:09 +01004846 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004847 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848}
4849
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004850/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004851 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004852 */
4853void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4854{
4855 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4856}
4857
Gilles Peskineae270bf2021-06-02 00:05:29 +02004858/* The selection should be the same as mbedtls_x509_crt_profile_default in
4859 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004860 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004861 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004862 * about this list.
4863 */
Brett Warrene0edc842021-08-17 09:53:13 +01004864static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004865#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004866 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004867#endif
4868#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004869 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004870#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004871#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004872 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004873#endif
4874#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004875 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004876#endif
4877#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004878 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004879#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004880#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004881 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004882#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004883#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004884 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004885#endif
4886#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004887 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004888#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004889 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004890};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004891
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004892static int ssl_preset_suiteb_ciphersuites[] = {
4893 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4894 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4895 0
4896};
4897
Ronald Crone68ab4f2022-10-05 12:46:29 +02004898#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004899
Jerry Yu909df7b2022-01-22 11:56:27 +08004900/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004901 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004902 * rules SHOULD be upheld.
4903 * - No duplicate entries.
4904 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004905 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004906 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004907 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004908static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004909
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#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 +08004911 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4912 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004913#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004914 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004915
Andrzej Kurek25f27152022-08-17 16:09:31 -04004916#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 +08004917 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4918 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004919#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004920 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4921
Andrzej Kurek25f27152022-08-17 16:09:31 -04004922#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 +08004923 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4924 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004925#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004926 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004927
Andrzej Kurek25f27152022-08-17 16:09:31 -04004928#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 +08004929 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004930#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 +08004931
Andrzej Kurek25f27152022-08-17 16:09:31 -04004932#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 +08004933 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004934#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 +08004935
Andrzej Kurek25f27152022-08-17 16:09:31 -04004936#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 +08004937 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004938#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 +08004939
Andrzej Kurek25f27152022-08-17 16:09:31 -04004940#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 +08004941 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4942#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4943
Andrzej Kurek25f27152022-08-17 16:09:31 -04004944#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 +08004945 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4946#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4947
Andrzej Kurek25f27152022-08-17 16:09:31 -04004948#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 +08004949 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4950#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4951
Gabor Mezei15b95a62022-05-09 16:37:58 +02004952 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004953};
4954
4955/* NOTICE: see above */
4956#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4957static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004959#if defined(MBEDTLS_ECDSA_C)
4960 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004961#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004962#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4963 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4964#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004965#if defined(MBEDTLS_RSA_C)
4966 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4967#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004968#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004970#if defined(MBEDTLS_ECDSA_C)
4971 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004972#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004973#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4974 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4975#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004976#if defined(MBEDTLS_RSA_C)
4977 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4978#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004979#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004980#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004981#if defined(MBEDTLS_ECDSA_C)
4982 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004983#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004984#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4985 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4986#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004987#if defined(MBEDTLS_RSA_C)
4988 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4989#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004990#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004991 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004992};
4993#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4994/* NOTICE: see above */
4995static uint16_t ssl_preset_suiteb_sig_algs[] = {
4996
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#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 +08004998 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4999 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005000#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005001 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5002
Andrzej Kurek25f27152022-08-17 16:09:31 -04005003#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 +08005004 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5005 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005006#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005007 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005008
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#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 +08005010 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005011#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 +08005012
Andrzej Kurek25f27152022-08-17 16:09:31 -04005013#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 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005015#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005016
Gabor Mezei15b95a62022-05-09 16:37:58 +02005017 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005018};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005019
Jerry Yu909df7b2022-01-22 11:56:27 +08005020/* NOTICE: see above */
5021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5022static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_ECDSA_C)
5025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08005026#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005027#if defined(MBEDTLS_RSA_C)
5028 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
5029#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005030#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005031#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005032#if defined(MBEDTLS_ECDSA_C)
5033 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08005034#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#if defined(MBEDTLS_RSA_C)
5036 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5037#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005038#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005039 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005040};
Jerry Yu909df7b2022-01-22 11:56:27 +08005041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5042
Ronald Crone68ab4f2022-10-05 12:46:29 +02005043#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005044
Brett Warrene0edc842021-08-17 09:53:13 +01005045static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005046#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005047 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005048#endif
5049#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005050 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005051#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005052 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005053};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005054
Ronald Crone68ab4f2022-10-05 12:46:29 +02005055#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005056/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005057 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005058MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005059static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005060{
5061 size_t i, j;
5062 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005063
Gabor Mezei15b95a62022-05-09 16:37:58 +02005064 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005065 {
Jerry Yuf377d642022-01-25 10:43:59 +08005066 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005067 {
Jerry Yuf377d642022-01-25 10:43:59 +08005068 if( sig_algs[i] != sig_algs[j] )
5069 continue;
5070 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5071 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5072 sig_algs[i], j, i );
5073 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005074 }
5075 }
5076 return( ret );
5077}
5078
Ronald Crone68ab4f2022-10-05 12:46:29 +02005079#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005080
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005081/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005082 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005083 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005084int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005085 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005086{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005087#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005089#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005090
Ronald Crone68ab4f2022-10-05 12:46:29 +02005091#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005092 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005093 {
5094 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5095 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5096 }
5097
Jerry Yuf377d642022-01-25 10:43:59 +08005098 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005099 {
5100 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5101 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5102 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005103
5104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005105 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005106 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005107 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005108 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5109 }
5110
Jerry Yuf377d642022-01-25 10:43:59 +08005111 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005112 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005113 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005114 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5115 }
5116#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005117#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005118
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005119 /* Use the functions here so that they are covered in tests,
5120 * but otherwise access member directly for efficiency */
5121 mbedtls_ssl_conf_endpoint( conf, endpoint );
5122 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005123
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005124 /*
5125 * Things that are common to all presets
5126 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005127#if defined(MBEDTLS_SSL_CLI_C)
5128 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5129 {
5130 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5131#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5132 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5133#endif
5134 }
5135#endif
5136
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005137#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5138 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5139#endif
5140
5141#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5142 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5143#endif
5144
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005145#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005146 conf->f_cookie_write = ssl_cookie_write_dummy;
5147 conf->f_cookie_check = ssl_cookie_check_dummy;
5148#endif
5149
5150#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5151 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5152#endif
5153
Janos Follath088ce432017-04-10 12:42:31 +01005154#if defined(MBEDTLS_SSL_SRV_C)
5155 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005156 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005157#endif
5158
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005159#if defined(MBEDTLS_SSL_PROTO_DTLS)
5160 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5161 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5162#endif
5163
5164#if defined(MBEDTLS_SSL_RENEGOTIATION)
5165 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005166 memset( conf->renego_period, 0x00, 2 );
5167 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005168#endif
5169
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005170#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005171 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5172 {
5173 const unsigned char dhm_p[] =
5174 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5175 const unsigned char dhm_g[] =
5176 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005177
Hanno Beckere2defad2021-07-24 05:59:17 +01005178 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5179 dhm_p, sizeof( dhm_p ),
5180 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5181 {
5182 return( ret );
5183 }
5184 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005185#endif
5186
Ronald Cron6f135e12021-12-08 16:57:54 +01005187#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005188
5189#if defined(MBEDTLS_SSL_EARLY_DATA)
5190 mbedtls_ssl_tls13_conf_early_data( conf, MBEDTLS_SSL_EARLY_DATA_DISABLED );
5191#if defined(MBEDTLS_SSL_SRV_C)
5192 mbedtls_ssl_tls13_conf_max_early_data_size(
5193 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE );
5194#endif
5195#endif /* MBEDTLS_SSL_EARLY_DATA */
5196
Jerry Yud0766ec2022-09-22 10:46:57 +08005197#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005198 mbedtls_ssl_conf_new_session_tickets(
5199 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5200#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005201 /*
5202 * Allow all TLS 1.3 key exchange modes by default.
5203 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005204 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005205#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005206
XiaokangQian4d3a6042022-04-21 13:46:17 +00005207 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5208 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005209#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005210 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005211 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005212#else
XiaokangQian060d8672022-04-21 09:24:56 +00005213 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005214#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005215 }
5216 else
5217 {
5218#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5219 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5220 {
5221 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5222 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5223 }
5224 else
5225 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5226 {
5227 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5228 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5229 }
5230#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5231 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5232 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5233#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5234 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5235 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5236#else
5237 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5238#endif
5239 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005240
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005241 /*
5242 * Preset-specific defaults
5243 */
5244 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005245 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005246 /*
5247 * NSA Suite B
5248 */
5249 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005250
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005251 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005252
5253#if defined(MBEDTLS_X509_CRT_PARSE_C)
5254 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005255#endif
5256
Ronald Crone68ab4f2022-10-05 12:46:29 +02005257#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005258#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5259 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5260 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5261 else
5262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5263 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005264#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005265
Brett Warrene0edc842021-08-17 09:53:13 +01005266#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5267 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005268#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005269 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005270 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005271
5272 /*
5273 * Default
5274 */
5275 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005276
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005277 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005278
5279#if defined(MBEDTLS_X509_CRT_PARSE_C)
5280 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5281#endif
5282
Ronald Crone68ab4f2022-10-05 12:46:29 +02005283#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005284#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5285 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5286 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5287 else
5288#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5289 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005290#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005291
Brett Warrene0edc842021-08-17 09:53:13 +01005292#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5293 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005294#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005295 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005296
5297#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5298 conf->dhm_min_bitlen = 1024;
5299#endif
5300 }
5301
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005302 return( 0 );
5303}
5304
5305/*
5306 * Free mbedtls_ssl_config
5307 */
5308void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5309{
5310#if defined(MBEDTLS_DHM_C)
5311 mbedtls_mpi_free( &conf->dhm_P );
5312 mbedtls_mpi_free( &conf->dhm_G );
5313#endif
5314
Ronald Cron73fe8df2022-10-05 14:31:43 +02005315#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005316#if defined(MBEDTLS_USE_PSA_CRYPTO)
5317 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5318 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005319 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5320 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005321#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005322 if( conf->psk != NULL )
5323 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005324 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005325 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005326 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005327 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005328 }
5329
5330 if( conf->psk_identity != NULL )
5331 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005332 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005333 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005334 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005335 conf->psk_identity_len = 0;
5336 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005337#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005338
5339#if defined(MBEDTLS_X509_CRT_PARSE_C)
5340 ssl_key_cert_free( conf->key_cert );
5341#endif
5342
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005343 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005344}
5345
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005346#if defined(MBEDTLS_PK_C) && \
5347 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005348/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if defined(MBEDTLS_RSA_C)
5354 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5355 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357#if defined(MBEDTLS_ECDSA_C)
5358 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5359 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005362}
5363
Hanno Becker7e5437a2017-04-28 17:15:26 +01005364unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5365{
5366 switch( type ) {
5367 case MBEDTLS_PK_RSA:
5368 return( MBEDTLS_SSL_SIG_RSA );
5369 case MBEDTLS_PK_ECDSA:
5370 case MBEDTLS_PK_ECKEY:
5371 return( MBEDTLS_SSL_SIG_ECDSA );
5372 default:
5373 return( MBEDTLS_SSL_SIG_ANON );
5374 }
5375}
5376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005378{
5379 switch( sig )
5380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381#if defined(MBEDTLS_RSA_C)
5382 case MBEDTLS_SSL_SIG_RSA:
5383 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385#if defined(MBEDTLS_ECDSA_C)
5386 case MBEDTLS_SSL_SIG_ECDSA:
5387 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005388#endif
5389 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005391 }
5392}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005393#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005394
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005395/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005396 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005399{
5400 switch( hash )
5401 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005402#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 case MBEDTLS_SSL_HASH_MD5:
5404 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005405#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005406#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 case MBEDTLS_SSL_HASH_SHA1:
5408 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005409#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005410#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 case MBEDTLS_SSL_HASH_SHA224:
5412 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005413#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005414#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 case MBEDTLS_SSL_HASH_SHA256:
5416 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005417#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005418#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 case MBEDTLS_SSL_HASH_SHA384:
5420 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005421#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005422#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 case MBEDTLS_SSL_HASH_SHA512:
5424 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005425#endif
5426 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005428 }
5429}
5430
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005431/*
5432 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5433 */
5434unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5435{
5436 switch( md )
5437 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005438#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005439 case MBEDTLS_MD_MD5:
5440 return( MBEDTLS_SSL_HASH_MD5 );
5441#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005442#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005443 case MBEDTLS_MD_SHA1:
5444 return( MBEDTLS_SSL_HASH_SHA1 );
5445#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005446#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005447 case MBEDTLS_MD_SHA224:
5448 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005449#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005450#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005451 case MBEDTLS_MD_SHA256:
5452 return( MBEDTLS_SSL_HASH_SHA256 );
5453#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005454#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005455 case MBEDTLS_MD_SHA384:
5456 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005457#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005458#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005459 case MBEDTLS_MD_SHA512:
5460 return( MBEDTLS_SSL_HASH_SHA512 );
5461#endif
5462 default:
5463 return( MBEDTLS_SSL_HASH_NONE );
5464 }
5465}
5466
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005467/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005468 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005469 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005470 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005471int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005472{
Brett Warrene0edc842021-08-17 09:53:13 +01005473 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005474
Brett Warrene0edc842021-08-17 09:53:13 +01005475 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005476 return( -1 );
5477
Brett Warrene0edc842021-08-17 09:53:13 +01005478 for( ; *group_list != 0; group_list++ )
5479 {
5480 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005481 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005482 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005483
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005484 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005485}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005486
5487#if defined(MBEDTLS_ECP_C)
5488/*
5489 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5490 */
5491int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5492{
Valerio Setti18c9fed2022-12-30 17:44:24 +01005493 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005494
Valerio Setti18c9fed2022-12-30 17:44:24 +01005495 if ( tls_id == 0 )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005496 return -1;
5497
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005498 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5499}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005500#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005501
Valerio Setti67419f02023-01-04 16:12:42 +01005502#if defined( MBEDTLS_DEBUG_C )
5503#define EC_NAME(_name_) _name_
5504#else
5505#define EC_NAME(_name_) NULL
5506#endif
5507
Valerio Setti18c9fed2022-12-30 17:44:24 +01005508static const struct {
5509 uint16_t tls_id;
5510 mbedtls_ecp_group_id ecp_group_id;
5511 psa_ecc_family_t psa_family;
5512 uint16_t bits;
5513 const char* name;
5514} tls_id_match_table[] =
5515{
5516#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Setti67419f02023-01-04 16:12:42 +01005517 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME( "secp521r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005518#endif
5519#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Setti67419f02023-01-04 16:12:42 +01005520 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME( "brainpoolP512r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005521#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005522#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
5523 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME( "secp384r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005524#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005525#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
5526 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME( "brainpoolP384r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005527#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005528#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
5529 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME( "secp256r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005530#endif
5531#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Setti67419f02023-01-04 16:12:42 +01005532 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME( "secp256k1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005533#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005534#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
5535 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME( "brainpoolP256r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005536#endif
5537#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Setti67419f02023-01-04 16:12:42 +01005538 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME( "secp224r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005539#endif
5540#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Setti67419f02023-01-04 16:12:42 +01005541 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME( "secp224k1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005542#endif
5543#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Setti67419f02023-01-04 16:12:42 +01005544 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME( "secp192r1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005545#endif
5546#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Setti67419f02023-01-04 16:12:42 +01005547 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME( "secp192k1" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005548#endif
5549#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Setti67419f02023-01-04 16:12:42 +01005550 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME( "x25519" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005551#endif
5552#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Setti67419f02023-01-04 16:12:42 +01005553 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME( "x448" ) },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005554#endif
5555 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5556};
5557
5558int mbedtls_ssl_get_psa_curve_info_from_tls_id( uint16_t tls_id,
5559 psa_ecc_family_t *family,
5560 size_t* bits )
5561{
5562 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5563 {
5564 if( tls_id_match_table[i].tls_id == tls_id )
5565 {
5566 if( family != NULL )
5567 *family = tls_id_match_table[i].psa_family;
5568 if( bits != NULL )
5569 *bits = tls_id_match_table[i].bits;
5570 return PSA_SUCCESS;
5571 }
5572 }
5573
5574 return PSA_ERROR_NOT_SUPPORTED;
5575}
5576
5577mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id( uint16_t tls_id )
5578{
5579 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5580 {
5581 if( tls_id_match_table[i].tls_id == tls_id )
5582 return tls_id_match_table[i].ecp_group_id;
5583 }
5584
5585 return MBEDTLS_ECP_DP_NONE;
5586}
5587
5588uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id( mbedtls_ecp_group_id grp_id )
5589{
5590 for( int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5591 i++ )
5592 {
5593 if( tls_id_match_table[i].ecp_group_id == grp_id )
5594 return tls_id_match_table[i].tls_id;
5595 }
5596
5597 return 0;
5598}
5599
Valerio Setti67419f02023-01-04 16:12:42 +01005600#if defined(MBEDTLS_DEBUG_C)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005601const char* mbedtls_ssl_get_curve_name_from_tls_id( uint16_t tls_id )
5602{
5603 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5604 {
5605 if( tls_id_match_table[i].tls_id == tls_id )
5606 return tls_id_match_table[i].name;
5607 }
5608
5609 return NULL;
5610}
Valerio Setti67419f02023-01-04 16:12:42 +01005611#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_X509_CRT_PARSE_C)
5614int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5615 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005616 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005617 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005618{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005619 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005620 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005621 const char *ext_oid;
5622 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005625 {
5626 /* Server part of the key exchange */
5627 switch( ciphersuite->key_exchange )
5628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 case MBEDTLS_KEY_EXCHANGE_RSA:
5630 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005631 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005632 break;
5633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5635 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5636 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5637 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005638 break;
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5641 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005642 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005643 break;
5644
5645 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 case MBEDTLS_KEY_EXCHANGE_NONE:
5647 case MBEDTLS_KEY_EXCHANGE_PSK:
5648 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5649 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005650 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005651 usage = 0;
5652 }
5653 }
5654 else
5655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5657 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005658 }
5659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005661 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005662 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005663 ret = -1;
5664 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5669 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005670 }
5671 else
5672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5674 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005675 }
5676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005678 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005679 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005680 ret = -1;
5681 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005682
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005683 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005686
Jerry Yu148165c2021-09-24 23:20:59 +08005687#if defined(MBEDTLS_USE_PSA_CRYPTO)
5688int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5689 const mbedtls_md_type_t md,
5690 unsigned char *dst,
5691 size_t dst_len,
5692 size_t *olen )
5693{
Ronald Cronf6893e12022-01-07 22:09:01 +01005694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5695 psa_hash_operation_t *hash_operation_to_clone;
5696 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5697
Jerry Yu148165c2021-09-24 23:20:59 +08005698 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005699
5700 switch( md )
5701 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005702#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005703 case MBEDTLS_MD_SHA384:
5704 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5705 break;
5706#endif
5707
Andrzej Kurek25f27152022-08-17 16:09:31 -04005708#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005709 case MBEDTLS_MD_SHA256:
5710 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5711 break;
5712#endif
5713
5714 default:
5715 goto exit;
5716 }
5717
5718 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5719 if( status != PSA_SUCCESS )
5720 goto exit;
5721
5722 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5723 if( status != PSA_SUCCESS )
5724 goto exit;
5725
5726exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005727#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5728 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5729 (void) ssl;
5730#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005731 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005732}
5733#else /* MBEDTLS_USE_PSA_CRYPTO */
5734
Andrzej Kurek25f27152022-08-17 16:09:31 -04005735#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005736MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005737static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5738 unsigned char *dst,
5739 size_t dst_len,
5740 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005741{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005742 int ret;
5743 mbedtls_sha512_context sha512;
5744
5745 if( dst_len < 48 )
5746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5747
5748 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005749 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005750
5751 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5752 {
5753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5754 goto exit;
5755 }
5756
5757 *olen = 48;
5758
5759exit:
5760
5761 mbedtls_sha512_free( &sha512 );
5762 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005763}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005764#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005765
Andrzej Kurek25f27152022-08-17 16:09:31 -04005766#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005767MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005768static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5769 unsigned char *dst,
5770 size_t dst_len,
5771 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005772{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005773 int ret;
5774 mbedtls_sha256_context sha256;
5775
5776 if( dst_len < 32 )
5777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5778
5779 mbedtls_sha256_init( &sha256 );
5780 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005781
Jerry Yu24c0ec32021-09-09 14:21:07 +08005782 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5783 {
5784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5785 goto exit;
5786 }
5787
5788 *olen = 32;
5789
5790exit:
5791
5792 mbedtls_sha256_free( &sha256 );
5793 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005794}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005795#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005796
Jerry Yu000f9762021-09-14 11:12:51 +08005797int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5798 const mbedtls_md_type_t md,
5799 unsigned char *dst,
5800 size_t dst_len,
5801 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005803 switch( md )
5804 {
5805
Andrzej Kurek25f27152022-08-17 16:09:31 -04005806#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005807 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005808 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005809#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005810
Andrzej Kurek25f27152022-08-17 16:09:31 -04005811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005812 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005813 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005814#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005815
5816 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005817#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5818 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5819 (void) ssl;
5820 (void) dst;
5821 (void) dst_len;
5822 (void) olen;
5823#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005824 break;
5825 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005827}
XiaokangQian647719a2021-12-07 09:16:29 +00005828
Jerry Yu148165c2021-09-24 23:20:59 +08005829#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005830
Ronald Crone68ab4f2022-10-05 12:46:29 +02005831#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005832/* mbedtls_ssl_parse_sig_alg_ext()
5833 *
5834 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5835 * value (TLS 1.3 RFC8446):
5836 * enum {
5837 * ....
5838 * ecdsa_secp256r1_sha256( 0x0403 ),
5839 * ecdsa_secp384r1_sha384( 0x0503 ),
5840 * ecdsa_secp521r1_sha512( 0x0603 ),
5841 * ....
5842 * } SignatureScheme;
5843 *
5844 * struct {
5845 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5846 * } SignatureSchemeList;
5847 *
5848 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5849 * value (TLS 1.2 RFC5246):
5850 * enum {
5851 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5852 * sha512(6), (255)
5853 * } HashAlgorithm;
5854 *
5855 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5856 * SignatureAlgorithm;
5857 *
5858 * struct {
5859 * HashAlgorithm hash;
5860 * SignatureAlgorithm signature;
5861 * } SignatureAndHashAlgorithm;
5862 *
5863 * SignatureAndHashAlgorithm
5864 * supported_signature_algorithms<2..2^16-2>;
5865 *
5866 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5867 * generalization of the TLS 1.2 signature algorithm extension.
5868 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5869 * `SignatureScheme` field of TLS 1.3
5870 *
5871 */
5872int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5873 const unsigned char *buf,
5874 const unsigned char *end )
5875{
5876 const unsigned char *p = buf;
5877 size_t supported_sig_algs_len = 0;
5878 const unsigned char *supported_sig_algs_end;
5879 uint16_t sig_alg;
5880 uint32_t common_idx = 0;
5881
5882 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5883 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5884 p += 2;
5885
5886 memset( ssl->handshake->received_sig_algs, 0,
5887 sizeof(ssl->handshake->received_sig_algs) );
5888
5889 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5890 supported_sig_algs_end = p + supported_sig_algs_len;
5891 while( p < supported_sig_algs_end )
5892 {
5893 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5894 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5895 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005896 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5897 sig_alg,
5898 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005899#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005900 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005901 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5902 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5903 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005904 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005905 }
5906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005907
Jerry Yuf3b46b52022-06-19 16:52:27 +08005908 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5909 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005910
5911 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5912 {
5913 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5914 common_idx += 1;
5915 }
5916 }
5917 /* Check that we consumed all the message. */
5918 if( p != end )
5919 {
5920 MBEDTLS_SSL_DEBUG_MSG( 1,
5921 ( "Signature algorithms extension length misaligned" ) );
5922 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5923 MBEDTLS_ERR_SSL_DECODE_ERROR );
5924 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5925 }
5926
5927 if( common_idx == 0 )
5928 {
5929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5930 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5931 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5932 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5933 }
5934
Gabor Mezei15b95a62022-05-09 16:37:58 +02005935 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005936 return( 0 );
5937}
5938
Ronald Crone68ab4f2022-10-05 12:46:29 +02005939#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005940
Jerry Yudc7bd172022-02-17 13:44:15 +08005941#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5942
5943#if defined(MBEDTLS_USE_PSA_CRYPTO)
5944
5945static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5946 mbedtls_svc_key_id_t key,
5947 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005948 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005949 const unsigned char* seed, size_t seed_length,
5950 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005951 const unsigned char* other_secret,
5952 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005953 size_t capacity )
5954{
5955 psa_status_t status;
5956
5957 status = psa_key_derivation_setup( derivation, alg );
5958 if( status != PSA_SUCCESS )
5959 return( status );
5960
5961 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5962 {
5963 status = psa_key_derivation_input_bytes( derivation,
5964 PSA_KEY_DERIVATION_INPUT_SEED,
5965 seed, seed_length );
5966 if( status != PSA_SUCCESS )
5967 return( status );
5968
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005969 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005970 {
5971 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005972 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5973 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005974 if( status != PSA_SUCCESS )
5975 return( status );
5976 }
5977
Jerry Yudc7bd172022-02-17 13:44:15 +08005978 if( mbedtls_svc_key_id_is_null( key ) )
5979 {
5980 status = psa_key_derivation_input_bytes(
5981 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005982 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005983 }
5984 else
5985 {
5986 status = psa_key_derivation_input_key(
5987 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5988 }
5989 if( status != PSA_SUCCESS )
5990 return( status );
5991
5992 status = psa_key_derivation_input_bytes( derivation,
5993 PSA_KEY_DERIVATION_INPUT_LABEL,
5994 label, label_length );
5995 if( status != PSA_SUCCESS )
5996 return( status );
5997 }
5998 else
5999 {
6000 return( PSA_ERROR_NOT_SUPPORTED );
6001 }
6002
6003 status = psa_key_derivation_set_capacity( derivation, capacity );
6004 if( status != PSA_SUCCESS )
6005 return( status );
6006
6007 return( PSA_SUCCESS );
6008}
6009
Andrzej Kurek57d10632022-10-24 10:32:01 -04006010#if defined(PSA_WANT_ALG_SHA_384) || \
6011 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006013static int tls_prf_generic( mbedtls_md_type_t md_type,
6014 const unsigned char *secret, size_t slen,
6015 const char *label,
6016 const unsigned char *random, size_t rlen,
6017 unsigned char *dstbuf, size_t dlen )
6018{
6019 psa_status_t status;
6020 psa_algorithm_t alg;
6021 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6022 psa_key_derivation_operation_t derivation =
6023 PSA_KEY_DERIVATION_OPERATION_INIT;
6024
6025 if( md_type == MBEDTLS_MD_SHA384 )
6026 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
6027 else
6028 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
6029
6030 /* Normally a "secret" should be long enough to be impossible to
6031 * find by brute force, and in particular should not be empty. But
6032 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6033 * and for this use case it makes sense to have a 0-length "secret".
6034 * Since the key API doesn't allow importing a key of length 0,
6035 * keep master_key=0, which setup_psa_key_derivation() understands
6036 * to mean a 0-length "secret" input. */
6037 if( slen != 0 )
6038 {
6039 psa_key_attributes_t key_attributes = psa_key_attributes_init();
6040 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
6041 psa_set_key_algorithm( &key_attributes, alg );
6042 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
6043
6044 status = psa_import_key( &key_attributes, secret, slen, &master_key );
6045 if( status != PSA_SUCCESS )
6046 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6047 }
6048
6049 status = setup_psa_key_derivation( &derivation,
6050 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006051 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08006052 random, rlen,
6053 (unsigned char const *) label,
6054 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02006055 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08006056 dlen );
6057 if( status != PSA_SUCCESS )
6058 {
6059 psa_key_derivation_abort( &derivation );
6060 psa_destroy_key( master_key );
6061 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6062 }
6063
6064 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
6065 if( status != PSA_SUCCESS )
6066 {
6067 psa_key_derivation_abort( &derivation );
6068 psa_destroy_key( master_key );
6069 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6070 }
6071
6072 status = psa_key_derivation_abort( &derivation );
6073 if( status != PSA_SUCCESS )
6074 {
6075 psa_destroy_key( master_key );
6076 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6077 }
6078
6079 if( ! mbedtls_svc_key_id_is_null( master_key ) )
6080 status = psa_destroy_key( master_key );
6081 if( status != PSA_SUCCESS )
6082 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6083
6084 return( 0 );
6085}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006086#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006087#else /* MBEDTLS_USE_PSA_CRYPTO */
6088
Andrzej Kurek57d10632022-10-24 10:32:01 -04006089#if defined(MBEDTLS_MD_C) && \
6090 ( defined(MBEDTLS_SHA256_C) || \
6091 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006092MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006093static int tls_prf_generic( mbedtls_md_type_t md_type,
6094 const unsigned char *secret, size_t slen,
6095 const char *label,
6096 const unsigned char *random, size_t rlen,
6097 unsigned char *dstbuf, size_t dlen )
6098{
6099 size_t nb;
6100 size_t i, j, k, md_len;
6101 unsigned char *tmp;
6102 size_t tmp_len = 0;
6103 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6104 const mbedtls_md_info_t *md_info;
6105 mbedtls_md_context_t md_ctx;
6106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6107
6108 mbedtls_md_init( &md_ctx );
6109
6110 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
6111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6112
6113 md_len = mbedtls_md_get_size( md_info );
6114
6115 tmp_len = md_len + strlen( label ) + rlen;
6116 tmp = mbedtls_calloc( 1, tmp_len );
6117 if( tmp == NULL )
6118 {
6119 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6120 goto exit;
6121 }
6122
6123 nb = strlen( label );
6124 memcpy( tmp + md_len, label, nb );
6125 memcpy( tmp + md_len + nb, random, rlen );
6126 nb += rlen;
6127
6128 /*
6129 * Compute P_<hash>(secret, label + random)[0..dlen]
6130 */
6131 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
6132 goto exit;
6133
6134 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
6135 if( ret != 0 )
6136 goto exit;
6137 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
6138 if( ret != 0 )
6139 goto exit;
6140 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6141 if( ret != 0 )
6142 goto exit;
6143
6144 for( i = 0; i < dlen; i += md_len )
6145 {
6146 ret = mbedtls_md_hmac_reset ( &md_ctx );
6147 if( ret != 0 )
6148 goto exit;
6149 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
6150 if( ret != 0 )
6151 goto exit;
6152 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6153 if( ret != 0 )
6154 goto exit;
6155
6156 ret = mbedtls_md_hmac_reset ( &md_ctx );
6157 if( ret != 0 )
6158 goto exit;
6159 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6160 if( ret != 0 )
6161 goto exit;
6162 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6163 if( ret != 0 )
6164 goto exit;
6165
6166 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6167
6168 for( j = 0; j < k; j++ )
6169 dstbuf[i + j] = h_i[j];
6170 }
6171
6172exit:
6173 mbedtls_md_free( &md_ctx );
6174
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006175 if ( tmp != NULL )
6176 mbedtls_platform_zeroize( tmp, tmp_len );
6177
Jerry Yudc7bd172022-02-17 13:44:15 +08006178 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6179
6180 mbedtls_free( tmp );
6181
6182 return( ret );
6183}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006184#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006185#endif /* MBEDTLS_USE_PSA_CRYPTO */
6186
Andrzej Kurek25f27152022-08-17 16:09:31 -04006187#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006188MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006189static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6190 const char *label,
6191 const unsigned char *random, size_t rlen,
6192 unsigned char *dstbuf, size_t dlen )
6193{
6194 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6195 label, random, rlen, dstbuf, dlen ) );
6196}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006197#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006198
Andrzej Kurek25f27152022-08-17 16:09:31 -04006199#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006200MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006201static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6202 const char *label,
6203 const unsigned char *random, size_t rlen,
6204 unsigned char *dstbuf, size_t dlen )
6205{
6206 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6207 label, random, rlen, dstbuf, dlen ) );
6208}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006209#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006210
Jerry Yuf009d862022-02-17 14:01:37 +08006211/*
6212 * Set appropriate PRF function and other SSL / TLS1.2 functions
6213 *
6214 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006215 * - hash associated with the ciphersuite (only used by TLS 1.2)
6216 *
6217 * Outputs:
6218 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6219 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006220MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006221static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006222 mbedtls_md_type_t hash )
6223{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006224#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006225 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006226 {
6227 handshake->tls_prf = tls_prf_sha384;
6228 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6229 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6230 }
6231 else
6232#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006233#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006234 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006235 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006236 handshake->tls_prf = tls_prf_sha256;
6237 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6238 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6239 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006240#else
Jerry Yuf009d862022-02-17 14:01:37 +08006241 {
Jerry Yuf009d862022-02-17 14:01:37 +08006242 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006243 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6245 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006246#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006247
6248 return( 0 );
6249}
Jerry Yud6ab2352022-02-17 14:03:43 +08006250
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006251/*
6252 * Compute master secret if needed
6253 *
6254 * Parameters:
6255 * [in/out] handshake
6256 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6257 * (PSA-PSK) ciphersuite_info, psk_opaque
6258 * [out] premaster (cleared)
6259 * [out] master
6260 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6261 * debug: conf->f_dbg, conf->p_dbg
6262 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006263 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006264 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006266static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6267 unsigned char *master,
6268 const mbedtls_ssl_context *ssl )
6269{
6270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6271
6272 /* cf. RFC 5246, Section 8.1:
6273 * "The master secret is always exactly 48 bytes in length." */
6274 size_t const master_secret_len = 48;
6275
6276#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6277 unsigned char session_hash[48];
6278#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6279
6280 /* The label for the KDF used for key expansion.
6281 * This is either "master secret" or "extended master secret"
6282 * depending on whether the Extended Master Secret extension
6283 * is used. */
6284 char const *lbl = "master secret";
6285
Przemek Stekielae4ed302022-04-05 17:15:55 +02006286 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006287 * - If the Extended Master Secret extension is not used,
6288 * this is ClientHello.Random + ServerHello.Random
6289 * (see Sect. 8.1 in RFC 5246).
6290 * - If the Extended Master Secret extension is used,
6291 * this is the transcript of the handshake so far.
6292 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006293 unsigned char const *seed = handshake->randbytes;
6294 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006295
6296#if !defined(MBEDTLS_DEBUG_C) && \
6297 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6298 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6299 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6300 ssl = NULL; /* make sure we don't use it except for those cases */
6301 (void) ssl;
6302#endif
6303
6304 if( handshake->resume != 0 )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6307 return( 0 );
6308 }
6309
6310#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6311 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6312 {
6313 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006314 seed = session_hash;
6315 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006316
6317 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006318 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006319 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006320#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006321
Przemek Stekiel99114f32022-04-22 11:20:09 +02006322#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006323 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006324 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006325 {
6326 /* Perform PSK-to-MS expansion in a single step. */
6327 psa_status_t status;
6328 psa_algorithm_t alg;
6329 mbedtls_svc_key_id_t psk;
6330 psa_key_derivation_operation_t derivation =
6331 PSA_KEY_DERIVATION_OPERATION_INIT;
6332 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6333
6334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6335
6336 psk = mbedtls_ssl_get_opaque_psk( ssl );
6337
6338 if( hash_alg == MBEDTLS_MD_SHA384 )
6339 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6340 else
6341 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6342
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006343 size_t other_secret_len = 0;
6344 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006345
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006346 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006347 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006348 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006349 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006350 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006351 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006352 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006353 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006354 other_secret_len = 48;
6355 other_secret = handshake->premaster + 2;
6356 break;
6357 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006358 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006359 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6360 other_secret = handshake->premaster + 2;
6361 break;
6362 default:
6363 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006364 }
6365
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006366 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006367 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006368 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006369 (unsigned char const *) lbl,
6370 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006371 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006372 master_secret_len );
6373 if( status != PSA_SUCCESS )
6374 {
6375 psa_key_derivation_abort( &derivation );
6376 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6377 }
6378
6379 status = psa_key_derivation_output_bytes( &derivation,
6380 master,
6381 master_secret_len );
6382 if( status != PSA_SUCCESS )
6383 {
6384 psa_key_derivation_abort( &derivation );
6385 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6386 }
6387
6388 status = psa_key_derivation_abort( &derivation );
6389 if( status != PSA_SUCCESS )
6390 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6391 }
6392 else
6393#endif
6394 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006395#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6396 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6397 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6398 {
6399 psa_status_t status;
6400 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6401 psa_key_derivation_operation_t derivation =
6402 PSA_KEY_DERIVATION_OPERATION_INIT;
6403
6404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6405
6406 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6407
6408 status = psa_key_derivation_setup( &derivation, alg );
6409 if( status != PSA_SUCCESS )
6410 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6411
6412 status = psa_key_derivation_set_capacity( &derivation,
6413 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6414 if( status != PSA_SUCCESS )
6415 {
6416 psa_key_derivation_abort( &derivation );
6417 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6418 }
6419
6420 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6421 &derivation );
6422 if( status != PSA_SUCCESS )
6423 {
6424 psa_key_derivation_abort( &derivation );
6425 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6426 }
6427
6428 status = psa_key_derivation_output_bytes( &derivation,
6429 handshake->premaster,
6430 handshake->pmslen );
6431 if( status != PSA_SUCCESS )
6432 {
6433 psa_key_derivation_abort( &derivation );
6434 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6435 }
6436
6437 status = psa_key_derivation_abort( &derivation );
6438 if( status != PSA_SUCCESS )
6439 {
6440 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6441 }
6442 }
6443#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006444 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006445 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006446 master,
6447 master_secret_len );
6448 if( ret != 0 )
6449 {
6450 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6451 return( ret );
6452 }
6453
6454 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6455 handshake->premaster,
6456 handshake->pmslen );
6457
6458 mbedtls_platform_zeroize( handshake->premaster,
6459 sizeof(handshake->premaster) );
6460 }
6461
6462 return( 0 );
6463}
6464
Jerry Yud62f87e2022-02-17 14:09:02 +08006465int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6466{
6467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6468 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6469 ssl->handshake->ciphersuite_info;
6470
6471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6472
6473 /* Set PRF, calc_verify and calc_finished function pointers */
6474 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006475 ciphersuite_info->mac );
6476 if( ret != 0 )
6477 {
6478 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6479 return( ret );
6480 }
6481
6482 /* Compute master secret if needed */
6483 ret = ssl_compute_master( ssl->handshake,
6484 ssl->session_negotiate->master,
6485 ssl );
6486 if( ret != 0 )
6487 {
6488 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6489 return( ret );
6490 }
6491
6492 /* Swap the client and server random values:
6493 * - MS derivation wanted client+server (RFC 5246 8.1)
6494 * - key derivation wants server+client (RFC 5246 6.3) */
6495 {
6496 unsigned char tmp[64];
6497 memcpy( tmp, ssl->handshake->randbytes, 64 );
6498 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6499 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6500 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6501 }
6502
6503 /* Populate transform structure */
6504 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6505 ssl->session_negotiate->ciphersuite,
6506 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006507#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006508 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006509#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006510 ssl->handshake->tls_prf,
6511 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006512 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006513 ssl->conf->endpoint,
6514 ssl );
6515 if( ret != 0 )
6516 {
6517 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6518 return( ret );
6519 }
6520
6521 /* We no longer need Server/ClientHello.random values */
6522 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6523 sizeof( ssl->handshake->randbytes ) );
6524
6525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6526
6527 return( 0 );
6528}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006529
Ronald Cron4dcbca92022-03-07 10:21:40 +01006530int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6531{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006532 switch( md )
6533 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006534#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006535 case MBEDTLS_SSL_HASH_SHA384:
6536 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6537 break;
6538#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006539#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006540 case MBEDTLS_SSL_HASH_SHA256:
6541 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6542 break;
6543#endif
6544 default:
6545 return( -1 );
6546 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006547#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6548 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6549 (void) ssl;
6550#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006551 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006552}
6553
Andrzej Kurek25f27152022-08-17 16:09:31 -04006554#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006555void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6556 unsigned char *hash,
6557 size_t *hlen )
6558{
6559#if defined(MBEDTLS_USE_PSA_CRYPTO)
6560 size_t hash_size;
6561 psa_status_t status;
6562 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6563
6564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6565 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6566 if( status != PSA_SUCCESS )
6567 {
6568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6569 return;
6570 }
6571
6572 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6573 if( status != PSA_SUCCESS )
6574 {
6575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6576 return;
6577 }
6578
6579 *hlen = 32;
6580 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6582#else
6583 mbedtls_sha256_context sha256;
6584
6585 mbedtls_sha256_init( &sha256 );
6586
6587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6588
6589 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6590 mbedtls_sha256_finish( &sha256, hash );
6591
6592 *hlen = 32;
6593
6594 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6596
6597 mbedtls_sha256_free( &sha256 );
6598#endif /* MBEDTLS_USE_PSA_CRYPTO */
6599 return;
6600}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006601#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006602
Andrzej Kurek25f27152022-08-17 16:09:31 -04006603#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006604void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6605 unsigned char *hash,
6606 size_t *hlen )
6607{
6608#if defined(MBEDTLS_USE_PSA_CRYPTO)
6609 size_t hash_size;
6610 psa_status_t status;
6611 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6612
6613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6614 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6615 if( status != PSA_SUCCESS )
6616 {
6617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6618 return;
6619 }
6620
6621 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6622 if( status != PSA_SUCCESS )
6623 {
6624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6625 return;
6626 }
6627
6628 *hlen = 48;
6629 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6631#else
6632 mbedtls_sha512_context sha512;
6633
6634 mbedtls_sha512_init( &sha512 );
6635
6636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6637
Andrzej Kureka242e832022-08-11 10:03:14 -04006638 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006639 mbedtls_sha512_finish( &sha512, hash );
6640
6641 *hlen = 48;
6642
6643 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6645
6646 mbedtls_sha512_free( &sha512 );
6647#endif /* MBEDTLS_USE_PSA_CRYPTO */
6648 return;
6649}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006650#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006651
Neil Armstrong80f6f322022-05-03 17:56:38 +02006652#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6653 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006654int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6655{
6656 unsigned char *p = ssl->handshake->premaster;
6657 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6658 const unsigned char *psk = NULL;
6659 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006660 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006661
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006662 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006663 {
6664 /*
6665 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006666 * checked before calling this function.
6667 *
6668 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006669 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006670 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006671 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6675 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006676 }
6677
6678 /*
6679 * PMS = struct {
6680 * opaque other_secret<0..2^16-1>;
6681 * opaque psk<0..2^16-1>;
6682 * };
6683 * with "other_secret" depending on the particular key exchange
6684 */
6685#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6686 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6687 {
6688 if( end - p < 2 )
6689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6690
6691 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6692 p += 2;
6693
6694 if( end < p || (size_t)( end - p ) < psk_len )
6695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6696
6697 memset( p, 0, psk_len );
6698 p += psk_len;
6699 }
6700 else
6701#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6702#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6703 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6704 {
6705 /*
6706 * other_secret already set by the ClientKeyExchange message,
6707 * and is 48 bytes long
6708 */
6709 if( end - p < 2 )
6710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6711
6712 *p++ = 0;
6713 *p++ = 48;
6714 p += 48;
6715 }
6716 else
6717#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6718#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6719 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6720 {
6721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6722 size_t len;
6723
6724 /* Write length only when we know the actual value */
6725 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6726 p + 2, end - ( p + 2 ), &len,
6727 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6728 {
6729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6730 return( ret );
6731 }
6732 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6733 p += 2 + len;
6734
6735 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6736 }
6737 else
6738#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006739#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006740 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6741 {
6742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6743 size_t zlen;
6744
6745 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6746 p + 2, end - ( p + 2 ),
6747 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6748 {
6749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6750 return( ret );
6751 }
6752
6753 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6754 p += 2 + zlen;
6755
6756 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6757 MBEDTLS_DEBUG_ECDH_Z );
6758 }
6759 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006760#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006761 {
6762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6764 }
6765
6766 /* opaque psk<0..2^16-1>; */
6767 if( end - p < 2 )
6768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6769
6770 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6771 p += 2;
6772
6773 if( end < p || (size_t)( end - p ) < psk_len )
6774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6775
6776 memcpy( p, psk, psk_len );
6777 p += psk_len;
6778
6779 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6780
6781 return( 0 );
6782}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006783#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006784
6785#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006786MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006787static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6788
6789#if defined(MBEDTLS_SSL_PROTO_DTLS)
6790int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6791{
6792 /* If renegotiation is not enforced, retransmit until we would reach max
6793 * timeout if we were using the usual handshake doubling scheme */
6794 if( ssl->conf->renego_max_records < 0 )
6795 {
6796 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6797 unsigned char doublings = 1;
6798
6799 while( ratio != 0 )
6800 {
6801 ++doublings;
6802 ratio >>= 1;
6803 }
6804
6805 if( ++ssl->renego_records_seen > doublings )
6806 {
6807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6808 return( 0 );
6809 }
6810 }
6811
6812 return( ssl_write_hello_request( ssl ) );
6813}
6814#endif
6815#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006816
Jerry Yud9526692022-02-17 14:23:47 +08006817/*
6818 * Handshake functions
6819 */
6820#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6821/* No certificate support -> dummy functions */
6822int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6823{
6824 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6825 ssl->handshake->ciphersuite_info;
6826
6827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6828
6829 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6830 {
6831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6832 ssl->state++;
6833 return( 0 );
6834 }
6835
6836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6838}
6839
6840int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6841{
6842 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6843 ssl->handshake->ciphersuite_info;
6844
6845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6846
6847 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6848 {
6849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6850 ssl->state++;
6851 return( 0 );
6852 }
6853
6854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6856}
6857
6858#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6859/* Some certificate support -> implement write and parse */
6860
6861int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6862{
6863 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6864 size_t i, n;
6865 const mbedtls_x509_crt *crt;
6866 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6867 ssl->handshake->ciphersuite_info;
6868
6869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6870
6871 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6872 {
6873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6874 ssl->state++;
6875 return( 0 );
6876 }
6877
6878#if defined(MBEDTLS_SSL_CLI_C)
6879 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6880 {
6881 if( ssl->handshake->client_auth == 0 )
6882 {
6883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6884 ssl->state++;
6885 return( 0 );
6886 }
6887 }
6888#endif /* MBEDTLS_SSL_CLI_C */
6889#if defined(MBEDTLS_SSL_SRV_C)
6890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6891 {
6892 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6893 {
6894 /* Should never happen because we shouldn't have picked the
6895 * ciphersuite if we don't have a certificate. */
6896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6897 }
6898 }
6899#endif
6900
6901 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6902
6903 /*
6904 * 0 . 0 handshake type
6905 * 1 . 3 handshake length
6906 * 4 . 6 length of all certs
6907 * 7 . 9 length of cert. 1
6908 * 10 . n-1 peer certificate
6909 * n . n+2 length of cert. 2
6910 * n+3 . ... upper level cert, etc.
6911 */
6912 i = 7;
6913 crt = mbedtls_ssl_own_cert( ssl );
6914
6915 while( crt != NULL )
6916 {
6917 n = crt->raw.len;
6918 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6919 {
6920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6921 " > %" MBEDTLS_PRINTF_SIZET,
6922 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6923 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6924 }
6925
6926 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6927 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6928 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6929
6930 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6931 i += n; crt = crt->next;
6932 }
6933
6934 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6935 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6936 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6937
6938 ssl->out_msglen = i;
6939 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6940 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6941
6942 ssl->state++;
6943
6944 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6945 {
6946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6947 return( ret );
6948 }
6949
6950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6951
6952 return( ret );
6953}
6954
6955#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6956
6957#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006958MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006959static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6960 unsigned char *crt_buf,
6961 size_t crt_buf_len )
6962{
6963 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6964
6965 if( peer_crt == NULL )
6966 return( -1 );
6967
6968 if( peer_crt->raw.len != crt_buf_len )
6969 return( -1 );
6970
6971 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6972}
6973#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006974MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006975static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6976 unsigned char *crt_buf,
6977 size_t crt_buf_len )
6978{
6979 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6980 unsigned char const * const peer_cert_digest =
6981 ssl->session->peer_cert_digest;
6982 mbedtls_md_type_t const peer_cert_digest_type =
6983 ssl->session->peer_cert_digest_type;
6984 mbedtls_md_info_t const * const digest_info =
6985 mbedtls_md_info_from_type( peer_cert_digest_type );
6986 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6987 size_t digest_len;
6988
6989 if( peer_cert_digest == NULL || digest_info == NULL )
6990 return( -1 );
6991
6992 digest_len = mbedtls_md_get_size( digest_info );
6993 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6994 return( -1 );
6995
6996 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6997 if( ret != 0 )
6998 return( -1 );
6999
7000 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
7001}
7002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7003#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7004
7005/*
7006 * Once the certificate message is read, parse it into a cert chain and
7007 * perform basic checks, but leave actual verification to the caller
7008 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007009MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007010static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7011 mbedtls_x509_crt *chain )
7012{
7013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7014#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7015 int crt_cnt=0;
7016#endif
7017 size_t i, n;
7018 uint8_t alert;
7019
7020 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7021 {
7022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7023 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7024 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7025 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
7026 }
7027
7028 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
7029 {
7030 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7031 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7032 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
7033 }
7034
7035 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
7036 {
7037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7038 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7039 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7040 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7041 }
7042
7043 i = mbedtls_ssl_hs_hdr_len( ssl );
7044
7045 /*
7046 * Same message structure as in mbedtls_ssl_write_certificate()
7047 */
7048 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
7049
7050 if( ssl->in_msg[i] != 0 ||
7051 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
7052 {
7053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7055 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7056 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7057 }
7058
7059 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7060 i += 3;
7061
7062 /* Iterate through and parse the CRTs in the provided chain. */
7063 while( i < ssl->in_hslen )
7064 {
7065 /* Check that there's room for the next CRT's length fields. */
7066 if ( i + 3 > ssl->in_hslen ) {
7067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7068 mbedtls_ssl_send_alert_message( ssl,
7069 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7070 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7071 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7072 }
7073 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7074 * anything beyond 2**16 ~ 64K. */
7075 if( ssl->in_msg[i] != 0 )
7076 {
7077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7078 mbedtls_ssl_send_alert_message( ssl,
7079 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7080 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
7081 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
7082 }
7083
7084 /* Read length of the next CRT in the chain. */
7085 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
7086 | (unsigned int) ssl->in_msg[i + 2];
7087 i += 3;
7088
7089 if( n < 128 || i + n > ssl->in_hslen )
7090 {
7091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7092 mbedtls_ssl_send_alert_message( ssl,
7093 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7094 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7095 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7096 }
7097
7098 /* Check if we're handling the first CRT in the chain. */
7099#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7100 if( crt_cnt++ == 0 &&
7101 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
7102 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7103 {
7104 /* During client-side renegotiation, check that the server's
7105 * end-CRTs hasn't changed compared to the initial handshake,
7106 * mitigating the triple handshake attack. On success, reuse
7107 * the original end-CRT instead of parsing it again. */
7108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7109 if( ssl_check_peer_crt_unchanged( ssl,
7110 &ssl->in_msg[i],
7111 n ) != 0 )
7112 {
7113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
7114 mbedtls_ssl_send_alert_message( ssl,
7115 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7116 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
7117 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
7118 }
7119
7120 /* Now we can safely free the original chain. */
7121 ssl_clear_peer_cert( ssl->session );
7122 }
7123#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7124
7125 /* Parse the next certificate in the chain. */
7126#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7127 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
7128#else
7129 /* If we don't need to store the CRT chain permanently, parse
7130 * it in-place from the input buffer instead of making a copy. */
7131 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7133 switch( ret )
7134 {
7135 case 0: /*ok*/
7136 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7137 /* Ignore certificate with an unknown algorithm: maybe a
7138 prior certificate was already trusted. */
7139 break;
7140
7141 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7142 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7143 goto crt_parse_der_failed;
7144
7145 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7146 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7147 goto crt_parse_der_failed;
7148
7149 default:
7150 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7151 crt_parse_der_failed:
7152 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7153 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7154 return( ret );
7155 }
7156
7157 i += n;
7158 }
7159
7160 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7161 return( 0 );
7162}
7163
7164#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007165MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007166static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7167{
7168 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7169 return( -1 );
7170
7171 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7172 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7173 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7174 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7175 {
Ronald Cron19385882022-06-15 16:26:13 +02007176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007177 return( 0 );
7178 }
7179 return( -1 );
7180}
7181#endif /* MBEDTLS_SSL_SRV_C */
7182
7183/* Check if a certificate message is expected.
7184 * Return either
7185 * - SSL_CERTIFICATE_EXPECTED, or
7186 * - SSL_CERTIFICATE_SKIP
7187 * indicating whether a Certificate message is expected or not.
7188 */
7189#define SSL_CERTIFICATE_EXPECTED 0
7190#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007191MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007192static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7193 int authmode )
7194{
7195 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7196 ssl->handshake->ciphersuite_info;
7197
7198 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7199 return( SSL_CERTIFICATE_SKIP );
7200
7201#if defined(MBEDTLS_SSL_SRV_C)
7202 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7203 {
7204 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7205 return( SSL_CERTIFICATE_SKIP );
7206
7207 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7208 {
7209 ssl->session_negotiate->verify_result =
7210 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7211 return( SSL_CERTIFICATE_SKIP );
7212 }
7213 }
7214#else
7215 ((void) authmode);
7216#endif /* MBEDTLS_SSL_SRV_C */
7217
7218 return( SSL_CERTIFICATE_EXPECTED );
7219}
7220
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007221MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007222static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7223 int authmode,
7224 mbedtls_x509_crt *chain,
7225 void *rs_ctx )
7226{
7227 int ret = 0;
7228 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7229 ssl->handshake->ciphersuite_info;
7230 int have_ca_chain = 0;
7231
7232 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7233 void *p_vrfy;
7234
7235 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7236 return( 0 );
7237
7238 if( ssl->f_vrfy != NULL )
7239 {
7240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7241 f_vrfy = ssl->f_vrfy;
7242 p_vrfy = ssl->p_vrfy;
7243 }
7244 else
7245 {
7246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7247 f_vrfy = ssl->conf->f_vrfy;
7248 p_vrfy = ssl->conf->p_vrfy;
7249 }
7250
7251 /*
7252 * Main check: verify certificate
7253 */
7254#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7255 if( ssl->conf->f_ca_cb != NULL )
7256 {
7257 ((void) rs_ctx);
7258 have_ca_chain = 1;
7259
7260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7261 ret = mbedtls_x509_crt_verify_with_ca_cb(
7262 chain,
7263 ssl->conf->f_ca_cb,
7264 ssl->conf->p_ca_cb,
7265 ssl->conf->cert_profile,
7266 ssl->hostname,
7267 &ssl->session_negotiate->verify_result,
7268 f_vrfy, p_vrfy );
7269 }
7270 else
7271#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7272 {
7273 mbedtls_x509_crt *ca_chain;
7274 mbedtls_x509_crl *ca_crl;
7275
7276#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7277 if( ssl->handshake->sni_ca_chain != NULL )
7278 {
7279 ca_chain = ssl->handshake->sni_ca_chain;
7280 ca_crl = ssl->handshake->sni_ca_crl;
7281 }
7282 else
7283#endif
7284 {
7285 ca_chain = ssl->conf->ca_chain;
7286 ca_crl = ssl->conf->ca_crl;
7287 }
7288
7289 if( ca_chain != NULL )
7290 have_ca_chain = 1;
7291
7292 ret = mbedtls_x509_crt_verify_restartable(
7293 chain,
7294 ca_chain, ca_crl,
7295 ssl->conf->cert_profile,
7296 ssl->hostname,
7297 &ssl->session_negotiate->verify_result,
7298 f_vrfy, p_vrfy, rs_ctx );
7299 }
7300
7301 if( ret != 0 )
7302 {
7303 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7304 }
7305
7306#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7307 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7308 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7309#endif
7310
7311 /*
7312 * Secondary checks: always done, but change 'ret' only if it was 0
7313 */
7314
7315#if defined(MBEDTLS_ECP_C)
7316 {
7317 const mbedtls_pk_context *pk = &chain->pk;
7318
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007319 /* If certificate uses an EC key, make sure the curve is OK.
7320 * This is a public key, so it can't be opaque, so can_do() is a good
7321 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007322 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007323 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007324 /* and in the unlikely case the above assumption no longer holds
7325 * we are making sure that pk_ec() here does not return a NULL
7326 */
7327 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7328 if( ec == NULL )
7329 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7332 }
Jerry Yud9526692022-02-17 14:23:47 +08007333
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007334 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7335 {
7336 ssl->session_negotiate->verify_result |=
7337 MBEDTLS_X509_BADCERT_BAD_KEY;
7338
7339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7340 if( ret == 0 )
7341 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7342 }
Jerry Yud9526692022-02-17 14:23:47 +08007343 }
7344 }
7345#endif /* MBEDTLS_ECP_C */
7346
7347 if( mbedtls_ssl_check_cert_usage( chain,
7348 ciphersuite_info,
7349 ! ssl->conf->endpoint,
7350 &ssl->session_negotiate->verify_result ) != 0 )
7351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7353 if( ret == 0 )
7354 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7355 }
7356
7357 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7358 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7359 * with details encoded in the verification flags. All other kinds
7360 * of error codes, including those from the user provided f_vrfy
7361 * functions, are treated as fatal and lead to a failure of
7362 * ssl_parse_certificate even if verification was optional. */
7363 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7364 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7365 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7366 {
7367 ret = 0;
7368 }
7369
7370 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7371 {
7372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7373 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7374 }
7375
7376 if( ret != 0 )
7377 {
7378 uint8_t alert;
7379
7380 /* The certificate may have been rejected for several reasons.
7381 Pick one and send the corresponding alert. Which alert to send
7382 may be a subject of debate in some cases. */
7383 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7384 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7385 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7386 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7387 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7388 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7389 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7390 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7391 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7392 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7393 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7394 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7395 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7396 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7397 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7398 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7399 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7400 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7401 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7402 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7403 else
7404 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7405 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7406 alert );
7407 }
7408
7409#if defined(MBEDTLS_DEBUG_C)
7410 if( ssl->session_negotiate->verify_result != 0 )
7411 {
7412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7413 (unsigned int) ssl->session_negotiate->verify_result ) );
7414 }
7415 else
7416 {
7417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7418 }
7419#endif /* MBEDTLS_DEBUG_C */
7420
7421 return( ret );
7422}
7423
7424#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007425MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007426static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7427 unsigned char *start, size_t len )
7428{
7429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7430 /* Remember digest of the peer's end-CRT. */
7431 ssl->session_negotiate->peer_cert_digest =
7432 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7433 if( ssl->session_negotiate->peer_cert_digest == NULL )
7434 {
7435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7436 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7437 mbedtls_ssl_send_alert_message( ssl,
7438 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7439 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7440
7441 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7442 }
7443
7444 ret = mbedtls_md( mbedtls_md_info_from_type(
7445 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7446 start, len,
7447 ssl->session_negotiate->peer_cert_digest );
7448
7449 ssl->session_negotiate->peer_cert_digest_type =
7450 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7451 ssl->session_negotiate->peer_cert_digest_len =
7452 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7453
7454 return( ret );
7455}
7456
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007457MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007458static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7459 unsigned char *start, size_t len )
7460{
7461 unsigned char *end = start + len;
7462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7463
7464 /* Make a copy of the peer's raw public key. */
7465 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7466 ret = mbedtls_pk_parse_subpubkey( &start, end,
7467 &ssl->handshake->peer_pubkey );
7468 if( ret != 0 )
7469 {
7470 /* We should have parsed the public key before. */
7471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7472 }
7473
7474 return( 0 );
7475}
7476#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7477
7478int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7479{
7480 int ret = 0;
7481 int crt_expected;
7482#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7483 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7484 ? ssl->handshake->sni_authmode
7485 : ssl->conf->authmode;
7486#else
7487 const int authmode = ssl->conf->authmode;
7488#endif
7489 void *rs_ctx = NULL;
7490 mbedtls_x509_crt *chain = NULL;
7491
7492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7493
7494 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7495 if( crt_expected == SSL_CERTIFICATE_SKIP )
7496 {
7497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7498 goto exit;
7499 }
7500
7501#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7502 if( ssl->handshake->ecrs_enabled &&
7503 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7504 {
7505 chain = ssl->handshake->ecrs_peer_cert;
7506 ssl->handshake->ecrs_peer_cert = NULL;
7507 goto crt_verify;
7508 }
7509#endif
7510
7511 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7512 {
7513 /* mbedtls_ssl_read_record may have sent an alert already. We
7514 let it decide whether to alert. */
7515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7516 goto exit;
7517 }
7518
7519#if defined(MBEDTLS_SSL_SRV_C)
7520 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7521 {
7522 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7523
7524 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7525 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7526
7527 goto exit;
7528 }
7529#endif /* MBEDTLS_SSL_SRV_C */
7530
7531 /* Clear existing peer CRT structure in case we tried to
7532 * reuse a session but it failed, and allocate a new one. */
7533 ssl_clear_peer_cert( ssl->session_negotiate );
7534
7535 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7536 if( chain == NULL )
7537 {
7538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7539 sizeof( mbedtls_x509_crt ) ) );
7540 mbedtls_ssl_send_alert_message( ssl,
7541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7542 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7543
7544 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7545 goto exit;
7546 }
7547 mbedtls_x509_crt_init( chain );
7548
7549 ret = ssl_parse_certificate_chain( ssl, chain );
7550 if( ret != 0 )
7551 goto exit;
7552
7553#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7554 if( ssl->handshake->ecrs_enabled)
7555 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7556
7557crt_verify:
7558 if( ssl->handshake->ecrs_enabled)
7559 rs_ctx = &ssl->handshake->ecrs_ctx;
7560#endif
7561
7562 ret = ssl_parse_certificate_verify( ssl, authmode,
7563 chain, rs_ctx );
7564 if( ret != 0 )
7565 goto exit;
7566
7567#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7568 {
7569 unsigned char *crt_start, *pk_start;
7570 size_t crt_len, pk_len;
7571
7572 /* We parse the CRT chain without copying, so
7573 * these pointers point into the input buffer,
7574 * and are hence still valid after freeing the
7575 * CRT chain. */
7576
7577 crt_start = chain->raw.p;
7578 crt_len = chain->raw.len;
7579
7580 pk_start = chain->pk_raw.p;
7581 pk_len = chain->pk_raw.len;
7582
7583 /* Free the CRT structures before computing
7584 * digest and copying the peer's public key. */
7585 mbedtls_x509_crt_free( chain );
7586 mbedtls_free( chain );
7587 chain = NULL;
7588
7589 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7590 if( ret != 0 )
7591 goto exit;
7592
7593 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7594 if( ret != 0 )
7595 goto exit;
7596 }
7597#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7598 /* Pass ownership to session structure. */
7599 ssl->session_negotiate->peer_cert = chain;
7600 chain = NULL;
7601#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7602
7603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7604
7605exit:
7606
7607 if( ret == 0 )
7608 ssl->state++;
7609
7610#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7611 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7612 {
7613 ssl->handshake->ecrs_peer_cert = chain;
7614 chain = NULL;
7615 }
7616#endif
7617
7618 if( chain != NULL )
7619 {
7620 mbedtls_x509_crt_free( chain );
7621 mbedtls_free( chain );
7622 }
7623
7624 return( ret );
7625}
7626#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7627
Andrzej Kurek25f27152022-08-17 16:09:31 -04007628#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007629static void ssl_calc_finished_tls_sha256(
7630 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7631{
7632 int len = 12;
7633 const char *sender;
7634 unsigned char padbuf[32];
7635#if defined(MBEDTLS_USE_PSA_CRYPTO)
7636 size_t hash_size;
7637 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7638 psa_status_t status;
7639#else
7640 mbedtls_sha256_context sha256;
7641#endif
7642
7643 mbedtls_ssl_session *session = ssl->session_negotiate;
7644 if( !session )
7645 session = ssl->session;
7646
7647 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7648 ? "client finished"
7649 : "server finished";
7650
7651#if defined(MBEDTLS_USE_PSA_CRYPTO)
7652 sha256_psa = psa_hash_operation_init();
7653
7654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7655
7656 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7657 if( status != PSA_SUCCESS )
7658 {
7659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7660 return;
7661 }
7662
7663 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7664 if( status != PSA_SUCCESS )
7665 {
7666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7667 return;
7668 }
7669 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7670#else
7671
7672 mbedtls_sha256_init( &sha256 );
7673
7674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7675
7676 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7677
7678 /*
7679 * TLSv1.2:
7680 * hash = PRF( master, finished_label,
7681 * Hash( handshake ) )[0.11]
7682 */
7683
7684#if !defined(MBEDTLS_SHA256_ALT)
7685 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7686 sha256.state, sizeof( sha256.state ) );
7687#endif
7688
7689 mbedtls_sha256_finish( &sha256, padbuf );
7690 mbedtls_sha256_free( &sha256 );
7691#endif /* MBEDTLS_USE_PSA_CRYPTO */
7692
7693 ssl->handshake->tls_prf( session->master, 48, sender,
7694 padbuf, 32, buf, len );
7695
7696 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7697
7698 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7699
7700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7701}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007702#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007703
Jerry Yub7ba49e2022-02-17 14:25:53 +08007704
Andrzej Kurek25f27152022-08-17 16:09:31 -04007705#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007706static void ssl_calc_finished_tls_sha384(
7707 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7708{
7709 int len = 12;
7710 const char *sender;
7711 unsigned char padbuf[48];
7712#if defined(MBEDTLS_USE_PSA_CRYPTO)
7713 size_t hash_size;
7714 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7715 psa_status_t status;
7716#else
7717 mbedtls_sha512_context sha512;
7718#endif
7719
7720 mbedtls_ssl_session *session = ssl->session_negotiate;
7721 if( !session )
7722 session = ssl->session;
7723
7724 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7725 ? "client finished"
7726 : "server finished";
7727
7728#if defined(MBEDTLS_USE_PSA_CRYPTO)
7729 sha384_psa = psa_hash_operation_init();
7730
7731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7732
7733 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7734 if( status != PSA_SUCCESS )
7735 {
7736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7737 return;
7738 }
7739
7740 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7741 if( status != PSA_SUCCESS )
7742 {
7743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7744 return;
7745 }
7746 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7747#else
7748 mbedtls_sha512_init( &sha512 );
7749
7750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7751
Andrzej Kureka242e832022-08-11 10:03:14 -04007752 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007753
7754 /*
7755 * TLSv1.2:
7756 * hash = PRF( master, finished_label,
7757 * Hash( handshake ) )[0.11]
7758 */
7759
7760#if !defined(MBEDTLS_SHA512_ALT)
7761 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7762 sha512.state, sizeof( sha512.state ) );
7763#endif
7764 mbedtls_sha512_finish( &sha512, padbuf );
7765
7766 mbedtls_sha512_free( &sha512 );
7767#endif
7768
7769 ssl->handshake->tls_prf( session->master, 48, sender,
7770 padbuf, 48, buf, len );
7771
7772 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7773
7774 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7775
7776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7777}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007778#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007779
Jerry Yuaef00152022-02-17 14:27:31 +08007780void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7781{
7782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7783
7784 /*
7785 * Free our handshake params
7786 */
7787 mbedtls_ssl_handshake_free( ssl );
7788 mbedtls_free( ssl->handshake );
7789 ssl->handshake = NULL;
7790
7791 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007792 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007793 */
7794 if( ssl->transform )
7795 {
7796 mbedtls_ssl_transform_free( ssl->transform );
7797 mbedtls_free( ssl->transform );
7798 }
7799 ssl->transform = ssl->transform_negotiate;
7800 ssl->transform_negotiate = NULL;
7801
7802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7803}
7804
Jerry Yu2a9fff52022-02-17 14:28:51 +08007805void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7806{
7807 int resume = ssl->handshake->resume;
7808
7809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7810
7811#if defined(MBEDTLS_SSL_RENEGOTIATION)
7812 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7813 {
7814 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7815 ssl->renego_records_seen = 0;
7816 }
7817#endif
7818
7819 /*
7820 * Free the previous session and switch in the current one
7821 */
7822 if( ssl->session )
7823 {
7824#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7825 /* RFC 7366 3.1: keep the EtM state */
7826 ssl->session_negotiate->encrypt_then_mac =
7827 ssl->session->encrypt_then_mac;
7828#endif
7829
7830 mbedtls_ssl_session_free( ssl->session );
7831 mbedtls_free( ssl->session );
7832 }
7833 ssl->session = ssl->session_negotiate;
7834 ssl->session_negotiate = NULL;
7835
7836 /*
7837 * Add cache entry
7838 */
7839 if( ssl->conf->f_set_cache != NULL &&
7840 ssl->session->id_len != 0 &&
7841 resume == 0 )
7842 {
7843 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7844 ssl->session->id,
7845 ssl->session->id_len,
7846 ssl->session ) != 0 )
7847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7848 }
7849
7850#if defined(MBEDTLS_SSL_PROTO_DTLS)
7851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7852 ssl->handshake->flight != NULL )
7853 {
7854 /* Cancel handshake timer */
7855 mbedtls_ssl_set_timer( ssl, 0 );
7856
7857 /* Keep last flight around in case we need to resend it:
7858 * we need the handshake and transform structures for that */
7859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7860 }
7861 else
7862#endif
7863 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7864
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007865 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007866
7867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7868}
7869
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007870int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7871{
7872 int ret, hash_len;
7873
7874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7875
7876 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7877
7878 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7879
7880 /*
7881 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7882 * may define some other value. Currently (early 2016), no defined
7883 * ciphersuite does this (and this is unlikely to change as activity has
7884 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7885 */
7886 hash_len = 12;
7887
7888#if defined(MBEDTLS_SSL_RENEGOTIATION)
7889 ssl->verify_data_len = hash_len;
7890 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7891#endif
7892
7893 ssl->out_msglen = 4 + hash_len;
7894 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7895 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7896
7897 /*
7898 * In case of session resuming, invert the client and server
7899 * ChangeCipherSpec messages order.
7900 */
7901 if( ssl->handshake->resume != 0 )
7902 {
7903#if defined(MBEDTLS_SSL_CLI_C)
7904 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7905 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7906#endif
7907#if defined(MBEDTLS_SSL_SRV_C)
7908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7909 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7910#endif
7911 }
7912 else
7913 ssl->state++;
7914
7915 /*
7916 * Switch to our negotiated transform and session parameters for outbound
7917 * data.
7918 */
7919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7920
7921#if defined(MBEDTLS_SSL_PROTO_DTLS)
7922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7923 {
7924 unsigned char i;
7925
7926 /* Remember current epoch settings for resending */
7927 ssl->handshake->alt_transform_out = ssl->transform_out;
7928 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7929 sizeof( ssl->handshake->alt_out_ctr ) );
7930
7931 /* Set sequence_number to zero */
7932 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7933
7934
7935 /* Increment epoch */
7936 for( i = 2; i > 0; i-- )
7937 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7938 break;
7939
7940 /* The loop goes to its end iff the counter is wrapping */
7941 if( i == 0 )
7942 {
7943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7944 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7945 }
7946 }
7947 else
7948#endif /* MBEDTLS_SSL_PROTO_DTLS */
7949 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7950
7951 ssl->transform_out = ssl->transform_negotiate;
7952 ssl->session_out = ssl->session_negotiate;
7953
7954#if defined(MBEDTLS_SSL_PROTO_DTLS)
7955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7956 mbedtls_ssl_send_flight_completed( ssl );
7957#endif
7958
7959 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7960 {
7961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7962 return( ret );
7963 }
7964
7965#if defined(MBEDTLS_SSL_PROTO_DTLS)
7966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7967 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7968 {
7969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7970 return( ret );
7971 }
7972#endif
7973
7974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7975
7976 return( 0 );
7977}
7978
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007979#define SSL_MAX_HASH_LEN 12
7980
7981int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7982{
7983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7984 unsigned int hash_len = 12;
7985 unsigned char buf[SSL_MAX_HASH_LEN];
7986
7987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7988
7989 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7990
7991 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7992 {
7993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7994 goto exit;
7995 }
7996
7997 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7998 {
7999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
8000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8001 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8002 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8003 goto exit;
8004 }
8005
8006 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
8007 {
8008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8009 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8010 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8011 goto exit;
8012 }
8013
8014 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
8015 {
8016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
8017 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8018 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
8019 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8020 goto exit;
8021 }
8022
8023 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
8024 buf, hash_len ) != 0 )
8025 {
8026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
8027 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8028 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
8029 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8030 goto exit;
8031 }
8032
8033#if defined(MBEDTLS_SSL_RENEGOTIATION)
8034 ssl->verify_data_len = hash_len;
8035 memcpy( ssl->peer_verify_data, buf, hash_len );
8036#endif
8037
8038 if( ssl->handshake->resume != 0 )
8039 {
8040#if defined(MBEDTLS_SSL_CLI_C)
8041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8042 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
8043#endif
8044#if defined(MBEDTLS_SSL_SRV_C)
8045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8046 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
8047#endif
8048 }
8049 else
8050 ssl->state++;
8051
8052#if defined(MBEDTLS_SSL_PROTO_DTLS)
8053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8054 mbedtls_ssl_recv_flight_completed( ssl );
8055#endif
8056
8057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
8058
8059exit:
8060 mbedtls_platform_zeroize( buf, hash_len );
8061 return( ret );
8062}
8063
Jerry Yu392112c2022-02-17 14:34:10 +08008064#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8065/*
8066 * Helper to get TLS 1.2 PRF from ciphersuite
8067 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8068 */
8069static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
8070{
Jerry Yu392112c2022-02-17 14:34:10 +08008071 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04008072 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
8073#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07008074 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08008075 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04008076 else
Jerry Yu392112c2022-02-17 14:34:10 +08008077#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008078#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8079 {
8080 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
8081 return( tls_prf_sha256 );
8082 }
8083#endif
8084#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8085 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8086 (void) ciphersuite_info;
8087#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008088
Andrzej Kurek894edde2022-09-29 06:31:14 -04008089 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08008090}
8091#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008092
8093static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
8094{
8095 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008096#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008097 if( tls_prf == tls_prf_sha384 )
8098 {
8099 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
8100 }
8101 else
8102#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008103#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008104 if( tls_prf == tls_prf_sha256 )
8105 {
8106 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
8107 }
8108 else
8109#endif
8110 return( MBEDTLS_SSL_TLS_PRF_NONE );
8111}
8112
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008113/*
8114 * Populate a transform structure with session keys and all the other
8115 * necessary information.
8116 *
8117 * Parameters:
8118 * - [in/out]: transform: structure to populate
8119 * [in] must be just initialised with mbedtls_ssl_transform_init()
8120 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8121 * - [in] ciphersuite
8122 * - [in] master
8123 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008124 * - [in] tls_prf: pointer to PRF to use for key derivation
8125 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008126 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008127 * - [in] endpoint: client or server
8128 * - [in] ssl: used for:
8129 * - ssl->conf->{f,p}_export_keys
8130 * [in] optionally used for:
8131 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008133MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008134static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
8135 int ciphersuite,
8136 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008137#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008138 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008139#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140 ssl_tls_prf_t tls_prf,
8141 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04008142 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008143 unsigned endpoint,
8144 const mbedtls_ssl_context *ssl )
8145{
8146 int ret = 0;
8147 unsigned char keyblk[256];
8148 unsigned char *key1;
8149 unsigned char *key2;
8150 unsigned char *mac_enc;
8151 unsigned char *mac_dec;
8152 size_t mac_key_len = 0;
8153 size_t iv_copy_len;
8154 size_t keylen;
8155 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008156 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008157#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008158 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008159 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008160#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161
8162#if defined(MBEDTLS_USE_PSA_CRYPTO)
8163 psa_key_type_t key_type;
8164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8165 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008166 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008167 size_t key_bits;
8168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8169#endif
8170
8171#if !defined(MBEDTLS_DEBUG_C) && \
8172 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8173 if( ssl->f_export_keys == NULL )
8174 {
8175 ssl = NULL; /* make sure we don't use it except for these cases */
8176 (void) ssl;
8177 }
8178#endif
8179
8180 /*
8181 * Some data just needs copying into the structure
8182 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008183#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008184 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008185#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008186 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008187
8188#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8189 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8190#endif
8191
8192#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008193 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008194 {
8195 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8196 * generation separate. This should never happen. */
8197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8198 }
8199#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8200
8201 /*
8202 * Get various info structures
8203 */
8204 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8205 if( ciphersuite_info == NULL )
8206 {
8207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8208 ciphersuite ) );
8209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8210 }
8211
Neil Armstrongab555e02022-04-04 11:07:59 +02008212 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008213#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008214 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008215#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008216 ciphersuite_info );
8217
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008218 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8219 transform->taglen =
8220 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8221
8222#if defined(MBEDTLS_USE_PSA_CRYPTO)
8223 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8224 transform->taglen,
8225 &alg,
8226 &key_type,
8227 &key_bits ) ) != PSA_SUCCESS )
8228 {
8229 ret = psa_ssl_status_to_mbedtls( status );
8230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8231 goto end;
8232 }
8233#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008234 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8235 if( cipher_info == NULL )
8236 {
8237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8238 ciphersuite_info->cipher ) );
8239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8240 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008241#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242
Neil Armstronge4512952022-03-08 09:08:22 +01008243#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008244 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008245 if( mac_alg == 0 )
8246 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008248 (unsigned) ciphersuite_info->mac ) );
8249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8250 }
8251#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008252 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8253 if( md_info == NULL )
8254 {
8255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8256 (unsigned) ciphersuite_info->mac ) );
8257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8258 }
Neil Armstronge4512952022-03-08 09:08:22 +01008259#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260
8261#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8262 /* Copy own and peer's CID if the use of the CID
8263 * extension has been negotiated. */
8264 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8265 {
8266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8267
8268 transform->in_cid_len = ssl->own_cid_len;
8269 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8270 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8271 transform->in_cid_len );
8272
8273 transform->out_cid_len = ssl->handshake->peer_cid_len;
8274 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8275 ssl->handshake->peer_cid_len );
8276 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8277 transform->out_cid_len );
8278 }
8279#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8280
8281 /*
8282 * Compute key block using the PRF
8283 */
8284 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8285 if( ret != 0 )
8286 {
8287 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8288 return( ret );
8289 }
8290
8291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8292 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8293 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8294 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8295 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8296
8297 /*
8298 * Determine the appropriate key, IV and MAC length.
8299 */
8300
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008301#if defined(MBEDTLS_USE_PSA_CRYPTO)
8302 keylen = PSA_BITS_TO_BYTES(key_bits);
8303#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008304 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008305#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306
8307#if defined(MBEDTLS_GCM_C) || \
8308 defined(MBEDTLS_CCM_C) || \
8309 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008310 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311 {
8312 size_t explicit_ivlen;
8313
8314 transform->maclen = 0;
8315 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008316
8317 /* All modes haves 96-bit IVs, but the length of the static parts vary
8318 * with mode and version:
8319 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8320 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8321 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8322 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8323 * sequence number).
8324 */
8325 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008326
8327 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008328#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008329 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008330#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008331 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8332 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008333#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008334
8335 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008336 transform->fixed_ivlen = 12;
8337 else
8338 transform->fixed_ivlen = 4;
8339
8340 /* Minimum length of encrypted record */
8341 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8342 transform->minlen = explicit_ivlen + transform->taglen;
8343 }
8344 else
8345#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8346#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008347 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8348 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8349 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008350 {
Neil Armstronge4512952022-03-08 09:08:22 +01008351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008352 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008353#else
8354 size_t block_size = cipher_info->block_size;
8355#endif /* MBEDTLS_USE_PSA_CRYPTO */
8356
8357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008358 /* Get MAC length */
8359 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8360#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008361 /* Initialize HMAC contexts */
8362 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8363 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8364 {
8365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8366 goto end;
8367 }
8368
8369 /* Get MAC length */
8370 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008371#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008372 transform->maclen = mac_key_len;
8373
8374 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008375#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008376 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008377#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008378 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008379#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008380
8381 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008382 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008383 transform->minlen = transform->maclen;
8384 else
8385 {
8386 /*
8387 * GenericBlockCipher:
8388 * 1. if EtM is in use: one block plus MAC
8389 * otherwise: * first multiple of blocklen greater than maclen
8390 * 2. IV
8391 */
8392#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008393 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008394 {
8395 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008396 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008397 }
8398 else
8399#endif
8400 {
8401 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008402 + block_size
8403 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008404 }
8405
Glenn Strauss07c64162022-03-14 12:34:51 -04008406 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407 {
8408 transform->minlen += transform->ivlen;
8409 }
8410 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008411 {
8412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8413 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8414 goto end;
8415 }
8416 }
8417 }
8418 else
8419#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8420 {
8421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8423 }
8424
8425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8426 (unsigned) keylen,
8427 (unsigned) transform->minlen,
8428 (unsigned) transform->ivlen,
8429 (unsigned) transform->maclen ) );
8430
8431 /*
8432 * Finally setup the cipher contexts, IVs and MAC secrets.
8433 */
8434#if defined(MBEDTLS_SSL_CLI_C)
8435 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8436 {
8437 key1 = keyblk + mac_key_len * 2;
8438 key2 = keyblk + mac_key_len * 2 + keylen;
8439
8440 mac_enc = keyblk;
8441 mac_dec = keyblk + mac_key_len;
8442
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443 iv_copy_len = ( transform->fixed_ivlen ) ?
8444 transform->fixed_ivlen : transform->ivlen;
8445 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8446 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8447 iv_copy_len );
8448 }
8449 else
8450#endif /* MBEDTLS_SSL_CLI_C */
8451#if defined(MBEDTLS_SSL_SRV_C)
8452 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8453 {
8454 key1 = keyblk + mac_key_len * 2 + keylen;
8455 key2 = keyblk + mac_key_len * 2;
8456
8457 mac_enc = keyblk + mac_key_len;
8458 mac_dec = keyblk;
8459
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460 iv_copy_len = ( transform->fixed_ivlen ) ?
8461 transform->fixed_ivlen : transform->ivlen;
8462 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8463 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8464 iv_copy_len );
8465 }
8466 else
8467#endif /* MBEDTLS_SSL_SRV_C */
8468 {
8469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8470 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8471 goto end;
8472 }
8473
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008474 if( ssl != NULL && ssl->f_export_keys != NULL )
8475 {
8476 ssl->f_export_keys( ssl->p_export_keys,
8477 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8478 master, 48,
8479 randbytes + 32,
8480 randbytes,
8481 tls_prf_get_type( tls_prf ) );
8482 }
8483
8484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008485 transform->psa_alg = alg;
8486
8487 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8488 {
8489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8490 psa_set_key_algorithm( &attributes, alg );
8491 psa_set_key_type( &attributes, key_type );
8492
8493 if( ( status = psa_import_key( &attributes,
8494 key1,
8495 PSA_BITS_TO_BYTES( key_bits ),
8496 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8497 {
8498 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8499 ret = psa_ssl_status_to_mbedtls( status );
8500 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8501 goto end;
8502 }
8503
8504 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8505
8506 if( ( status = psa_import_key( &attributes,
8507 key2,
8508 PSA_BITS_TO_BYTES( key_bits ),
8509 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8510 {
8511 ret = psa_ssl_status_to_mbedtls( status );
8512 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8513 goto end;
8514 }
8515 }
8516#else
8517 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8518 cipher_info ) ) != 0 )
8519 {
8520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8521 goto end;
8522 }
8523
8524 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8525 cipher_info ) ) != 0 )
8526 {
8527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8528 goto end;
8529 }
8530
8531 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8532 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8533 MBEDTLS_ENCRYPT ) ) != 0 )
8534 {
8535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8536 goto end;
8537 }
8538
8539 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8540 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8541 MBEDTLS_DECRYPT ) ) != 0 )
8542 {
8543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8544 goto end;
8545 }
8546
8547#if defined(MBEDTLS_CIPHER_MODE_CBC)
8548 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8549 {
8550 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8551 MBEDTLS_PADDING_NONE ) ) != 0 )
8552 {
8553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8554 goto end;
8555 }
8556
8557 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8558 MBEDTLS_PADDING_NONE ) ) != 0 )
8559 {
8560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8561 goto end;
8562 }
8563 }
8564#endif /* MBEDTLS_CIPHER_MODE_CBC */
8565#endif /* MBEDTLS_USE_PSA_CRYPTO */
8566
Neil Armstrong29c0c042022-03-17 17:47:28 +01008567#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8568 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8569 For AEAD-based ciphersuites, there is nothing to do here. */
8570 if( mac_key_len != 0 )
8571 {
8572#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008573 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008574
8575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008576 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008577 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8578
8579 if( ( status = psa_import_key( &attributes,
8580 mac_enc, mac_key_len,
8581 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8582 {
8583 ret = psa_ssl_status_to_mbedtls( status );
8584 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8585 goto end;
8586 }
8587
Ronald Cronfb39f152022-03-25 14:36:28 +01008588 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008589 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8590#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8591 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8592#endif
8593 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008594 /* mbedtls_ct_hmac() requires the key to be exportable */
8595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8596 PSA_KEY_USAGE_VERIFY_HASH );
8597 else
8598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8599
8600 if( ( status = psa_import_key( &attributes,
8601 mac_dec, mac_key_len,
8602 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8603 {
8604 ret = psa_ssl_status_to_mbedtls( status );
8605 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8606 goto end;
8607 }
8608#else
8609 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8610 if( ret != 0 )
8611 goto end;
8612 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8613 if( ret != 0 )
8614 goto end;
8615#endif /* MBEDTLS_USE_PSA_CRYPTO */
8616 }
8617#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8618
8619 ((void) mac_dec);
8620 ((void) mac_enc);
8621
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008622end:
8623 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8624 return( ret );
8625}
8626
Valerio Settia08b1a42022-11-17 15:10:02 +01008627#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8628 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008629int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008630 psa_pake_operation_t *pake_ctx,
8631 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008632 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008633{
8634 psa_status_t status;
8635 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008636 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008637 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8638 * At round two perform a single cycle
8639 */
8640 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008641
Valerio Setti6b3dab02022-11-17 17:14:54 +01008642 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008643 {
8644 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8645 step <= PSA_PAKE_STEP_ZK_PROOF;
8646 ++step )
8647 {
8648 /* Length is stored at the first byte */
8649 size_t length = buf[input_offset];
8650 input_offset += 1;
8651
8652 if( input_offset + length > len )
8653 {
8654 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8655 }
8656
8657 status = psa_pake_input( pake_ctx, step,
8658 buf + input_offset, length );
8659 if( status != PSA_SUCCESS)
8660 {
8661 return psa_ssl_status_to_mbedtls( status );
8662 }
8663
8664 input_offset += length;
8665 }
8666 }
8667
Valerio Setti819de862022-11-17 18:05:19 +01008668 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008669 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008670
Valerio Settia08b1a42022-11-17 15:10:02 +01008671 return( 0 );
8672}
8673
Valerio Setti6b3dab02022-11-17 17:14:54 +01008674int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008675 psa_pake_operation_t *pake_ctx,
8676 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008677 size_t len, size_t *olen,
8678 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008679{
8680 psa_status_t status;
8681 size_t output_offset = 0;
8682 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008683 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008684 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8685 * At round two perform a single cycle
8686 */
8687 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008688
Valerio Setti6b3dab02022-11-17 17:14:54 +01008689 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008690 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008691 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8692 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008693 ++step )
8694 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008695 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008696 * For each step, prepend 1 byte with the length of the data as
8697 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008698 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008699 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008700 buf + output_offset + 1,
8701 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008702 &output_len );
8703 if( status != PSA_SUCCESS )
8704 {
8705 return( psa_ssl_status_to_mbedtls( status ) );
8706 }
8707
Valerio Setti99d88c12022-11-22 16:03:43 +01008708 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008709
8710 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008711 }
8712 }
8713
8714 *olen = output_offset;
8715
8716 return( 0 );
8717}
Valerio Settia08b1a42022-11-17 15:10:02 +01008718#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8719
Jerry Yuee40f9d2022-02-17 14:55:16 +08008720#if defined(MBEDTLS_USE_PSA_CRYPTO)
8721int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8722 unsigned char *hash, size_t *hashlen,
8723 unsigned char *data, size_t data_len,
8724 mbedtls_md_type_t md_alg )
8725{
8726 psa_status_t status;
8727 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008728 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008729
8730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8731
8732 if( ( status = psa_hash_setup( &hash_operation,
8733 hash_alg ) ) != PSA_SUCCESS )
8734 {
8735 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8736 goto exit;
8737 }
8738
8739 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8740 64 ) ) != PSA_SUCCESS )
8741 {
8742 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8743 goto exit;
8744 }
8745
8746 if( ( status = psa_hash_update( &hash_operation,
8747 data, data_len ) ) != PSA_SUCCESS )
8748 {
8749 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8750 goto exit;
8751 }
8752
8753 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8754 hashlen ) ) != PSA_SUCCESS )
8755 {
8756 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8757 goto exit;
8758 }
8759
8760exit:
8761 if( status != PSA_SUCCESS )
8762 {
8763 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8764 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8765 switch( status )
8766 {
8767 case PSA_ERROR_NOT_SUPPORTED:
8768 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8769 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8770 case PSA_ERROR_BUFFER_TOO_SMALL:
8771 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8772 case PSA_ERROR_INSUFFICIENT_MEMORY:
8773 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8774 default:
8775 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8776 }
8777 }
8778 return( 0 );
8779}
8780
8781#else
8782
8783int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8784 unsigned char *hash, size_t *hashlen,
8785 unsigned char *data, size_t data_len,
8786 mbedtls_md_type_t md_alg )
8787{
8788 int ret = 0;
8789 mbedtls_md_context_t ctx;
8790 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8791 *hashlen = mbedtls_md_get_size( md_info );
8792
8793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8794
8795 mbedtls_md_init( &ctx );
8796
8797 /*
8798 * digitally-signed struct {
8799 * opaque client_random[32];
8800 * opaque server_random[32];
8801 * ServerDHParams params;
8802 * };
8803 */
8804 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8805 {
8806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8807 goto exit;
8808 }
8809 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8810 {
8811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8812 goto exit;
8813 }
8814 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8815 {
8816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8817 goto exit;
8818 }
8819 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8820 {
8821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8822 goto exit;
8823 }
8824 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8825 {
8826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8827 goto exit;
8828 }
8829
8830exit:
8831 mbedtls_md_free( &ctx );
8832
8833 if( ret != 0 )
8834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8835 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8836
8837 return( ret );
8838}
8839#endif /* MBEDTLS_USE_PSA_CRYPTO */
8840
Jerry Yud9d91da2022-02-17 14:57:06 +08008841#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8842
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008843/* Find the preferred hash for a given signature algorithm. */
8844unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8845 mbedtls_ssl_context *ssl,
8846 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008847{
Gabor Mezei078e8032022-04-27 21:17:56 +02008848 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008849 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008850
8851 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008852 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008853
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008854 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008855 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008856 unsigned int hash_alg_received =
8857 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8858 received_sig_algs[i] );
8859 unsigned int sig_alg_received =
8860 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8861 received_sig_algs[i] );
8862
8863 if( sig_alg == sig_alg_received )
8864 {
8865#if defined(MBEDTLS_USE_PSA_CRYPTO)
8866 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8867 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008868 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008869 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008870
Neil Armstrong96eceb82022-06-30 18:05:05 +02008871 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8872 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8873 PSA_ALG_ECDSA( psa_hash_alg ),
8874 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008875 continue;
8876
Neil Armstrong96eceb82022-06-30 18:05:05 +02008877 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008878 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8879 PSA_ALG_RSA_PKCS1V15_SIGN(
8880 psa_hash_alg ),
8881 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008882 continue;
8883 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008884#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008885
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008886 return( hash_alg_received );
8887 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008888 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008889
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008890 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008891}
8892
8893#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8894
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895/* Serialization of TLS 1.2 sessions:
8896 *
8897 * struct {
8898 * uint64 start_time;
8899 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008900 * uint8 session_id_len; // at most 32
8901 * opaque session_id[32];
8902 * opaque master[48]; // fixed length in the standard
8903 * uint32 verify_result;
8904 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8905 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8906 * uint32 ticket_lifetime;
8907 * uint8 mfl_code; // up to 255 according to standard
8908 * uint8 encrypt_then_mac; // 0 or 1
8909 * } serialized_session_tls12;
8910 *
8911 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008912static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008913 unsigned char *buf,
8914 size_t buf_len )
8915{
8916 unsigned char *p = buf;
8917 size_t used = 0;
8918
8919#if defined(MBEDTLS_HAVE_TIME)
8920 uint64_t start;
8921#endif
8922#if defined(MBEDTLS_X509_CRT_PARSE_C)
8923#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8924 size_t cert_len;
8925#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8926#endif /* MBEDTLS_X509_CRT_PARSE_C */
8927
8928 /*
8929 * Time
8930 */
8931#if defined(MBEDTLS_HAVE_TIME)
8932 used += 8;
8933
8934 if( used <= buf_len )
8935 {
8936 start = (uint64_t) session->start;
8937
8938 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8939 p += 8;
8940 }
8941#endif /* MBEDTLS_HAVE_TIME */
8942
8943 /*
8944 * Basic mandatory fields
8945 */
8946 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008947 + 1 /* id_len */
8948 + sizeof( session->id )
8949 + sizeof( session->master )
8950 + 4; /* verify_result */
8951
8952 if( used <= buf_len )
8953 {
8954 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8955 p += 2;
8956
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 *p++ = MBEDTLS_BYTE_0( session->id_len );
8958 memcpy( p, session->id, 32 );
8959 p += 32;
8960
8961 memcpy( p, session->master, 48 );
8962 p += 48;
8963
8964 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8965 p += 4;
8966 }
8967
8968 /*
8969 * Peer's end-entity certificate
8970 */
8971#if defined(MBEDTLS_X509_CRT_PARSE_C)
8972#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8973 if( session->peer_cert == NULL )
8974 cert_len = 0;
8975 else
8976 cert_len = session->peer_cert->raw.len;
8977
8978 used += 3 + cert_len;
8979
8980 if( used <= buf_len )
8981 {
8982 *p++ = MBEDTLS_BYTE_2( cert_len );
8983 *p++ = MBEDTLS_BYTE_1( cert_len );
8984 *p++ = MBEDTLS_BYTE_0( cert_len );
8985
8986 if( session->peer_cert != NULL )
8987 {
8988 memcpy( p, session->peer_cert->raw.p, cert_len );
8989 p += cert_len;
8990 }
8991 }
8992#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8993 if( session->peer_cert_digest != NULL )
8994 {
8995 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8996 if( used <= buf_len )
8997 {
8998 *p++ = (unsigned char) session->peer_cert_digest_type;
8999 *p++ = (unsigned char) session->peer_cert_digest_len;
9000 memcpy( p, session->peer_cert_digest,
9001 session->peer_cert_digest_len );
9002 p += session->peer_cert_digest_len;
9003 }
9004 }
9005 else
9006 {
9007 used += 2;
9008 if( used <= buf_len )
9009 {
9010 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9011 *p++ = 0;
9012 }
9013 }
9014#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9015#endif /* MBEDTLS_X509_CRT_PARSE_C */
9016
9017 /*
9018 * Session ticket if any, plus associated data
9019 */
9020#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9021 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9022
9023 if( used <= buf_len )
9024 {
9025 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
9026 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
9027 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
9028
9029 if( session->ticket != NULL )
9030 {
9031 memcpy( p, session->ticket, session->ticket_len );
9032 p += session->ticket_len;
9033 }
9034
9035 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
9036 p += 4;
9037 }
9038#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9039
9040 /*
9041 * Misc extension-related info
9042 */
9043#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9044 used += 1;
9045
9046 if( used <= buf_len )
9047 *p++ = session->mfl_code;
9048#endif
9049
9050#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9051 used += 1;
9052
9053 if( used <= buf_len )
9054 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
9055#endif
9056
9057 return( used );
9058}
9059
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009060MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00009061static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062 const unsigned char *buf,
9063 size_t len )
9064{
9065#if defined(MBEDTLS_HAVE_TIME)
9066 uint64_t start;
9067#endif
9068#if defined(MBEDTLS_X509_CRT_PARSE_C)
9069#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9070 size_t cert_len;
9071#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9072#endif /* MBEDTLS_X509_CRT_PARSE_C */
9073
9074 const unsigned char *p = buf;
9075 const unsigned char * const end = buf + len;
9076
9077 /*
9078 * Time
9079 */
9080#if defined(MBEDTLS_HAVE_TIME)
9081 if( 8 > (size_t)( end - p ) )
9082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9083
9084 start = ( (uint64_t) p[0] << 56 ) |
9085 ( (uint64_t) p[1] << 48 ) |
9086 ( (uint64_t) p[2] << 40 ) |
9087 ( (uint64_t) p[3] << 32 ) |
9088 ( (uint64_t) p[4] << 24 ) |
9089 ( (uint64_t) p[5] << 16 ) |
9090 ( (uint64_t) p[6] << 8 ) |
9091 ( (uint64_t) p[7] );
9092 p += 8;
9093
9094 session->start = (time_t) start;
9095#endif /* MBEDTLS_HAVE_TIME */
9096
9097 /*
9098 * Basic mandatory fields
9099 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01009100 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9102
9103 session->ciphersuite = ( p[0] << 8 ) | p[1];
9104 p += 2;
9105
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106 session->id_len = *p++;
9107 memcpy( session->id, p, 32 );
9108 p += 32;
9109
9110 memcpy( session->master, p, 48 );
9111 p += 48;
9112
9113 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9114 ( (uint32_t) p[1] << 16 ) |
9115 ( (uint32_t) p[2] << 8 ) |
9116 ( (uint32_t) p[3] );
9117 p += 4;
9118
9119 /* Immediately clear invalid pointer values that have been read, in case
9120 * we exit early before we replaced them with valid ones. */
9121#if defined(MBEDTLS_X509_CRT_PARSE_C)
9122#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9123 session->peer_cert = NULL;
9124#else
9125 session->peer_cert_digest = NULL;
9126#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9127#endif /* MBEDTLS_X509_CRT_PARSE_C */
9128#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9129 session->ticket = NULL;
9130#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9131
9132 /*
9133 * Peer certificate
9134 */
9135#if defined(MBEDTLS_X509_CRT_PARSE_C)
9136#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9137 /* Deserialize CRT from the end of the ticket. */
9138 if( 3 > (size_t)( end - p ) )
9139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9140
9141 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9142 p += 3;
9143
9144 if( cert_len != 0 )
9145 {
9146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9147
9148 if( cert_len > (size_t)( end - p ) )
9149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9150
9151 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9152
9153 if( session->peer_cert == NULL )
9154 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9155
9156 mbedtls_x509_crt_init( session->peer_cert );
9157
9158 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9159 p, cert_len ) ) != 0 )
9160 {
9161 mbedtls_x509_crt_free( session->peer_cert );
9162 mbedtls_free( session->peer_cert );
9163 session->peer_cert = NULL;
9164 return( ret );
9165 }
9166
9167 p += cert_len;
9168 }
9169#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9170 /* Deserialize CRT digest from the end of the ticket. */
9171 if( 2 > (size_t)( end - p ) )
9172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9173
9174 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9175 session->peer_cert_digest_len = (size_t) *p++;
9176
9177 if( session->peer_cert_digest_len != 0 )
9178 {
9179 const mbedtls_md_info_t *md_info =
9180 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9181 if( md_info == NULL )
9182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9183 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9185
9186 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9188
9189 session->peer_cert_digest =
9190 mbedtls_calloc( 1, session->peer_cert_digest_len );
9191 if( session->peer_cert_digest == NULL )
9192 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9193
9194 memcpy( session->peer_cert_digest, p,
9195 session->peer_cert_digest_len );
9196 p += session->peer_cert_digest_len;
9197 }
9198#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9199#endif /* MBEDTLS_X509_CRT_PARSE_C */
9200
9201 /*
9202 * Session ticket and associated data
9203 */
9204#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9205 if( 3 > (size_t)( end - p ) )
9206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9207
9208 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9209 p += 3;
9210
9211 if( session->ticket_len != 0 )
9212 {
9213 if( session->ticket_len > (size_t)( end - p ) )
9214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9215
9216 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9217 if( session->ticket == NULL )
9218 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9219
9220 memcpy( session->ticket, p, session->ticket_len );
9221 p += session->ticket_len;
9222 }
9223
9224 if( 4 > (size_t)( end - p ) )
9225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9226
9227 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9228 ( (uint32_t) p[1] << 16 ) |
9229 ( (uint32_t) p[2] << 8 ) |
9230 ( (uint32_t) p[3] );
9231 p += 4;
9232#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9233
9234 /*
9235 * Misc extension-related info
9236 */
9237#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9238 if( 1 > (size_t)( end - p ) )
9239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9240
9241 session->mfl_code = *p++;
9242#endif
9243
9244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9245 if( 1 > (size_t)( end - p ) )
9246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9247
9248 session->encrypt_then_mac = *p++;
9249#endif
9250
9251 /* Done, should have consumed entire buffer */
9252 if( p != end )
9253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9254
9255 return( 0 );
9256}
Jerry Yudc7bd172022-02-17 13:44:15 +08009257#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9258
XiaokangQian75d40ef2022-04-20 11:05:24 +00009259int mbedtls_ssl_validate_ciphersuite(
9260 const mbedtls_ssl_context *ssl,
9261 const mbedtls_ssl_ciphersuite_t *suite_info,
9262 mbedtls_ssl_protocol_version min_tls_version,
9263 mbedtls_ssl_protocol_version max_tls_version )
9264{
9265 (void) ssl;
9266
9267 if( suite_info == NULL )
9268 return( -1 );
9269
9270 if( ( suite_info->min_tls_version > max_tls_version ) ||
9271 ( suite_info->max_tls_version < min_tls_version ) )
9272 {
9273 return( -1 );
9274 }
9275
XiaokangQian060d8672022-04-21 09:24:56 +00009276#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009277#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009278#if defined(MBEDTLS_USE_PSA_CRYPTO)
9279 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9280 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9281#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009282 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9283 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009284#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009285 {
9286 return( -1 );
9287 }
9288#endif
9289
9290 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9291#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9292 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9293 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9294 {
9295 return( -1 );
9296 }
9297#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9298#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9299
9300 return( 0 );
9301}
9302
Ronald Crone68ab4f2022-10-05 12:46:29 +02009303#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009304/*
9305 * Function for writing a signature algorithm extension.
9306 *
9307 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9308 * value (TLS 1.3 RFC8446):
9309 * enum {
9310 * ....
9311 * ecdsa_secp256r1_sha256( 0x0403 ),
9312 * ecdsa_secp384r1_sha384( 0x0503 ),
9313 * ecdsa_secp521r1_sha512( 0x0603 ),
9314 * ....
9315 * } SignatureScheme;
9316 *
9317 * struct {
9318 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9319 * } SignatureSchemeList;
9320 *
9321 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9322 * value (TLS 1.2 RFC5246):
9323 * enum {
9324 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9325 * sha512(6), (255)
9326 * } HashAlgorithm;
9327 *
9328 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9329 * SignatureAlgorithm;
9330 *
9331 * struct {
9332 * HashAlgorithm hash;
9333 * SignatureAlgorithm signature;
9334 * } SignatureAndHashAlgorithm;
9335 *
9336 * SignatureAndHashAlgorithm
9337 * supported_signature_algorithms<2..2^16-2>;
9338 *
9339 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9340 * generalization of the TLS 1.2 signature algorithm extension.
9341 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9342 * `SignatureScheme` field of TLS 1.3
9343 *
9344 */
9345int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9346 const unsigned char *end, size_t *out_len )
9347{
9348 unsigned char *p = buf;
9349 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9350 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9351
9352 *out_len = 0;
9353
9354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9355
9356 /* Check if we have space for header and length field:
9357 * - extension_type (2 bytes)
9358 * - extension_data_length (2 bytes)
9359 * - supported_signature_algorithms_length (2 bytes)
9360 */
9361 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9362 p += 6;
9363
9364 /*
9365 * Write supported_signature_algorithms
9366 */
9367 supported_sig_alg = p;
9368 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9369 if( sig_alg == NULL )
9370 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9371
9372 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9373 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009374 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9375 *sig_alg,
9376 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009377 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9378 continue;
9379 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9380 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9381 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009383 *sig_alg,
9384 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009385 }
9386
9387 /* Length of supported_signature_algorithms */
9388 supported_sig_alg_len = p - supported_sig_alg;
9389 if( supported_sig_alg_len == 0 )
9390 {
9391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9393 }
9394
XiaokangQianeaf36512022-04-24 09:07:44 +00009395 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009396 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009397 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9398
XiaokangQianeaf36512022-04-24 09:07:44 +00009399 *out_len = p - buf;
9400
9401#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009402 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009403#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009404
XiaokangQianeaf36512022-04-24 09:07:44 +00009405 return( 0 );
9406}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009407#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009408
XiaokangQian40a35232022-05-07 09:02:40 +00009409#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009410/*
9411 * mbedtls_ssl_parse_server_name_ext
9412 *
9413 * Structure of server_name extension:
9414 *
9415 * enum {
9416 * host_name(0), (255)
9417 * } NameType;
9418 * opaque HostName<1..2^16-1>;
9419 *
9420 * struct {
9421 * NameType name_type;
9422 * select (name_type) {
9423 * case host_name: HostName;
9424 * } name;
9425 * } ServerName;
9426 * struct {
9427 * ServerName server_name_list<1..2^16-1>
9428 * } ServerNameList;
9429 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009430MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009431int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9432 const unsigned char *buf,
9433 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009434{
9435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9436 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009437 size_t server_name_list_len, hostname_len;
9438 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009439
XiaokangQianf2a94202022-05-20 06:44:24 +00009440 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009441
9442 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009443 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009444 p += 2;
9445
XiaokangQian9b2b7712022-05-17 02:57:00 +00009446 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9447 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009448 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009449 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009450 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009451 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009452 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9453 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009454
9455 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9456 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009457 /* sni_name is intended to be used only during the parsing of the
9458 * ClientHello message (it is reset to NULL before the end of
9459 * the message parsing). Thus it is ok to just point to the
9460 * reception buffer and not make a copy of it.
9461 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009462 ssl->handshake->sni_name = p + 3;
9463 ssl->handshake->sni_name_len = hostname_len;
9464 if( ssl->conf->f_sni == NULL )
9465 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009466 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009467 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009468 if( ret != 0 )
9469 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009470 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009471 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9472 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009473 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9474 }
9475 return( 0 );
9476 }
9477
9478 p += hostname_len + 3;
9479 }
9480
9481 return( 0 );
9482}
9483#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9484
XiaokangQianacb39922022-06-17 10:18:48 +00009485#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009486MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009487int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9488 const unsigned char *buf,
9489 const unsigned char *end )
9490{
9491 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009492 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009493 const unsigned char *protocol_name_list;
9494 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009495 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009496
9497 /* If ALPN not configured, just ignore the extension */
9498 if( ssl->conf->alpn_list == NULL )
9499 return( 0 );
9500
9501 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009502 * RFC7301, section 3.1
9503 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009504 *
XiaokangQianc7403452022-06-23 03:24:12 +00009505 * struct {
9506 * ProtocolName protocol_name_list<2..2^16-1>
9507 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009508 */
9509
XiaokangQianc7403452022-06-23 03:24:12 +00009510 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009511 * protocol_name_list_len 2 bytes
9512 * protocol_name_len 1 bytes
9513 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009514 */
XiaokangQianacb39922022-06-17 10:18:48 +00009515 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9516
XiaokangQianc7403452022-06-23 03:24:12 +00009517 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009518 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009519 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009520 protocol_name_list = p;
9521 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009522
9523 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009524 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009525 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009526 protocol_name_len = *p++;
9527 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9528 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009529 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009530 {
9531 MBEDTLS_SSL_PEND_FATAL_ALERT(
9532 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9533 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009534 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009535 }
9536
9537 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009538 }
9539
9540 /* Use our order of preference */
9541 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9542 {
9543 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009544 p = protocol_name_list;
9545 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009546 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009547 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009548 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009549 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009550 {
9551 ssl->alpn_chosen = *alpn;
9552 return( 0 );
9553 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009554
9555 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009556 }
9557 }
9558
XiaokangQian95d5f542022-06-24 02:29:26 +00009559 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009560 MBEDTLS_SSL_PEND_FATAL_ALERT(
9561 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9562 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9563 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9564}
9565
9566int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9567 unsigned char *buf,
9568 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009569 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009570{
9571 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009572 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009573 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009574
9575 if( ssl->alpn_chosen == NULL )
9576 {
9577 return( 0 );
9578 }
9579
XiaokangQian95d5f542022-06-24 02:29:26 +00009580 protocol_name_len = strlen( ssl->alpn_chosen );
9581 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009582
9583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9584 /*
9585 * 0 . 1 ext identifier
9586 * 2 . 3 ext length
9587 * 4 . 5 protocol list length
9588 * 6 . 6 protocol name length
9589 * 7 . 7+n protocol name
9590 */
9591 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9592
XiaokangQian95d5f542022-06-24 02:29:26 +00009593 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009594
XiaokangQian95d5f542022-06-24 02:29:26 +00009595 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9596 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009597 /* Note: the length of the chosen protocol has been checked to be less
9598 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9599 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009600 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009601
XiaokangQian95d5f542022-06-24 02:29:26 +00009602 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009603
9604#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9605 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9606#endif
9607
XiaokangQianacb39922022-06-17 10:18:48 +00009608 return ( 0 );
9609}
9610#endif /* MBEDTLS_SSL_ALPN */
9611
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009612#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009613 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009614 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009615 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009616int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9617 const char *hostname )
9618{
9619 /* Initialize to suppress unnecessary compiler warning */
9620 size_t hostname_len = 0;
9621
9622 /* Check if new hostname is valid before
9623 * making any change to current one */
9624 if( hostname != NULL )
9625 {
9626 hostname_len = strlen( hostname );
9627
9628 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9630 }
9631
9632 /* Now it's clear that we will overwrite the old hostname,
9633 * so we can free it safely */
9634 if( session->hostname != NULL )
9635 {
9636 mbedtls_platform_zeroize( session->hostname,
9637 strlen( session->hostname ) );
9638 mbedtls_free( session->hostname );
9639 }
9640
9641 /* Passing NULL as hostname shall clear the old one */
9642 if( hostname == NULL )
9643 {
9644 session->hostname = NULL;
9645 }
9646 else
9647 {
9648 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9649 if( session->hostname == NULL )
9650 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9651
9652 memcpy( session->hostname, hostname, hostname_len );
9653 }
9654
9655 return( 0 );
9656}
Xiaokang Qian03409292022-10-12 02:49:52 +00009657#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9658 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009659 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009660 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662#endif /* MBEDTLS_SSL_TLS_C */