blob: dbee389616cbfb231a4353de727cb6f88258ef0d [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 */
Paul Bakker48916f92012-09-16 19:57:18 +0000968 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200973 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974
975 /*
976 * Either the pointers are now NULL or cleared properly and can be freed.
977 * Now allocate missing structures.
978 */
979 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker82788fb2014-10-20 13:59:19 +0200989 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200991 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
994 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400995
Andrzej Kurek4a063792020-10-21 15:08:44 +0200996 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
997 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500998#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001000 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00001001 if( ssl->handshake == NULL ||
1002 ssl->transform_negotiate == NULL ||
1003 ssl->session_negotiate == NULL )
1004 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_free( ssl->handshake );
1008 mbedtls_free( ssl->transform_negotiate );
1009 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010
1011 ssl->handshake = NULL;
1012 ssl->transform_negotiate = NULL;
1013 ssl->session_negotiate = NULL;
1014
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001016 }
1017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001020 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001021 ssl_handshake_params_init( ssl->handshake );
1022
Jerry Yud0766ec2022-09-22 10:46:57 +08001023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1024 defined(MBEDTLS_SSL_SRV_C) && \
1025 defined(MBEDTLS_SSL_SESSION_TICKETS)
1026 ssl->handshake->new_session_tickets_count =
1027 ssl->conf->new_session_tickets_count ;
1028#endif
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1032 {
1033 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001034
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1037 else
1038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001039
Hanno Becker0f57a652020-02-05 10:37:26 +00001040 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001041 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001042#endif
1043
Brett Warrene0edc842021-08-17 09:53:13 +01001044/*
1045 * curve_list is translated to IANA TLS group identifiers here because
1046 * mbedtls_ssl_conf_curves returns void and so can't return
1047 * any error codes.
1048 */
1049#if defined(MBEDTLS_ECP_C)
1050#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1051 /* Heap allocate and translate curve_list from internal to IANA group ids */
1052 if ( ssl->conf->curve_list != NULL )
1053 {
1054 size_t length;
1055 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1056
1057 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1058 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1059
1060 /* Leave room for zero termination */
1061 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1062 if ( group_list == NULL )
1063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1064
1065 for( size_t i = 0; i < length; i++ )
1066 {
Valerio Setti6eca7b42022-12-30 17:44:24 +01001067 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
1068 curve_list[i] );
1069 if ( tls_id == 0 )
Brett Warrene0edc842021-08-17 09:53:13 +01001070 {
1071 mbedtls_free( group_list );
1072 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1073 }
Valerio Setti6eca7b42022-12-30 17:44:24 +01001074 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001075 }
1076
1077 group_list[length] = 0;
1078
1079 ssl->handshake->group_list = group_list;
1080 ssl->handshake->group_list_heap_allocated = 1;
1081 }
1082 else
1083 {
1084 ssl->handshake->group_list = ssl->conf->group_list;
1085 ssl->handshake->group_list_heap_allocated = 0;
1086 }
1087#endif /* MBEDTLS_DEPRECATED_REMOVED */
1088#endif /* MBEDTLS_ECP_C */
1089
Ronald Crone68ab4f2022-10-05 12:46:29 +02001090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001091#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001093 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1094 signature algorithms IANA identifiers. */
1095 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 ssl->conf->sig_hashes != NULL )
1097 {
1098 const int *md;
1099 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001100 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001101 uint16_t *p;
1102
Jerry Yub476a442022-01-21 18:14:45 +08001103#if defined(static_assert)
1104 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1105 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1106 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001107#endif
1108
Jerry Yuf017ee42022-01-12 15:49:48 +08001109 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1110 {
1111 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1112 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001113#if defined(MBEDTLS_ECDSA_C)
1114 sig_algs_len += sizeof( uint16_t );
1115#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(MBEDTLS_RSA_C)
1118 sig_algs_len += sizeof( uint16_t );
1119#endif
1120 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1121 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 }
1123
Jerry Yub476a442022-01-21 18:14:45 +08001124 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1126
Jerry Yub476a442022-01-21 18:14:45 +08001127 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1128 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001129 if( ssl->handshake->sig_algs == NULL )
1130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1131
1132 p = (uint16_t *)ssl->handshake->sig_algs;
1133 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1134 {
1135 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1136 if( hash == MBEDTLS_SSL_HASH_NONE )
1137 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001138#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1140 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001141#endif
1142#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1144 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001145#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001146 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001147 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 1;
1149 }
1150 else
Jerry Yua69269a2022-01-17 21:06:01 +08001151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 ssl->handshake->sig_algs_heap_allocated = 0;
1154 }
Jerry Yucc539102022-06-27 16:27:35 +08001155#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001163static int ssl_cookie_write_dummy( void *ctx,
1164 unsigned char **p, unsigned char *end,
1165 const unsigned char *cli_id, size_t cli_id_len )
1166{
1167 ((void) ctx);
1168 ((void) p);
1169 ((void) end);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177static int ssl_cookie_check_dummy( void *ctx,
1178 const unsigned char *cookie, size_t cookie_len,
1179 const unsigned char *cli_id, size_t cli_id_len )
1180{
1181 ((void) ctx);
1182 ((void) cookie);
1183 ((void) cookie_len);
1184 ((void) cli_id);
1185 ((void) cli_id_len);
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191/*
1192 * Initialize an SSL context
1193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1195{
1196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1197}
1198
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001199MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001200static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1201{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001202 const mbedtls_ssl_config *conf = ssl->conf;
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001205 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1206 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1208 {
1209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1210 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1211 }
1212
Jerry Yu60835a82021-08-04 10:13:52 +08001213 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1214 return( 0 );
1215 }
1216#endif
1217
1218#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001219 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1222 return( 0 );
1223 }
1224#endif
1225
Ronald Cron6f135e12021-12-08 16:57:54 +01001226#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001227 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001228 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1230 {
1231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1233 }
1234
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001235 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1236 {
1237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1238 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1239 }
1240
1241
Ronald Crone1d3f062022-02-10 14:50:54 +01001242 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1243 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001244 }
1245#endif
1246
1247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1248 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1249}
1250
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001251MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001252static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1253{
1254 int ret;
1255 ret = ssl_conf_version_check( ssl );
1256 if( ret != 0 )
1257 return( ret );
1258
Jerry Yudef7ae42022-10-30 14:13:19 +08001259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1260 /* RFC 8446 section 4.4.3
1261 *
1262 * If the verification fails, the receiver MUST terminate the handshake with
1263 * a "decrypt_error" alert.
1264 *
1265 * If the client is configured as TLS 1.3 only with optional verify, return
1266 * bad config.
1267 *
1268 */
1269 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1270 (mbedtls_ssl_context *)ssl ) &&
1271 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1272 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1273 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1274 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1275 {
1276 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001277 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001278 "is not available for TLS 1.3 client" ) );
1279 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1280 }
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1282
Jerry Yu60835a82021-08-04 10:13:52 +08001283 /* Space for further checks */
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001288/*
1289 * Setup an SSL context
1290 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001292int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001293 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Janos Follath865b3eb2019-12-16 11:46:15 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001296 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1297 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001300
Jerry Yu60835a82021-08-04 10:13:52 +08001301 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1302 return( ret );
1303
Paul Bakker62f2dee2012-09-28 07:31:51 +00001304 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001305 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001306 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001307
1308 /* Set to NULL in case of an error condition */
1309 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001310
Darryl Greenb33cc762019-11-28 14:29:44 +00001311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1312 ssl->in_buf_len = in_buf_len;
1313#endif
1314 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001315 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001318 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001320 }
1321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->out_buf_len = out_buf_len;
1324#endif
1325 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001326 if( ssl->out_buf == NULL )
1327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001330 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001333 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001334
Johan Pascalb62bb512015-12-03 21:56:45 +01001335#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001336 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001337#endif
1338
Paul Bakker48916f92012-09-16 19:57:18 +00001339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001340 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001343
1344error:
1345 mbedtls_free( ssl->in_buf );
1346 mbedtls_free( ssl->out_buf );
1347
1348 ssl->conf = NULL;
1349
Darryl Greenb33cc762019-11-28 14:29:44 +00001350#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1351 ssl->in_buf_len = 0;
1352 ssl->out_buf_len = 0;
1353#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001354 ssl->in_buf = NULL;
1355 ssl->out_buf = NULL;
1356
1357 ssl->in_hdr = NULL;
1358 ssl->in_ctr = NULL;
1359 ssl->in_len = NULL;
1360 ssl->in_iv = NULL;
1361 ssl->in_msg = NULL;
1362
1363 ssl->out_hdr = NULL;
1364 ssl->out_ctr = NULL;
1365 ssl->out_len = NULL;
1366 ssl->out_iv = NULL;
1367 ssl->out_msg = NULL;
1368
k-stachowiak9f7798e2018-07-31 16:52:32 +02001369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370}
1371
1372/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 * Reset an initialized and used SSL context for re-use while retaining
1374 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001375 *
1376 * If partial is non-zero, keep data in the input buffer and client ID.
1377 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001378 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1380 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001381{
Darryl Greenb33cc762019-11-28 14:29:44 +00001382#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1383 size_t in_buf_len = ssl->in_buf_len;
1384 size_t out_buf_len = ssl->out_buf_len;
1385#else
1386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1388#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001389
Hanno Beckerb0302c42021-08-03 09:39:42 +01001390#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1391 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001392#endif
1393
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001394 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001395 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001396
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 mbedtls_ssl_reset_in_out_pointers( ssl );
1398
1399 /* Reset incoming message parsing */
1400 ssl->in_offt = NULL;
1401 ssl->nb_zero = 0;
1402 ssl->in_msgtype = 0;
1403 ssl->in_msglen = 0;
1404 ssl->in_hslen = 0;
1405 ssl->keep_current_message = 0;
1406 ssl->transform_in = NULL;
1407
1408#if defined(MBEDTLS_SSL_PROTO_DTLS)
1409 ssl->next_record_offset = 0;
1410 ssl->in_epoch = 0;
1411#endif
1412
1413 /* Keep current datagram if partial == 1 */
1414 if( partial == 0 )
1415 {
1416 ssl->in_left = 0;
1417 memset( ssl->in_buf, 0, in_buf_len );
1418 }
1419
Ronald Cronad8c17b2022-06-10 17:18:09 +02001420 ssl->send_alert = 0;
1421
Hanno Beckerb0302c42021-08-03 09:39:42 +01001422 /* Reset outgoing message writing */
1423 ssl->out_msgtype = 0;
1424 ssl->out_msglen = 0;
1425 ssl->out_left = 0;
1426 memset( ssl->out_buf, 0, out_buf_len );
1427 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1428 ssl->transform_out = NULL;
1429
1430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1431 mbedtls_ssl_dtls_replay_reset( ssl );
1432#endif
1433
XiaokangQian2b01dc32022-01-21 02:53:13 +00001434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435 if( ssl->transform )
1436 {
1437 mbedtls_ssl_transform_free( ssl->transform );
1438 mbedtls_free( ssl->transform );
1439 ssl->transform = NULL;
1440 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1444 mbedtls_ssl_transform_free( ssl->transform_application );
1445 mbedtls_free( ssl->transform_application );
1446 ssl->transform_application = NULL;
1447
1448 if( ssl->handshake != NULL )
1449 {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001450#if defined(MBEDTLS_SSL_EARLY_DATA)
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1452 mbedtls_free( ssl->handshake->transform_earlydata );
1453 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001454#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455
1456 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1457 mbedtls_free( ssl->handshake->transform_handshake );
1458 ssl->handshake->transform_handshake = NULL;
1459 }
1460
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001462}
1463
1464int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1465{
1466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1467
1468 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1469
XiaokangQian78b1fa72022-01-19 06:56:30 +00001470 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471
1472 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_RENEGOTIATION)
1474 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001475 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001476
1477 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1479 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001484 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001485 if( ssl->session )
1486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 mbedtls_ssl_session_free( ssl->session );
1488 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001489 ssl->session = NULL;
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001493 ssl->alpn_chosen = NULL;
1494#endif
1495
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001496#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001497 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001498#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001499 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001500#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001501 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001502 {
1503 mbedtls_free( ssl->cli_id );
1504 ssl->cli_id = NULL;
1505 ssl->cli_id_len = 0;
1506 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001507#endif
1508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1510 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
1512 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001513}
1514
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001515/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001516 * Reset an initialized and used SSL context for re-use while retaining
1517 * all application-set variables, function pointers and data.
1518 */
1519int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1520{
Hanno Becker43aefe22020-02-05 10:44:56 +00001521 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001522}
1523
1524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 * SSL set accessors
1526 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001527void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001529 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530}
1531
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001532void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001534 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001538void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001540 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001541}
1542#endif
1543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001544void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001547}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001550
Hanno Becker1841b0a2018-08-24 11:13:57 +01001551void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1552 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001553{
1554 ssl->disable_datagram_packing = !allow_packing;
1555}
1556
1557void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1558 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001560 conf->hs_timeout_min = min;
1561 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001562}
1563#endif
1564
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001565void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001567 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568}
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001571void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001572 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001573 void *p_vrfy )
1574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001575 conf->f_vrfy = f_vrfy;
1576 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001579
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001580void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001581 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 void *p_rng )
1583{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001584 conf->f_rng = f_rng;
1585 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586}
1587
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001588void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001589 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 void *p_dbg )
1591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001592 conf->f_dbg = f_dbg;
1593 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001597 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001598 mbedtls_ssl_send_t *f_send,
1599 mbedtls_ssl_recv_t *f_recv,
1600 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001601{
1602 ssl->p_bio = p_bio;
1603 ssl->f_send = f_send;
1604 ssl->f_recv = f_recv;
1605 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001606}
1607
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001608#if defined(MBEDTLS_SSL_PROTO_DTLS)
1609void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1610{
1611 ssl->mtu = mtu;
1612}
1613#endif
1614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001615void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001616{
1617 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001618}
1619
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001620void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1621 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001622 mbedtls_ssl_set_timer_t *f_set_timer,
1623 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001624{
1625 ssl->p_timer = p_timer;
1626 ssl->f_set_timer = f_set_timer;
1627 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001628
1629 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001630 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001631}
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001634void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001635 void *p_cache,
1636 mbedtls_ssl_cache_get_t *f_get_cache,
1637 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001639 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001641 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_SSL_CLI_C)
1646int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Janos Follath865b3eb2019-12-16 11:46:15 +00001648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649
1650 if( ssl == NULL ||
1651 session == NULL ||
1652 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001653 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656 }
1657
Hanno Beckere810bbc2021-05-14 16:01:05 +01001658 if( ssl->handshake->resume == 1 )
1659 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1660
Jerry Yu21092062022-10-10 21:21:31 +08001661#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1662 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001663 {
Jerry Yu21092062022-10-10 21:21:31 +08001664 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1665 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1666
1667 if( mbedtls_ssl_validate_ciphersuite(
1668 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1669 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1670 {
1671 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1672 session->ciphersuite ) );
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1674 }
Jerry Yu40afab62022-10-08 10:42:13 +08001675 }
Jerry Yu21092062022-10-10 21:21:31 +08001676#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001677
Hanno Becker52055ae2019-02-06 14:30:46 +00001678 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1679 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001680 return( ret );
1681
Paul Bakker0a597072012-09-25 21:55:46 +00001682 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001683
1684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001688void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1689 const int *ciphersuites )
1690{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001691 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Ronald Cron6f135e12021-12-08 16:57:54 +01001694#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001695void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001696 const int kex_modes )
1697{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001698 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001699}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001700
1701#if defined(MBEDTLS_SSL_EARLY_DATA)
1702void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001703 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001704{
1705 conf->early_data_enabled = early_data_enabled;
1706}
Jerry Yucc4e0072022-11-22 17:22:22 +08001707
1708#if defined(MBEDTLS_SSL_SRV_C)
1709void mbedtls_ssl_tls13_conf_max_early_data_size(
1710 mbedtls_ssl_config *conf, uint32_t max_early_data_size )
1711{
Jerry Yu39da9852022-12-06 16:58:36 +08001712 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001713}
1714#endif /* MBEDTLS_SSL_SRV_C */
1715
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001716#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001717#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001720void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001721 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001722{
1723 conf->cert_profile = profile;
1724}
1725
Glenn Strauss36872db2022-01-22 05:06:31 -05001726static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1727{
1728 mbedtls_ssl_key_cert *cur = key_cert, *next;
1729
1730 while( cur != NULL )
1731 {
1732 next = cur->next;
1733 mbedtls_free( cur );
1734 cur = next;
1735 }
1736}
1737
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001738/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001739MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001740static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1741 mbedtls_x509_crt *cert,
1742 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001743{
niisato8ee24222018-06-25 19:05:48 +09001744 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745
Glenn Strauss36872db2022-01-22 05:06:31 -05001746 if( cert == NULL )
1747 {
1748 /* Free list if cert is null */
1749 ssl_key_cert_free( *head );
1750 *head = NULL;
1751 return( 0 );
1752 }
1753
niisato8ee24222018-06-25 19:05:48 +09001754 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1755 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001756 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001757
niisato8ee24222018-06-25 19:05:48 +09001758 new_cert->cert = cert;
1759 new_cert->key = key;
1760 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001761
Glenn Strauss36872db2022-01-22 05:06:31 -05001762 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001763 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001764 {
niisato8ee24222018-06-25 19:05:48 +09001765 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001766 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001767 else
1768 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001769 mbedtls_ssl_key_cert *cur = *head;
1770 while( cur->next != NULL )
1771 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001772 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001773 }
1774
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001775 return( 0 );
1776}
1777
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001778int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001779 mbedtls_x509_crt *own_cert,
1780 mbedtls_pk_context *pk_key )
1781{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001782 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001783}
1784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001785void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001786 mbedtls_x509_crt *ca_chain,
1787 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001789 conf->ca_chain = ca_chain;
1790 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001791
1792#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1793 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1794 * cannot be used together. */
1795 conf->f_ca_cb = NULL;
1796 conf->p_ca_cb = NULL;
1797#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798}
Hanno Becker5adaad92019-03-27 16:54:37 +00001799
1800#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1801void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001802 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001803 void *p_ca_cb )
1804{
1805 conf->f_ca_cb = f_ca_cb;
1806 conf->p_ca_cb = p_ca_cb;
1807
1808 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1809 * cannot be used together. */
1810 conf->ca_chain = NULL;
1811 conf->ca_crl = NULL;
1812}
1813#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001815
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001816#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001817const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1818 size_t *name_len )
1819{
1820 *name_len = ssl->handshake->sni_name_len;
1821 return( ssl->handshake->sni_name );
1822}
1823
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001824int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1825 mbedtls_x509_crt *own_cert,
1826 mbedtls_pk_context *pk_key )
1827{
1828 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1829 own_cert, pk_key ) );
1830}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001831
1832void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1833 mbedtls_x509_crt *ca_chain,
1834 mbedtls_x509_crl *ca_crl )
1835{
1836 ssl->handshake->sni_ca_chain = ca_chain;
1837 ssl->handshake->sni_ca_crl = ca_crl;
1838}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001839
Glenn Strauss999ef702022-03-11 01:37:23 -05001840#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1841void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1842 const mbedtls_x509_crt *crt)
1843{
1844 ssl->handshake->dn_hints = crt;
1845}
1846#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1847
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001848void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1849 int authmode )
1850{
1851 ssl->handshake->sni_authmode = authmode;
1852}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001853#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1854
Hanno Becker8927c832019-04-03 12:52:50 +01001855#if defined(MBEDTLS_X509_CRT_PARSE_C)
1856void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1857 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1858 void *p_vrfy )
1859{
1860 ssl->f_vrfy = f_vrfy;
1861 ssl->p_vrfy = p_vrfy;
1862}
1863#endif
1864
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001865#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001866
Valerio Setti016f6822022-12-09 14:17:50 +01001867#if defined(MBEDTLS_USE_PSA_CRYPTO)
1868static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
1869 mbedtls_ssl_context *ssl,
1870 mbedtls_svc_key_id_t pwd )
1871{
1872 psa_status_t status;
1873 psa_pake_role_t psa_role;
1874 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1875
1876 psa_pake_cs_set_algorithm( &cipher_suite, PSA_ALG_JPAKE );
1877 psa_pake_cs_set_primitive( &cipher_suite,
1878 PSA_PAKE_PRIMITIVE( PSA_PAKE_PRIMITIVE_TYPE_ECC,
1879 PSA_ECC_FAMILY_SECP_R1,
1880 256) );
1881 psa_pake_cs_set_hash( &cipher_suite, PSA_ALG_SHA_256 );
1882
1883 status = psa_pake_setup( &ssl->handshake->psa_pake_ctx, &cipher_suite );
1884 if( status != PSA_SUCCESS )
1885 return status;
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001886
Neil Armstrongca7d5062022-05-31 14:43:23 +02001887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1888 psa_role = PSA_PAKE_ROLE_SERVER;
1889 else
1890 psa_role = PSA_PAKE_ROLE_CLIENT;
1891
Valerio Setti016f6822022-12-09 14:17:50 +01001892 status = psa_pake_set_role( &ssl->handshake->psa_pake_ctx, psa_role );
1893 if( status != PSA_SUCCESS )
1894 return status;
1895
1896 status = psa_pake_set_password_key( &ssl->handshake->psa_pake_ctx, pwd );
1897 if( status != PSA_SUCCESS )
1898 return status;
1899
1900 ssl->handshake->psa_pake_ctx_is_ok = 1;
1901
1902 return ( PSA_SUCCESS );
1903}
1904
1905int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1906 const unsigned char *pw,
1907 size_t pw_len )
1908{
1909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1910 psa_status_t status;
1911
1912 if( ssl->handshake == NULL || ssl->conf == NULL )
1913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1914
Valerio Settiaca21b72022-11-17 18:17:01 +01001915 /* Empty password is not valid */
1916 if( ( pw == NULL) || ( pw_len == 0 ) )
1917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001918
Valerio Settiaca21b72022-11-17 18:17:01 +01001919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
1920 psa_set_key_algorithm( &attributes, PSA_ALG_JPAKE );
1921 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001922
Valerio Settiaca21b72022-11-17 18:17:01 +01001923 status = psa_import_key( &attributes, pw, pw_len,
1924 &ssl->handshake->psa_pake_password );
1925 if( status != PSA_SUCCESS )
1926 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001927
Valerio Setti016f6822022-12-09 14:17:50 +01001928 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl,
1929 ssl->handshake->psa_pake_password );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001930 if( status != PSA_SUCCESS )
1931 {
1932 psa_destroy_key( ssl->handshake->psa_pake_password );
1933 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1934 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1935 }
1936
Neil Armstrongca7d5062022-05-31 14:43:23 +02001937 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001938}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001939
1940int mbedtls_ssl_set_hs_ecjpake_password_opaque( mbedtls_ssl_context *ssl,
1941 mbedtls_svc_key_id_t pwd )
1942{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001943 psa_status_t status;
1944
1945 if( ssl->handshake == NULL || ssl->conf == NULL )
1946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1947
Valerio Settia9a97dc2022-11-28 18:26:16 +01001948 if( mbedtls_svc_key_id_is_null( pwd ) )
1949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Valerio Settia9a97dc2022-11-28 18:26:16 +01001950
Valerio Setti016f6822022-12-09 14:17:50 +01001951 status = mbedtls_ssl_set_hs_ecjpake_password_common( ssl, pwd );
Neil Armstrongca7d5062022-05-31 14:43:23 +02001952 if( status != PSA_SUCCESS )
1953 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001954 psa_pake_abort( &ssl->handshake->psa_pake_ctx );
1955 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1956 }
1957
Neil Armstrongca7d5062022-05-31 14:43:23 +02001958 return( 0 );
Valerio Setti02c25b52022-11-15 14:08:42 +01001959}
1960#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001961int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1962 const unsigned char *pw,
1963 size_t pw_len )
1964{
1965 mbedtls_ecjpake_role role;
1966
1967 if( ssl->handshake == NULL || ssl->conf == NULL )
1968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1969
Valerio Settic689ed82022-12-07 14:40:38 +01001970 /* Empty password is not valid */
1971 if( ( pw == NULL) || ( pw_len == 0 ) )
1972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1973
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1975 role = MBEDTLS_ECJPAKE_SERVER;
1976 else
1977 role = MBEDTLS_ECJPAKE_CLIENT;
1978
1979 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1980 role,
1981 MBEDTLS_MD_SHA256,
1982 MBEDTLS_ECP_DP_SECP256R1,
1983 pw, pw_len ) );
1984}
Valerio Setti02c25b52022-11-15 14:08:42 +01001985#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001986#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001987
Ronald Cron73fe8df2022-10-05 14:31:43 +02001988#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001989int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001990{
Ronald Crond29e13e2022-10-19 10:33:48 +02001991 if( conf->psk_identity == NULL ||
1992 conf->psk_identity_len == 0 )
1993 {
1994 return( 0 );
1995 }
1996
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001997#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001998 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001999 return( 1 );
2000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002001
2002 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002003 return( 1 );
2004
2005 return( 0 );
2006}
2007
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002008static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
2009{
2010 /* Remove reference to existing PSK, if any. */
2011#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002012 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002013 {
2014 /* The maintenance of the PSK key slot is the
2015 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002016 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002017 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002018#endif /* MBEDTLS_USE_PSA_CRYPTO */
2019 if( conf->psk != NULL )
2020 {
2021 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
2022
2023 mbedtls_free( conf->psk );
2024 conf->psk = NULL;
2025 conf->psk_len = 0;
2026 }
2027
2028 /* Remove reference to PSK identity, if any. */
2029 if( conf->psk_identity != NULL )
2030 {
2031 mbedtls_free( conf->psk_identity );
2032 conf->psk_identity = NULL;
2033 conf->psk_identity_len = 0;
2034 }
2035}
2036
Hanno Becker7390c712018-11-15 13:33:04 +00002037/* This function assumes that PSK identity in the SSL config is unset.
2038 * It checks that the provided identity is well-formed and attempts
2039 * to make a copy of it in the SSL config.
2040 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002041MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00002042static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
2043 unsigned char const *psk_identity,
2044 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002045{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002046 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00002047 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002048 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00002049 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10002050 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002051 {
2052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2053 }
2054
Hanno Becker7390c712018-11-15 13:33:04 +00002055 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
2056 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02002058
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002059 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002060 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02002061
2062 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02002063}
2064
Hanno Becker7390c712018-11-15 13:33:04 +00002065int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
2066 const unsigned char *psk, size_t psk_len,
2067 const unsigned char *psk_identity, size_t psk_identity_len )
2068{
Janos Follath865b3eb2019-12-16 11:46:15 +00002069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002070
2071 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002072 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002073 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00002074
2075 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002076 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00002077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002078 if( psk_len == 0 )
2079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2080 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2082
Hanno Becker7390c712018-11-15 13:33:04 +00002083 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
2084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2085 conf->psk_len = psk_len;
2086 memcpy( conf->psk, psk, conf->psk_len );
2087
2088 /* Check and set PSK Identity */
2089 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
2090 if( ret != 0 )
2091 ssl_conf_remove_psk( conf );
2092
2093 return( ret );
2094}
2095
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002096static void ssl_remove_psk( mbedtls_ssl_context *ssl )
2097{
2098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02002099 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002100 {
Neil Armstrong501c9322022-05-03 09:35:09 +02002101 /* The maintenance of the external PSK key slot is the
2102 * user's responsibility. */
2103 if( ssl->handshake->psk_opaque_is_internal )
2104 {
2105 psa_destroy_key( ssl->handshake->psk_opaque );
2106 ssl->handshake->psk_opaque_is_internal = 0;
2107 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002109 }
Neil Armstronge952a302022-05-03 10:22:14 +02002110#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111 if( ssl->handshake->psk != NULL )
2112 {
2113 mbedtls_platform_zeroize( ssl->handshake->psk,
2114 ssl->handshake->psk_len );
2115 mbedtls_free( ssl->handshake->psk );
2116 ssl->handshake->psk_len = 0;
2117 }
Neil Armstronge952a302022-05-03 10:22:14 +02002118#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002119}
2120
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002121int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2122 const unsigned char *psk, size_t psk_len )
2123{
Neil Armstrong501c9322022-05-03 09:35:09 +02002124#if defined(MBEDTLS_USE_PSA_CRYPTO)
2125 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002127 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002129#endif /* MBEDTLS_USE_PSA_CRYPTO */
2130
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002131 if( psk == NULL || ssl->handshake == NULL )
2132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2133
2134 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2135 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2136
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002137 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002138
Neil Armstrong501c9322022-05-03 09:35:09 +02002139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2141 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2142 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002143 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002144 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2145 else
2146 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2147 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2148 }
2149#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002150
Jerry Yu568ec252022-07-22 21:27:34 +08002151#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002152 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2153 {
2154 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2155 psa_set_key_usage_flags( &key_attributes,
2156 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2157 }
2158#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2159
Neil Armstrong501c9322022-05-03 09:35:09 +02002160 psa_set_key_algorithm( &key_attributes, alg );
2161 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2162
2163 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2164 if( status != PSA_SUCCESS )
2165 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2166
2167 /* Allow calling psa_destroy_key() on psk remove */
2168 ssl->handshake->psk_opaque_is_internal = 1;
2169 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2170#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002171 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002172 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002173
2174 ssl->handshake->psk_len = psk_len;
2175 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2176
2177 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002179}
2180
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002181#if defined(MBEDTLS_USE_PSA_CRYPTO)
2182int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002183 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002184 const unsigned char *psk_identity,
2185 size_t psk_identity_len )
2186{
Janos Follath865b3eb2019-12-16 11:46:15 +00002187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002188
2189 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002190 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002192
Hanno Becker7390c712018-11-15 13:33:04 +00002193 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002194 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002196 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002197
2198 /* Check and set PSK Identity */
2199 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2200 psk_identity_len );
2201 if( ret != 0 )
2202 ssl_conf_remove_psk( conf );
2203
2204 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002205}
2206
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002207int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2208 mbedtls_svc_key_id_t psk )
2209{
2210 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2211 ( ssl->handshake == NULL ) )
2212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2213
2214 ssl_remove_psk( ssl );
2215 ssl->handshake->psk_opaque = psk;
2216 return( 0 );
2217}
2218#endif /* MBEDTLS_USE_PSA_CRYPTO */
2219
Jerry Yu8897c072022-08-12 13:56:53 +08002220#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002221void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2222 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2223 size_t),
2224 void *p_psk )
2225{
2226 conf->f_psk = f_psk;
2227 conf->p_psk = p_psk;
2228}
Jerry Yu8897c072022-08-12 13:56:53 +08002229#endif /* MBEDTLS_SSL_SRV_C */
2230
Ronald Cron73fe8df2022-10-05 14:31:43 +02002231#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002232
2233#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002234static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2235 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002236{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002237#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002238 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002239 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002240#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002241 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002242 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002243 return( MBEDTLS_SSL_MODE_STREAM );
2244}
2245
2246#else /* MBEDTLS_USE_PSA_CRYPTO */
2247
2248static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2249 mbedtls_cipher_mode_t mode )
2250{
2251#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2252 if( mode == MBEDTLS_MODE_CBC )
2253 return( MBEDTLS_SSL_MODE_CBC );
2254#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2255
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002256#if defined(MBEDTLS_GCM_C) || \
2257 defined(MBEDTLS_CCM_C) || \
2258 defined(MBEDTLS_CHACHAPOLY_C)
2259 if( mode == MBEDTLS_MODE_GCM ||
2260 mode == MBEDTLS_MODE_CCM ||
2261 mode == MBEDTLS_MODE_CHACHAPOLY )
2262 return( MBEDTLS_SSL_MODE_AEAD );
2263#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002264
2265 return( MBEDTLS_SSL_MODE_STREAM );
2266}
Gilles Peskine301711e2022-04-26 16:57:05 +02002267#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002268
Gilles Peskinee108d982022-04-26 16:50:40 +02002269static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2270 mbedtls_ssl_mode_t base_mode,
2271 int encrypt_then_mac )
2272{
2273#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2274 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2275 base_mode == MBEDTLS_SSL_MODE_CBC )
2276 {
2277 return( MBEDTLS_SSL_MODE_CBC_ETM );
2278 }
2279#else
2280 (void) encrypt_then_mac;
2281#endif
2282 return( base_mode );
2283}
2284
Neil Armstrongab555e02022-04-04 11:07:59 +02002285mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002286 const mbedtls_ssl_transform *transform )
2287{
Gilles Peskinee108d982022-04-26 16:50:40 +02002288 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002289#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002290 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002291#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002292 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2293#endif
2294 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002295
Gilles Peskinee108d982022-04-26 16:50:40 +02002296 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002297#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002298 encrypt_then_mac = transform->encrypt_then_mac;
2299#endif
2300 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002301}
2302
Neil Armstrongab555e02022-04-04 11:07:59 +02002303mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002304#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002305 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002306#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002307 const mbedtls_ssl_ciphersuite_t *suite )
2308{
Gilles Peskinee108d982022-04-26 16:50:40 +02002309 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2310
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002311#if defined(MBEDTLS_USE_PSA_CRYPTO)
2312 psa_status_t status;
2313 psa_algorithm_t alg;
2314 psa_key_type_t type;
2315 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002316 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2317 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002318 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002319#else
2320 const mbedtls_cipher_info_t *cipher =
2321 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002322 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002323 {
2324 base_mode =
2325 mbedtls_ssl_get_base_mode(
2326 mbedtls_cipher_info_get_mode( cipher ) );
2327 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002328#endif /* MBEDTLS_USE_PSA_CRYPTO */
2329
Gilles Peskinee108d982022-04-26 16:50:40 +02002330#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2331 int encrypt_then_mac = 0;
2332#endif
2333 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002334}
2335
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002336#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002337
2338#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002339/* Serialization of TLS 1.3 sessions:
2340 *
2341 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002342 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002343 * uint64 ticket_received;
2344 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002345 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002346 * } ClientOnlyData;
2347 *
2348 * struct {
2349 * uint8 endpoint;
2350 * uint8 ciphersuite[2];
2351 * uint32 ticket_age_add;
2352 * uint8 ticket_flags;
2353 * opaque resumption_key<0..255>;
2354 * select ( endpoint ) {
2355 * case client: ClientOnlyData;
2356 * case server: uint64 start_time;
2357 * };
2358 * } serialized_session_tls13;
2359 *
2360 */
2361#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002362MBEDTLS_CHECK_RETURN_CRITICAL
2363static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2364 unsigned char *buf,
2365 size_t buf_len,
2366 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002367{
2368 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002369#if defined(MBEDTLS_SSL_CLI_C) && \
2370 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2371 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002372 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002373#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002374 size_t needed = 1 /* endpoint */
2375 + 2 /* ciphersuite */
2376 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002377 + 1 /* ticket_flags */
2378 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002379 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002380
2381 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2383 needed += session->resumption_key_len; /* resumption_key */
2384
Jerry Yu438ddd82022-07-07 06:55:50 +00002385#if defined(MBEDTLS_HAVE_TIME)
2386 needed += 8; /* start_time or ticket_received */
2387#endif
2388
2389#if defined(MBEDTLS_SSL_CLI_C)
2390 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2391 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002392#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002393 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002394 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002395#endif
2396
Jerry Yu438ddd82022-07-07 06:55:50 +00002397 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002398 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002399
2400 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002401 if( session->ticket_len > SIZE_MAX - needed )
2402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002403
Jerry Yue28d9742022-08-18 15:44:03 +08002404 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002405 }
2406#endif /* MBEDTLS_SSL_CLI_C */
2407
Jerry Yue36fdd62022-08-17 21:31:36 +08002408 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002409 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002410 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002411
2412 p[0] = session->endpoint;
2413 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2414 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2415 p[7] = session->ticket_flags;
2416
2417 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002418 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002419 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002420 memcpy( p, session->resumption_key, session->resumption_key_len );
2421 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002422
2423#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2424 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2425 {
2426 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2427 p += 8;
2428 }
2429#endif /* MBEDTLS_HAVE_TIME */
2430
2431#if defined(MBEDTLS_SSL_CLI_C)
2432 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2433 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002434#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2435 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2436 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002437 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002438 {
2439 /* save host name */
2440 memcpy( p, session->hostname, hostname_len );
2441 p += hostname_len;
2442 }
2443#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2444
Jerry Yu438ddd82022-07-07 06:55:50 +00002445#if defined(MBEDTLS_HAVE_TIME)
2446 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2447 p += 8;
2448#endif
2449 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2450 p += 4;
2451
2452 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2453 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002454
2455 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002456 {
2457 memcpy( p, session->ticket, session->ticket_len );
2458 p += session->ticket_len;
2459 }
2460 }
2461#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002462 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002463}
2464
2465MBEDTLS_CHECK_RETURN_CRITICAL
2466static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2467 const unsigned char *buf,
2468 size_t len )
2469{
2470 const unsigned char *p = buf;
2471 const unsigned char *end = buf + len;
2472
2473 if( end - p < 9 )
2474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2475 session->endpoint = p[0];
2476 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2477 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2478 session->ticket_flags = p[7];
2479
2480 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002481 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002482 p += 9;
2483
Jerry Yubc7c1a42022-07-21 22:57:37 +08002484 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2486
Jerry Yubc7c1a42022-07-21 22:57:37 +08002487 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002489 memcpy( session->resumption_key, p, session->resumption_key_len );
2490 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002491
2492#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2493 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2494 {
2495 if( end - p < 8 )
2496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2497 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2498 p += 8;
2499 }
2500#endif /* MBEDTLS_HAVE_TIME */
2501
2502#if defined(MBEDTLS_SSL_CLI_C)
2503 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2504 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002505#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2506 defined(MBEDTLS_SSL_SESSION_TICKETS)
2507 size_t hostname_len;
2508 /* load host name */
2509 if( end - p < 2 )
2510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2511 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2512 p += 2;
2513
2514 if( end - p < ( long int )hostname_len )
2515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2516 if( hostname_len > 0 )
2517 {
2518 session->hostname = mbedtls_calloc( 1, hostname_len );
2519 if( session->hostname == NULL )
2520 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2521 memcpy( session->hostname, p, hostname_len );
2522 p += hostname_len;
2523 }
2524#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2525 MBEDTLS_SSL_SESSION_TICKETS */
2526
Jerry Yu438ddd82022-07-07 06:55:50 +00002527#if defined(MBEDTLS_HAVE_TIME)
2528 if( end - p < 8 )
2529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2530 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2531 p += 8;
2532#endif
2533 if( end - p < 4 )
2534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2535 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2536 p += 4;
2537
2538 if( end - p < 2 )
2539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2540 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2541 p += 2;
2542
2543 if( end - p < ( long int )session->ticket_len )
2544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2545 if( session->ticket_len > 0 )
2546 {
2547 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2548 if( session->ticket == NULL )
2549 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2550 memcpy( session->ticket, p, session->ticket_len );
2551 p += session->ticket_len;
2552 }
2553 }
2554#endif /* MBEDTLS_SSL_CLI_C */
2555
2556 return( 0 );
2557
2558}
2559#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002560MBEDTLS_CHECK_RETURN_CRITICAL
2561static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2562 unsigned char *buf,
2563 size_t buf_len,
2564 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002565{
2566 ((void) session);
2567 ((void) buf);
2568 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002569 *olen = 0;
2570 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002571}
Jerry Yu438ddd82022-07-07 06:55:50 +00002572
2573static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2574 unsigned char *buf,
2575 size_t buf_len )
2576{
2577 ((void) session);
2578 ((void) buf);
2579 ((void) buf_len);
2580 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2581}
2582#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002583#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2584
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002585psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002586 size_t taglen,
2587 psa_algorithm_t *alg,
2588 psa_key_type_t *key_type,
2589 size_t *key_size )
2590{
2591 switch ( mbedtls_cipher_type )
2592 {
2593 case MBEDTLS_CIPHER_AES_128_CBC:
2594 *alg = PSA_ALG_CBC_NO_PADDING;
2595 *key_type = PSA_KEY_TYPE_AES;
2596 *key_size = 128;
2597 break;
2598 case MBEDTLS_CIPHER_AES_128_CCM:
2599 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2600 *key_type = PSA_KEY_TYPE_AES;
2601 *key_size = 128;
2602 break;
2603 case MBEDTLS_CIPHER_AES_128_GCM:
2604 *alg = PSA_ALG_GCM;
2605 *key_type = PSA_KEY_TYPE_AES;
2606 *key_size = 128;
2607 break;
2608 case MBEDTLS_CIPHER_AES_192_CCM:
2609 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2610 *key_type = PSA_KEY_TYPE_AES;
2611 *key_size = 192;
2612 break;
2613 case MBEDTLS_CIPHER_AES_192_GCM:
2614 *alg = PSA_ALG_GCM;
2615 *key_type = PSA_KEY_TYPE_AES;
2616 *key_size = 192;
2617 break;
2618 case MBEDTLS_CIPHER_AES_256_CBC:
2619 *alg = PSA_ALG_CBC_NO_PADDING;
2620 *key_type = PSA_KEY_TYPE_AES;
2621 *key_size = 256;
2622 break;
2623 case MBEDTLS_CIPHER_AES_256_CCM:
2624 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2625 *key_type = PSA_KEY_TYPE_AES;
2626 *key_size = 256;
2627 break;
2628 case MBEDTLS_CIPHER_AES_256_GCM:
2629 *alg = PSA_ALG_GCM;
2630 *key_type = PSA_KEY_TYPE_AES;
2631 *key_size = 256;
2632 break;
2633 case MBEDTLS_CIPHER_ARIA_128_CBC:
2634 *alg = PSA_ALG_CBC_NO_PADDING;
2635 *key_type = PSA_KEY_TYPE_ARIA;
2636 *key_size = 128;
2637 break;
2638 case MBEDTLS_CIPHER_ARIA_128_CCM:
2639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2640 *key_type = PSA_KEY_TYPE_ARIA;
2641 *key_size = 128;
2642 break;
2643 case MBEDTLS_CIPHER_ARIA_128_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_ARIA;
2646 *key_size = 128;
2647 break;
2648 case MBEDTLS_CIPHER_ARIA_192_CCM:
2649 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2650 *key_type = PSA_KEY_TYPE_ARIA;
2651 *key_size = 192;
2652 break;
2653 case MBEDTLS_CIPHER_ARIA_192_GCM:
2654 *alg = PSA_ALG_GCM;
2655 *key_type = PSA_KEY_TYPE_ARIA;
2656 *key_size = 192;
2657 break;
2658 case MBEDTLS_CIPHER_ARIA_256_CBC:
2659 *alg = PSA_ALG_CBC_NO_PADDING;
2660 *key_type = PSA_KEY_TYPE_ARIA;
2661 *key_size = 256;
2662 break;
2663 case MBEDTLS_CIPHER_ARIA_256_CCM:
2664 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2665 *key_type = PSA_KEY_TYPE_ARIA;
2666 *key_size = 256;
2667 break;
2668 case MBEDTLS_CIPHER_ARIA_256_GCM:
2669 *alg = PSA_ALG_GCM;
2670 *key_type = PSA_KEY_TYPE_ARIA;
2671 *key_size = 256;
2672 break;
2673 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2674 *alg = PSA_ALG_CBC_NO_PADDING;
2675 *key_type = PSA_KEY_TYPE_CAMELLIA;
2676 *key_size = 128;
2677 break;
2678 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2680 *key_type = PSA_KEY_TYPE_CAMELLIA;
2681 *key_size = 128;
2682 break;
2683 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_CAMELLIA;
2686 *key_size = 128;
2687 break;
2688 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2689 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2690 *key_type = PSA_KEY_TYPE_CAMELLIA;
2691 *key_size = 192;
2692 break;
2693 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2694 *alg = PSA_ALG_GCM;
2695 *key_type = PSA_KEY_TYPE_CAMELLIA;
2696 *key_size = 192;
2697 break;
2698 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2699 *alg = PSA_ALG_CBC_NO_PADDING;
2700 *key_type = PSA_KEY_TYPE_CAMELLIA;
2701 *key_size = 256;
2702 break;
2703 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2705 *key_type = PSA_KEY_TYPE_CAMELLIA;
2706 *key_size = 256;
2707 break;
2708 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2709 *alg = PSA_ALG_GCM;
2710 *key_type = PSA_KEY_TYPE_CAMELLIA;
2711 *key_size = 256;
2712 break;
2713 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2714 *alg = PSA_ALG_CHACHA20_POLY1305;
2715 *key_type = PSA_KEY_TYPE_CHACHA20;
2716 *key_size = 256;
2717 break;
2718 case MBEDTLS_CIPHER_NULL:
2719 *alg = MBEDTLS_SSL_NULL_CIPHER;
2720 *key_type = 0;
2721 *key_size = 0;
2722 break;
2723 default:
2724 return PSA_ERROR_NOT_SUPPORTED;
2725 }
2726
2727 return PSA_SUCCESS;
2728}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002729#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002730
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002731#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002732int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2733 const unsigned char *dhm_P, size_t P_len,
2734 const unsigned char *dhm_G, size_t G_len )
2735{
Janos Follath865b3eb2019-12-16 11:46:15 +00002736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002737
Glenn Strausscee11292021-12-20 01:43:17 -05002738 mbedtls_mpi_free( &conf->dhm_P );
2739 mbedtls_mpi_free( &conf->dhm_G );
2740
Hanno Beckera90658f2017-10-04 15:29:08 +01002741 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2742 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2743 {
2744 mbedtls_mpi_free( &conf->dhm_P );
2745 mbedtls_mpi_free( &conf->dhm_G );
2746 return( ret );
2747 }
2748
2749 return( 0 );
2750}
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002752int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002753{
Janos Follath865b3eb2019-12-16 11:46:15 +00002754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002755
Glenn Strausscee11292021-12-20 01:43:17 -05002756 mbedtls_mpi_free( &conf->dhm_P );
2757 mbedtls_mpi_free( &conf->dhm_G );
2758
Gilles Peskinee5702482021-06-11 21:59:08 +02002759 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2760 &conf->dhm_P ) ) != 0 ||
2761 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2762 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002763 {
2764 mbedtls_mpi_free( &conf->dhm_P );
2765 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002766 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002767 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002768
2769 return( 0 );
2770}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002771#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002772
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002773#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2774/*
2775 * Set the minimum length for Diffie-Hellman parameters
2776 */
2777void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2778 unsigned int bitlen )
2779{
2780 conf->dhm_min_bitlen = bitlen;
2781}
2782#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2783
Ronald Crone68ab4f2022-10-05 12:46:29 +02002784#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002785#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002786/*
2787 * Set allowed/preferred hashes for handshake signatures
2788 */
2789void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2790 const int *hashes )
2791{
2792 conf->sig_hashes = hashes;
2793}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002794#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002795
Jerry Yuf017ee42022-01-12 15:49:48 +08002796/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002797void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2798 const uint16_t* sig_algs )
2799{
Jerry Yuf017ee42022-01-12 15:49:48 +08002800#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2801 conf->sig_hashes = NULL;
2802#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2803 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002804}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002805#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002806
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002807#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002808#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002809/*
2810 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002811 *
2812 * mbedtls_ssl_setup() takes the provided list
2813 * and translates it to a list of IANA TLS group identifiers,
2814 * stored in ssl->handshake->group_list.
2815 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002816 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002817void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002818 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002820 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002821 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002822}
Brett Warrene0edc842021-08-17 09:53:13 +01002823#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002824#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002825
Brett Warrene0edc842021-08-17 09:53:13 +01002826/*
2827 * Set the allowed groups
2828 */
2829void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2830 const uint16_t *group_list )
2831{
2832#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2833 conf->curve_list = NULL;
2834#endif
2835 conf->group_list = group_list;
2836}
2837
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002838#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840{
Hanno Becker947194e2017-04-07 13:25:49 +01002841 /* Initialize to suppress unnecessary compiler warning */
2842 size_t hostname_len = 0;
2843
2844 /* Check if new hostname is valid before
2845 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002846 if( hostname != NULL )
2847 {
2848 hostname_len = strlen( hostname );
2849
2850 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2852 }
2853
2854 /* Now it's clear that we will overwrite the old hostname,
2855 * so we can free it safely */
2856
2857 if( ssl->hostname != NULL )
2858 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002859 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002860 mbedtls_free( ssl->hostname );
2861 }
2862
2863 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002864
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002866 {
2867 ssl->hostname = NULL;
2868 }
2869 else
2870 {
2871 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002872 if( ssl->hostname == NULL )
2873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002874
Hanno Becker947194e2017-04-07 13:25:49 +01002875 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002876
Hanno Becker947194e2017-04-07 13:25:49 +01002877 ssl->hostname[hostname_len] = '\0';
2878 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
2880 return( 0 );
2881}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002882#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002884#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002885void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002887 const unsigned char *, size_t),
2888 void *p_sni )
2889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002890 conf->f_sni = f_sni;
2891 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002896int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002897{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002898 size_t cur_len, tot_len;
2899 const char **p;
2900
2901 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002902 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2903 * MUST NOT be truncated."
2904 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002905 */
2906 tot_len = 0;
2907 for( p = protos; *p != NULL; p++ )
2908 {
2909 cur_len = strlen( *p );
2910 tot_len += cur_len;
2911
Ronald Cron8216dd32020-04-23 16:41:44 +02002912 if( ( cur_len == 0 ) ||
2913 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2914 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002916 }
2917
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002918 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002919
2920 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002921}
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002924{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002925 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002928
Johan Pascalb62bb512015-12-03 21:56:45 +01002929#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002930void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2931 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002932{
2933 conf->dtls_srtp_mki_support = support_mki_value;
2934}
2935
Ron Eldoref72faf2018-07-12 11:54:20 +03002936int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2937 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002938 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002939{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002940 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002941 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002943 }
Ron Eldor591f1622018-01-22 12:30:04 +02002944
2945 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002946 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002947 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002948 }
Ron Eldor591f1622018-01-22 12:30:04 +02002949
2950 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2951 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002952 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002953}
2954
Ron Eldoref72faf2018-07-12 11:54:20 +03002955int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002956 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002957{
Johan Pascal253d0262020-09-22 13:04:45 +02002958 const mbedtls_ssl_srtp_profile *p;
2959 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002960
Johan Pascal253d0262020-09-22 13:04:45 +02002961 /* check the profiles list: all entry must be valid,
2962 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002963 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2964 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2965 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002966 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002967 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002968 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002969 list_size++;
2970 }
2971 else
2972 {
2973 /* unsupported value, stop parsing and set the size to an error value */
2974 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002975 }
2976 }
2977
Johan Pascal5ef72d22020-10-28 17:05:47 +01002978 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002979 {
Johan Pascal253d0262020-09-22 13:04:45 +02002980 conf->dtls_srtp_profile_list = NULL;
2981 conf->dtls_srtp_profile_list_len = 0;
2982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2983 }
2984
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002985 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002986 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002987
2988 return( 0 );
2989}
2990
Johan Pascal5ef72d22020-10-28 17:05:47 +01002991void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2992 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002993{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002994 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2995 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002996 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002997 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002998 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002999 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01003000 else
3001 {
3002 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01003003 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3004 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01003005 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003006}
Johan Pascalb62bb512015-12-03 21:56:45 +01003007#endif /* MBEDTLS_SSL_DTLS_SRTP */
3008
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303009#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003010void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003011{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003012 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003013}
3014
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02003015void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00003016{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003017 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003018}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303019#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003020
Janos Follath088ce432017-04-10 12:42:31 +01003021#if defined(MBEDTLS_SSL_SRV_C)
3022void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
3023 char cert_req_ca_list )
3024{
3025 conf->cert_req_ca_list = cert_req_ca_list;
3026}
3027#endif
3028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003030void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003032 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003033}
3034#endif
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003037void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003038{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003039 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003040}
3041#endif
3042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003044int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10003047 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003050 }
3051
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003052 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003053
3054 return( 0 );
3055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003058void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00003059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003060 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003061}
3062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003064void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003066 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003067}
3068
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003069void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003071 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003072}
3073
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02003074void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003075 const unsigned char period[8] )
3076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003077 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003082#if defined(MBEDTLS_SSL_CLI_C)
3083void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003084{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003085 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003086}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003087#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003088
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003089#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003090
Jerry Yud0766ec2022-09-22 10:46:57 +08003091#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003092void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
3093 uint16_t num_tickets )
3094{
Jerry Yud0766ec2022-09-22 10:46:57 +08003095 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003096}
3097#endif
3098
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003099void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
3100 mbedtls_ssl_ticket_write_t *f_ticket_write,
3101 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3102 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02003103{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003104 conf->f_ticket_write = f_ticket_write;
3105 conf->f_ticket_parse = f_ticket_parse;
3106 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003107}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003110
Hanno Becker7e6c1782021-06-08 09:24:55 +01003111void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3112 mbedtls_ssl_export_keys_t *f_export_keys,
3113 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003114{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003115 ssl->f_export_keys = f_export_keys;
3116 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003117}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003118
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003119#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003120void mbedtls_ssl_conf_async_private_cb(
3121 mbedtls_ssl_config *conf,
3122 mbedtls_ssl_async_sign_t *f_async_sign,
3123 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3124 mbedtls_ssl_async_resume_t *f_async_resume,
3125 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003126 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003127{
3128 conf->f_async_sign_start = f_async_sign;
3129 conf->f_async_decrypt_start = f_async_decrypt;
3130 conf->f_async_resume = f_async_resume;
3131 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003132 conf->p_async_config_data = async_config_data;
3133}
3134
Gilles Peskine8f97af72018-04-26 11:46:10 +02003135void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3136{
3137 return( conf->p_async_config_data );
3138}
3139
Gilles Peskine1febfef2018-04-30 11:54:39 +02003140void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003141{
3142 if( ssl->handshake == NULL )
3143 return( NULL );
3144 else
3145 return( ssl->handshake->user_async_ctx );
3146}
3147
Gilles Peskine1febfef2018-04-30 11:54:39 +02003148void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003149 void *ctx )
3150{
3151 if( ssl->handshake != NULL )
3152 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003153}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003154#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003155
Paul Bakker5121ce52009-01-03 21:22:43 +00003156/*
3157 * SSL get accessors
3158 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003159uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003160{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003161 if( ssl->session != NULL )
3162 return( ssl->session->verify_result );
3163
3164 if( ssl->session_negotiate != NULL )
3165 return( ssl->session_negotiate->verify_result );
3166
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003167 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003168}
3169
Glenn Strauss8f526902022-01-13 00:04:49 -05003170int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3171{
3172 if( ssl == NULL || ssl->session == NULL )
3173 return( 0 );
3174
3175 return( ssl->session->ciphersuite );
3176}
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003179{
Paul Bakker926c8e42013-03-06 10:23:34 +01003180 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003181 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003184}
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003189 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003190 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003191 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003192 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003193 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003194 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003195 default:
3196 return( "unknown (DTLS)" );
3197 }
3198 }
3199#endif
3200
Glenn Strauss60bfe602022-03-14 19:04:24 -04003201 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003202 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003203 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003205 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003206 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003207 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003208 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003209 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003210}
3211
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003213size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3214{
David Horstmann95d516f2021-05-04 18:36:56 +01003215 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003216 size_t read_mfl;
3217
3218 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3220 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3221 {
3222 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3223 }
3224
3225 /* Check if a smaller max length was negotiated */
3226 if( ssl->session_out != NULL )
3227 {
3228 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3229 if( read_mfl < max_len )
3230 {
3231 max_len = read_mfl;
3232 }
3233 }
3234
3235 // During a handshake, use the value being negotiated
3236 if( ssl->session_negotiate != NULL )
3237 {
3238 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3239 if( read_mfl < max_len )
3240 {
3241 max_len = read_mfl;
3242 }
3243 }
3244
3245 return( max_len );
3246}
3247
3248size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003249{
3250 size_t max_len;
3251
3252 /*
3253 * Assume mfl_code is correct since it was checked when set
3254 */
Angus Grattond8213d02016-05-25 20:56:48 +10003255 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003256
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003257 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003258 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003259 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003260 {
Angus Grattond8213d02016-05-25 20:56:48 +10003261 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003262 }
3263
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003264 /* During a handshake, use the value being negotiated */
3265 if( ssl->session_negotiate != NULL &&
3266 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3267 {
3268 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3269 }
3270
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003271 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003272}
3273#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3274
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003276size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003277{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003278 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3279 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3280 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3281 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3282 return ( 0 );
3283
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003284 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3285 return( ssl->mtu );
3286
3287 if( ssl->mtu == 0 )
3288 return( ssl->handshake->mtu );
3289
3290 return( ssl->mtu < ssl->handshake->mtu ?
3291 ssl->mtu : ssl->handshake->mtu );
3292}
3293#endif /* MBEDTLS_SSL_PROTO_DTLS */
3294
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003295int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3296{
3297 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3298
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003299#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3300 !defined(MBEDTLS_SSL_PROTO_DTLS)
3301 (void) ssl;
3302#endif
3303
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003304#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003305 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003306
3307 if( max_len > mfl )
3308 max_len = mfl;
3309#endif
3310
3311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003312 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313 {
Hanno Becker89490712020-02-05 10:50:12 +00003314 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003315 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3316 const size_t overhead = (size_t) ret;
3317
3318 if( ret < 0 )
3319 return( ret );
3320
3321 if( mtu <= overhead )
3322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3324 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3325 }
3326
3327 if( max_len > mtu - overhead )
3328 max_len = mtu - overhead;
3329 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003330#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003331
Hanno Becker0defedb2018-08-10 12:35:02 +01003332#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3333 !defined(MBEDTLS_SSL_PROTO_DTLS)
3334 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003335#endif
3336
3337 return( (int) max_len );
3338}
3339
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003340int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3341{
3342 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3343
3344#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3345 (void) ssl;
3346#endif
3347
3348#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3349 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3350
3351 if( max_len > mfl )
3352 max_len = mfl;
3353#endif
3354
3355 return( (int) max_len );
3356}
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_X509_CRT_PARSE_C)
3359const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003360{
3361 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003362 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003363
Hanno Beckere6824572019-02-07 13:18:46 +00003364#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003365 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003366#else
3367 return( NULL );
3368#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003373int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3374 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003375{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003376 int ret;
3377
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003378 if( ssl == NULL ||
3379 dst == NULL ||
3380 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003381 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003384 }
3385
Hanno Beckere810bbc2021-05-14 16:01:05 +01003386 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3387 * idempotent: Each session can only be exported once.
3388 *
3389 * (This is in preparation for TLS 1.3 support where we will
3390 * need the ability to export multiple sessions (aka tickets),
3391 * which will be achieved by calling mbedtls_ssl_get_session()
3392 * multiple times until it fails.)
3393 *
3394 * Check whether we have already exported the current session,
3395 * and fail if so.
3396 */
3397 if( ssl->session->exported == 1 )
3398 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3399
3400 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3401 if( ret != 0 )
3402 return( ret );
3403
3404 /* Remember that we've exported the session. */
3405 ssl->session->exported = 1;
3406 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003409
Paul Bakker5121ce52009-01-03 21:22:43 +00003410/*
Hanno Beckera835da52019-05-16 12:39:07 +01003411 * Define ticket header determining Mbed TLS version
3412 * and structure of the ticket.
3413 */
3414
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415/*
Hanno Becker50b59662019-05-28 14:30:45 +01003416 * Define bitflag determining compile-time settings influencing
3417 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003418 */
3419
Hanno Becker50b59662019-05-28 14:30:45 +01003420#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003421#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003422#else
Hanno Becker3e088662019-05-29 11:10:18 +01003423#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003424#endif /* MBEDTLS_HAVE_TIME */
3425
3426#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003427#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003428#else
Hanno Becker3e088662019-05-29 11:10:18 +01003429#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003430#endif /* MBEDTLS_X509_CRT_PARSE_C */
3431
3432#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003433#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003434#else
Hanno Becker3e088662019-05-29 11:10:18 +01003435#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003436#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3437
3438#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003439#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003440#else
Hanno Becker3e088662019-05-29 11:10:18 +01003441#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003442#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3443
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003445#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003446#else
Hanno Becker3e088662019-05-29 11:10:18 +01003447#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003448#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3449
Hanno Becker94ef3b32019-05-16 12:50:45 +01003450#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3451#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3452#else
3453#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3454#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3455
Hanno Becker3e088662019-05-29 11:10:18 +01003456#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3457#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3458#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3459#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003460#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3461#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003462
Hanno Becker50b59662019-05-28 14:30:45 +01003463#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003464 ( (uint16_t) ( \
3465 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3466 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3467 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3468 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003469 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003470 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003471
Hanno Beckerf8787072019-05-16 12:41:07 +01003472static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003473 MBEDTLS_VERSION_MAJOR,
3474 MBEDTLS_VERSION_MINOR,
3475 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003476 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3477 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003478};
Hanno Beckera835da52019-05-16 12:39:07 +01003479
3480/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003481 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003482 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003483 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003484 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003485 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003486 * opaque mbedtls_version[3]; // library version: major, minor, patch
3487 * opaque session_format[2]; // library-version specific 16-bit field
3488 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003489 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003490 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003491 * Note: When updating the format, remember to keep
3492 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003493 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003494 * // In this version, `session_format` determines
3495 * // the setting of those compile-time
3496 * // configuration options which influence
3497 * // the structure of mbedtls_ssl_session.
3498 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003499 * uint8_t minor_ver; // Protocol minor version. Possible values:
3500 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003502 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003504 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003505 * serialized_session_tls12 data;
3506 *
3507 * };
3508 *
3509 * } serialized_session;
3510 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003511 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003512
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003513MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003514static int ssl_session_save( const mbedtls_ssl_session *session,
3515 unsigned char omit_header,
3516 unsigned char *buf,
3517 size_t buf_len,
3518 size_t *olen )
3519{
3520 unsigned char *p = buf;
3521 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003522 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003523#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3524 size_t out_len;
3525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3526#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003527 if( session == NULL )
3528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3529
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003530 if( !omit_header )
3531 {
3532 /*
3533 * Add Mbed TLS version identifier
3534 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003535 used += sizeof( ssl_serialized_session_header );
3536
3537 if( used <= buf_len )
3538 {
3539 memcpy( p, ssl_serialized_session_header,
3540 sizeof( ssl_serialized_session_header ) );
3541 p += sizeof( ssl_serialized_session_header );
3542 }
3543 }
3544
3545 /*
3546 * TLS version identifier
3547 */
3548 used += 1;
3549 if( used <= buf_len )
3550 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003551 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003552 }
3553
3554 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003555 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003556 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003557 {
3558#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003559 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003560 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003561 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3563
Jerry Yu251a12e2022-07-13 15:15:48 +08003564#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3565 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003566 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3567 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003568 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003569 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003570 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003571#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3572
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003573 default:
3574 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3575 }
3576
3577 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003578 if( used > buf_len )
3579 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003580
3581 return( 0 );
3582}
3583
3584/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003585 * Public wrapper for ssl_session_save()
3586 */
3587int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3588 unsigned char *buf,
3589 size_t buf_len,
3590 size_t *olen )
3591{
3592 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3593}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003594
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003595/*
3596 * Deserialize session, see mbedtls_ssl_session_save() for format.
3597 *
3598 * This internal version is wrapped by a public function that cleans up in
3599 * case of error, and has an extra option omit_header.
3600 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003601MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003602static int ssl_session_load( mbedtls_ssl_session *session,
3603 unsigned char omit_header,
3604 const unsigned char *buf,
3605 size_t len )
3606{
3607 const unsigned char *p = buf;
3608 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003609 size_t remaining_len;
3610
3611
3612 if( session == NULL )
3613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003614
3615 if( !omit_header )
3616 {
3617 /*
3618 * Check Mbed TLS version identifier
3619 */
3620
3621 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3623
3624 if( memcmp( p, ssl_serialized_session_header,
3625 sizeof( ssl_serialized_session_header ) ) != 0 )
3626 {
3627 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3628 }
3629 p += sizeof( ssl_serialized_session_header );
3630 }
3631
3632 /*
3633 * TLS version identifier
3634 */
3635 if( 1 > (size_t)( end - p ) )
3636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003637 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003638
3639 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003640 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003641 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003642 {
3643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003644 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003645 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003646#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3647
Jerry Yu438ddd82022-07-07 06:55:50 +00003648#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3649 case MBEDTLS_SSL_VERSION_TLS1_3:
3650 return( ssl_tls13_session_load( session, p, remaining_len ) );
3651#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3652
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653 default:
3654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3655 }
3656}
3657
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003658/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003659 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003660 */
3661int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3662 const unsigned char *buf,
3663 size_t len )
3664{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003665 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003666
3667 if( ret != 0 )
3668 mbedtls_ssl_session_free( session );
3669
3670 return( ret );
3671}
3672
3673/*
Paul Bakker1961b702013-01-25 14:49:24 +01003674 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003676MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003677static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3678{
3679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3680
Ronald Cron66dbf912022-02-02 15:33:46 +01003681 /*
3682 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003683 * that were written into the output buffer by the previous handshake step,
3684 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003685 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3686 * We proceed to the next handshake step only when all data from the
3687 * previous one have been sent to the peer, thus we make sure that this is
3688 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3689 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3690 * we have to wait before to go ahead.
3691 * In the case of TLS 1.3, handshake step handlers do not send data to the
3692 * peer. Data are only sent here and through
3693 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003694 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003695 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003696 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3697 return( ret );
3698
3699#if defined(MBEDTLS_SSL_PROTO_DTLS)
3700 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3701 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3702 {
3703 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3704 return( ret );
3705 }
3706#endif /* MBEDTLS_SSL_PROTO_DTLS */
3707
3708 return( ret );
3709}
3710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712{
Hanno Becker41934dd2021-08-07 19:13:43 +01003713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
Hanno Becker41934dd2021-08-07 19:13:43 +01003715 if( ssl == NULL ||
3716 ssl->conf == NULL ||
3717 ssl->handshake == NULL ||
Jerry Yu1fb32992022-10-27 13:18:19 +08003718 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker41934dd2021-08-07 19:13:43 +01003719 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003721 }
3722
3723 ret = ssl_prepare_handshake_step( ssl );
3724 if( ret != 0 )
3725 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003726
Jerry Yue7047812021-09-13 19:26:39 +08003727 ret = mbedtls_ssl_handle_pending_alert( ssl );
3728 if( ret != 0 )
3729 goto cleanup;
3730
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003731 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3732 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3733 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003736 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003737 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3739 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003740
Ronald Cron9f0fba32022-02-10 16:45:15 +01003741 switch( ssl->state )
3742 {
3743 case MBEDTLS_SSL_HELLO_REQUEST:
3744 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003745 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003746 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003747
Ronald Cron9f0fba32022-02-10 16:45:15 +01003748 case MBEDTLS_SSL_CLIENT_HELLO:
3749 ret = mbedtls_ssl_write_client_hello( ssl );
3750 break;
3751
3752 default:
3753#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003754 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003755 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3756 else
3757 ret = mbedtls_ssl_handshake_client_step( ssl );
3758#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3759 ret = mbedtls_ssl_handshake_client_step( ssl );
3760#else
3761 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3762#endif
3763 }
Jerry Yub9930e72021-08-06 17:11:51 +08003764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003767 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003768 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003769#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003770 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003771 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003772#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003773
3774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3775 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3776 ret = mbedtls_ssl_handshake_server_step( ssl );
3777#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3778 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003779#endif
3780
Jerry Yue7047812021-09-13 19:26:39 +08003781 if( ret != 0 )
3782 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003783 /* handshake_step return error. And it is same
3784 * with alert_reason.
3785 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003786 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003787 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003788 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003789 goto cleanup;
3790 }
3791 }
3792
3793cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003794 return( ret );
3795}
3796
3797/*
3798 * Perform the SSL handshake
3799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003801{
3802 int ret = 0;
3803
Hanno Beckera817ea42020-10-20 15:20:23 +01003804 /* Sanity checks */
3805
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003806 if( ssl == NULL || ssl->conf == NULL )
3807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3808
Hanno Beckera817ea42020-10-20 15:20:23 +01003809#if defined(MBEDTLS_SSL_PROTO_DTLS)
3810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3811 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3812 {
3813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3814 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3816 }
3817#endif /* MBEDTLS_SSL_PROTO_DTLS */
3818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003820
Hanno Beckera817ea42020-10-20 15:20:23 +01003821 /* Main handshake loop */
Jerry Yu1fb32992022-10-27 13:18:19 +08003822 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003825
3826 if( ret != 0 )
3827 break;
3828 }
3829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003831
3832 return( ret );
3833}
3834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_RENEGOTIATION)
3836#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003837/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003838 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003839 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003840MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003842{
Janos Follath865b3eb2019-12-16 11:46:15 +00003843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003846
3847 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3849 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003850
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003851 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003852 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003854 return( ret );
3855 }
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003858
3859 return( 0 );
3860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003862
3863/*
3864 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 * - any side: calling mbedtls_ssl_renegotiate(),
3866 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3867 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003868 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003869 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003872int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003873{
Janos Follath865b3eb2019-12-16 11:46:15 +00003874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003877
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003878 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3879 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003880
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003881 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3882 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003886 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003888 ssl->handshake->out_msg_seq = 1;
3889 else
3890 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003891 }
3892#endif
3893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3895 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003900 return( ret );
3901 }
3902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003904
3905 return( 0 );
3906}
3907
3908/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003909 * Renegotiate current connection on client,
3910 * or request renegotiation on server
3911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003915
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003916 if( ssl == NULL || ssl->conf == NULL )
3917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003920 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003921 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003922 {
Jerry Yu6848a612022-10-27 13:03:26 +08003923 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003927
3928 /* Did we already try/start sending HelloRequest? */
3929 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003931
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003937 /*
3938 * On client, either start the renegotiation process or,
3939 * if already in progress, continue the handshake
3940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003942 {
Jerry Yu6848a612022-10-27 13:03:26 +08003943 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003945
Hanno Becker40cdaa12020-02-05 10:48:27 +00003946 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003947 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003949 return( ret );
3950 }
3951 }
3952 else
3953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003957 return( ret );
3958 }
3959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003961
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003962 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003965
Gilles Peskine9b562d52018-04-25 20:32:43 +02003966void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003967{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003968 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3969
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003970 if( handshake == NULL )
3971 return;
3972
Brett Warrene0edc842021-08-17 09:53:13 +01003973#if defined(MBEDTLS_ECP_C)
3974#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3975 if ( ssl->handshake->group_list_heap_allocated )
3976 mbedtls_free( (void*) handshake->group_list );
3977 handshake->group_list = NULL;
3978#endif /* MBEDTLS_DEPRECATED_REMOVED */
3979#endif /* MBEDTLS_ECP_C */
3980
Ronald Crone68ab4f2022-10-05 12:46:29 +02003981#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003982#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3983 if ( ssl->handshake->sig_algs_heap_allocated )
3984 mbedtls_free( (void*) handshake->sig_algs );
3985 handshake->sig_algs = NULL;
3986#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003987#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3988 if( ssl->handshake->certificate_request_context )
3989 {
3990 mbedtls_free( (void*) handshake->certificate_request_context );
3991 }
3992#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003993#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003994
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003995#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3996 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3997 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003998 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003999 handshake->async_in_progress = 0;
4000 }
4001#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4002
Andrzej Kurek25f27152022-08-17 16:09:31 -04004003#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004004#if defined(MBEDTLS_USE_PSA_CRYPTO)
4005 psa_hash_abort( &handshake->fin_sha256_psa );
4006#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004007 mbedtls_sha256_free( &handshake->fin_sha256 );
4008#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004009#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004010#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004011#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05004012 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05004013#else
Andrzej Kureka242e832022-08-11 10:03:14 -04004014 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004015#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004016#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018#if defined(MBEDTLS_DHM_C)
4019 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00004020#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004021#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02004023#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004024
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004025#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004026#if defined(MBEDTLS_USE_PSA_CRYPTO)
4027 psa_pake_abort( &handshake->psa_pake_ctx );
Valerio Settieb3f7882022-12-08 18:42:58 +01004028 /*
4029 * Opaque keys are not stored in the handshake's data and it's the user
4030 * responsibility to destroy them. Clear ones, instead, are created by
4031 * the TLS library and should be destroyed at the same level
4032 */
4033 if( ! mbedtls_svc_key_id_is_null( handshake->psa_pake_password ) )
4034 {
4035 psa_destroy_key( handshake->psa_pake_password );
4036 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004037 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4038#else
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004039 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Neil Armstrongca7d5062022-05-31 14:43:23 +02004040#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004041#if defined(MBEDTLS_SSL_CLI_C)
4042 mbedtls_free( handshake->ecjpake_cache );
4043 handshake->ecjpake_cache = NULL;
4044 handshake->ecjpake_cache_len = 0;
4045#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004046#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004047
Janos Follath4ae5c292016-02-10 11:27:43 +00004048#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4049 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004050 /* explicit void pointer cast for buggy MS compiler */
Valerio Setti6eca7b42022-12-30 17:44:24 +01004051 mbedtls_free( (void *) handshake->curves_tls_id );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004052#endif
4053
Ronald Cron73fe8df2022-10-05 14:31:43 +02004054#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004055#if defined(MBEDTLS_USE_PSA_CRYPTO)
4056 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
4057 {
4058 /* The maintenance of the external PSK key slot is the
4059 * user's responsibility. */
4060 if( ssl->handshake->psk_opaque_is_internal )
4061 {
4062 psa_destroy_key( ssl->handshake->psk_opaque );
4063 ssl->handshake->psk_opaque_is_internal = 0;
4064 }
4065 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4066 }
Neil Armstronge952a302022-05-03 10:22:14 +02004067#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004068 if( handshake->psk != NULL )
4069 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004070 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004071 mbedtls_free( handshake->psk );
4072 }
Neil Armstronge952a302022-05-03 10:22:14 +02004073#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004074#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4077 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004078 /*
4079 * Free only the linked list wrapper, not the keys themselves
4080 * since the belong to the SNI callback
4081 */
Glenn Strauss36872db2022-01-22 05:06:31 -05004082 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004084
Gilles Peskineeccd8882020-03-10 12:19:08 +01004085#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02004086 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00004087 if( handshake->ecrs_peer_cert != NULL )
4088 {
4089 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
4090 mbedtls_free( handshake->ecrs_peer_cert );
4091 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004092#endif
4093
Hanno Becker75173122019-02-06 16:18:31 +00004094#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4095 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4096 mbedtls_pk_free( &handshake->peer_pubkey );
4097#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4098
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004099#if defined(MBEDTLS_SSL_CLI_C) && \
4100 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
4101 mbedtls_free( handshake->cookie );
4102#endif /* MBEDTLS_SSL_CLI_C &&
4103 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004104
4105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00004106 mbedtls_ssl_flight_free( handshake->flight );
4107 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00004108#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004109
Ronald Cronf12b81d2022-03-15 10:42:41 +01004110#if defined(MBEDTLS_ECDH_C) && \
4111 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02004112 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01004113 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00004114#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4115
Ronald Cron6f135e12021-12-08 16:57:54 +01004116#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08004117 mbedtls_ssl_transform_free( handshake->transform_handshake );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004118 mbedtls_free( handshake->transform_handshake );
4119#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yua1a568c2021-11-09 10:17:21 +08004120 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004121 mbedtls_free( handshake->transform_earlydata );
Jerry Yu3d9b5902022-11-04 14:07:25 +08004122#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004123#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004124
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004125
4126#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4127 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4128 * processes datagrams and the fact that a datagram is allowed to have
4129 * several records in it, it is possible that the I/O buffers are not
4130 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004131 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4132 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004133#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004134
Jerry Yuba9c7272021-10-30 11:54:10 +08004135 /* mbedtls_platform_zeroize MUST be last one in this function */
4136 mbedtls_platform_zeroize( handshake,
4137 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004138}
4139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004141{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004142 if( session == NULL )
4143 return;
4144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004146 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004147#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004148
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004149#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004150#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4151 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004152 mbedtls_free( session->hostname );
4153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004154 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004155#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004157 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004158}
4159
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004160#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004161
4162#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4163#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4164#else
4165#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4167
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004168#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004169
4170#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4171#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4172#else
4173#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4174#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4175
4176#if defined(MBEDTLS_SSL_ALPN)
4177#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4178#else
4179#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4180#endif /* MBEDTLS_SSL_ALPN */
4181
4182#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4183#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4184#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4185#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4186
4187#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4188 ( (uint32_t) ( \
4189 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4190 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4191 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4192 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4193 0u ) )
4194
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004195static unsigned char ssl_serialized_context_header[] = {
4196 MBEDTLS_VERSION_MAJOR,
4197 MBEDTLS_VERSION_MINOR,
4198 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004199 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4200 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4201 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4202 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4203 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004204};
4205
Paul Bakker5121ce52009-01-03 21:22:43 +00004206/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004207 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004208 *
4209 * The format of the serialized data is:
4210 * (in the presentation language of TLS, RFC 8446 section 3)
4211 *
4212 * // header
4213 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004214 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004215 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004216 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004217 * Note: When updating the format, remember to keep these
4218 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004219 *
4220 * // session sub-structure
4221 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4222 * // transform sub-structure
4223 * uint8 random[64]; // ServerHello.random+ClientHello.random
4224 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4225 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4226 * // fields from ssl_context
4227 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4228 * uint64 in_window_top; // DTLS: last validated record seq_num
4229 * uint64 in_window; // DTLS: bitmask for replay protection
4230 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4231 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4232 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4233 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4234 *
4235 * Note that many fields of the ssl_context or sub-structures are not
4236 * serialized, as they fall in one of the following categories:
4237 *
4238 * 1. forced value (eg in_left must be 0)
4239 * 2. pointer to dynamically-allocated memory (eg session, transform)
4240 * 3. value can be re-derived from other data (eg session keys from MS)
4241 * 4. value was temporary (eg content of input buffer)
4242 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004243 */
4244int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4245 unsigned char *buf,
4246 size_t buf_len,
4247 size_t *olen )
4248{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004249 unsigned char *p = buf;
4250 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004251 size_t session_len;
4252 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004253
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004254 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004255 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4256 * this function's documentation.
4257 *
4258 * These are due to assumptions/limitations in the implementation. Some of
4259 * them are likely to stay (no handshake in progress) some might go away
4260 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004261 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004262 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004263 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004264 {
4265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004267 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004268 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004269 {
4270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004272 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004273 /* Double-check that sub-structures are indeed ready */
4274 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004275 {
4276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004278 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004279 /* There must be no pending incoming or outgoing data */
4280 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004281 {
4282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +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->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004286 {
4287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
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 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004290 /* Protocol must be DTLS, not TLS */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004291 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004292 {
4293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
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 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004297 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 {
4299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
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 /* We must be using an AEAD ciphersuite */
4303 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004304 {
4305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004307 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004308 /* Renegotiation must not be enabled */
4309#if defined(MBEDTLS_SSL_RENEGOTIATION)
4310 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004311 {
4312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004314 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004315#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004316
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004317 /*
4318 * Version and format identifier
4319 */
4320 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004321
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004322 if( used <= buf_len )
4323 {
4324 memcpy( p, ssl_serialized_context_header,
4325 sizeof( ssl_serialized_context_header ) );
4326 p += sizeof( ssl_serialized_context_header );
4327 }
4328
4329 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004330 * Session (length + data)
4331 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004332 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004333 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4334 return( ret );
4335
4336 used += 4 + session_len;
4337 if( used <= buf_len )
4338 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004339 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4340 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004341
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004342 ret = ssl_session_save( ssl->session, 1,
4343 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004344 if( ret != 0 )
4345 return( ret );
4346
4347 p += session_len;
4348 }
4349
4350 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004351 * Transform
4352 */
4353 used += sizeof( ssl->transform->randbytes );
4354 if( used <= buf_len )
4355 {
4356 memcpy( p, ssl->transform->randbytes,
4357 sizeof( ssl->transform->randbytes ) );
4358 p += sizeof( ssl->transform->randbytes );
4359 }
4360
4361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4362 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4363 if( used <= buf_len )
4364 {
4365 *p++ = ssl->transform->in_cid_len;
4366 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4367 p += ssl->transform->in_cid_len;
4368
4369 *p++ = ssl->transform->out_cid_len;
4370 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4371 p += ssl->transform->out_cid_len;
4372 }
4373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4374
4375 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004376 * Saved fields from top-level ssl_context structure
4377 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004378 used += 4;
4379 if( used <= buf_len )
4380 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004381 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4382 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004383 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004384
4385#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4386 used += 16;
4387 if( used <= buf_len )
4388 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004389 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4390 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004391
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004392 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4393 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004394 }
4395#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4396
4397#if defined(MBEDTLS_SSL_PROTO_DTLS)
4398 used += 1;
4399 if( used <= buf_len )
4400 {
4401 *p++ = ssl->disable_datagram_packing;
4402 }
4403#endif /* MBEDTLS_SSL_PROTO_DTLS */
4404
Jerry Yuae0b2e22021-10-08 15:21:19 +08004405 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004406 if( used <= buf_len )
4407 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004408 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4409 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004410 }
4411
4412#if defined(MBEDTLS_SSL_PROTO_DTLS)
4413 used += 2;
4414 if( used <= buf_len )
4415 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004416 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4417 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004418 }
4419#endif /* MBEDTLS_SSL_PROTO_DTLS */
4420
4421#if defined(MBEDTLS_SSL_ALPN)
4422 {
4423 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004424 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004425 : 0;
4426
4427 used += 1 + alpn_len;
4428 if( used <= buf_len )
4429 {
4430 *p++ = alpn_len;
4431
4432 if( ssl->alpn_chosen != NULL )
4433 {
4434 memcpy( p, ssl->alpn_chosen, alpn_len );
4435 p += alpn_len;
4436 }
4437 }
4438 }
4439#endif /* MBEDTLS_SSL_ALPN */
4440
4441 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004442 * Done
4443 */
4444 *olen = used;
4445
4446 if( used > buf_len )
4447 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004448
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004449 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4450
Hanno Becker43aefe22020-02-05 10:44:56 +00004451 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004452}
4453
4454/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004455 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004456 *
4457 * This internal version is wrapped by a public function that cleans up in
4458 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004459 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004460MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004461static int ssl_context_load( mbedtls_ssl_context *ssl,
4462 const unsigned char *buf,
4463 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004464{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004465 const unsigned char *p = buf;
4466 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004467 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004469#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004470 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004471#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004472
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004473 /*
4474 * The context should have been freshly setup or reset.
4475 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004476 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004477 * renegotiating, or if the user mistakenly loaded a session first.)
4478 */
4479 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4480 ssl->session != NULL )
4481 {
4482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4483 }
4484
4485 /*
4486 * We can't check that the config matches the initial one, but we can at
4487 * least check it matches the requirements for serializing.
4488 */
4489 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004490 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4491 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004492#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004493 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004494#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004495 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004496 {
4497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4498 }
4499
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004500 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4501
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004502 /*
4503 * Check version identifier
4504 */
4505 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4507
4508 if( memcmp( p, ssl_serialized_context_header,
4509 sizeof( ssl_serialized_context_header ) ) != 0 )
4510 {
4511 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4512 }
4513 p += sizeof( ssl_serialized_context_header );
4514
4515 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004516 * Session
4517 */
4518 if( (size_t)( end - p ) < 4 )
4519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4520
4521 session_len = ( (size_t) p[0] << 24 ) |
4522 ( (size_t) p[1] << 16 ) |
4523 ( (size_t) p[2] << 8 ) |
4524 ( (size_t) p[3] );
4525 p += 4;
4526
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004527 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004528 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004529 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004530 ssl->session_in = ssl->session;
4531 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004532 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533
4534 if( (size_t)( end - p ) < session_len )
4535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4536
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004537 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004539 {
4540 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004541 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004542 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004543
4544 p += session_len;
4545
4546 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004547 * Transform
4548 */
4549
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004550 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004551 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004552 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004553 ssl->transform_in = ssl->transform;
4554 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004555 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004556
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004557#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004558 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4559 if( prf_func == NULL )
4560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4561
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004562 /* Read random bytes and populate structure */
4563 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004565
Hanno Beckerbd257552021-03-22 06:59:27 +00004566 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004567 ssl->session->ciphersuite,
4568 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004569#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004570 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004571#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004572 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004573 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004574 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004575 ssl->conf->endpoint,
4576 ssl );
4577 if( ret != 0 )
4578 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004580 p += sizeof( ssl->transform->randbytes );
4581
4582#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4583 /* Read connection IDs and store them */
4584 if( (size_t)( end - p ) < 1 )
4585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4586
4587 ssl->transform->in_cid_len = *p++;
4588
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004589 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4591
4592 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4593 p += ssl->transform->in_cid_len;
4594
4595 ssl->transform->out_cid_len = *p++;
4596
4597 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4599
4600 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4601 p += ssl->transform->out_cid_len;
4602#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4603
4604 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004605 * Saved fields from top-level ssl_context structure
4606 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004607 if( (size_t)( end - p ) < 4 )
4608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4609
4610 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4611 ( (uint32_t) p[1] << 16 ) |
4612 ( (uint32_t) p[2] << 8 ) |
4613 ( (uint32_t) p[3] );
4614 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004615
4616#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4617 if( (size_t)( end - p ) < 16 )
4618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4619
4620 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4621 ( (uint64_t) p[1] << 48 ) |
4622 ( (uint64_t) p[2] << 40 ) |
4623 ( (uint64_t) p[3] << 32 ) |
4624 ( (uint64_t) p[4] << 24 ) |
4625 ( (uint64_t) p[5] << 16 ) |
4626 ( (uint64_t) p[6] << 8 ) |
4627 ( (uint64_t) p[7] );
4628 p += 8;
4629
4630 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4631 ( (uint64_t) p[1] << 48 ) |
4632 ( (uint64_t) p[2] << 40 ) |
4633 ( (uint64_t) p[3] << 32 ) |
4634 ( (uint64_t) p[4] << 24 ) |
4635 ( (uint64_t) p[5] << 16 ) |
4636 ( (uint64_t) p[6] << 8 ) |
4637 ( (uint64_t) p[7] );
4638 p += 8;
4639#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4640
4641#if defined(MBEDTLS_SSL_PROTO_DTLS)
4642 if( (size_t)( end - p ) < 1 )
4643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4644
4645 ssl->disable_datagram_packing = *p++;
4646#endif /* MBEDTLS_SSL_PROTO_DTLS */
4647
Jerry Yud9a94fe2021-09-28 18:58:59 +08004648 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004650 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4651 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004652
4653#if defined(MBEDTLS_SSL_PROTO_DTLS)
4654 if( (size_t)( end - p ) < 2 )
4655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4656
4657 ssl->mtu = ( p[0] << 8 ) | p[1];
4658 p += 2;
4659#endif /* MBEDTLS_SSL_PROTO_DTLS */
4660
4661#if defined(MBEDTLS_SSL_ALPN)
4662 {
4663 uint8_t alpn_len;
4664 const char **cur;
4665
4666 if( (size_t)( end - p ) < 1 )
4667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4668
4669 alpn_len = *p++;
4670
4671 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4672 {
4673 /* alpn_chosen should point to an item in the configured list */
4674 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4675 {
4676 if( strlen( *cur ) == alpn_len &&
4677 memcmp( p, cur, alpn_len ) == 0 )
4678 {
4679 ssl->alpn_chosen = *cur;
4680 break;
4681 }
4682 }
4683 }
4684
4685 /* can only happen on conf mismatch */
4686 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4688
4689 p += alpn_len;
4690 }
4691#endif /* MBEDTLS_SSL_ALPN */
4692
4693 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004694 * Forced fields from top-level ssl_context structure
4695 *
4696 * Most of them already set to the correct value by mbedtls_ssl_init() and
4697 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4698 */
4699 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004700 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004701
Hanno Becker361b10d2019-08-30 10:42:49 +01004702 /* Adjust pointers for header fields of outgoing records to
4703 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004704 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004705
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004706#if defined(MBEDTLS_SSL_PROTO_DTLS)
4707 ssl->in_epoch = 1;
4708#endif
4709
4710 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4711 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004712 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4713 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004714 if( ssl->handshake != NULL )
4715 {
4716 mbedtls_ssl_handshake_free( ssl );
4717 mbedtls_free( ssl->handshake );
4718 ssl->handshake = NULL;
4719 }
4720
4721 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004722 * Done - should have consumed entire buffer
4723 */
4724 if( p != end )
4725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004726
4727 return( 0 );
4728}
4729
4730/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004731 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004732 */
4733int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4734 const unsigned char *buf,
4735 size_t len )
4736{
4737 int ret = ssl_context_load( context, buf, len );
4738
4739 if( ret != 0 )
4740 mbedtls_ssl_free( context );
4741
4742 return( ret );
4743}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004744#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004745
4746/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004747 * Free an SSL context
4748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004750{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004751 if( ssl == NULL )
4752 return;
4753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004755
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004756 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004757 {
sander-visserb8aa2072020-05-06 22:05:13 +02004758#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4759 size_t out_buf_len = ssl->out_buf_len;
4760#else
4761 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4762#endif
4763
Darryl Greenb33cc762019-11-28 14:29:44 +00004764 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004766 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 }
4768
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004769 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004770 {
sander-visserb8aa2072020-05-06 22:05:13 +02004771#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4772 size_t in_buf_len = ssl->in_buf_len;
4773#else
4774 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4775#endif
4776
Darryl Greenb33cc762019-11-28 14:29:44 +00004777 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004779 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
4781
Paul Bakker48916f92012-09-16 19:57:18 +00004782 if( ssl->transform )
4783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 mbedtls_ssl_transform_free( ssl->transform );
4785 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004786 }
4787
4788 if( ssl->handshake )
4789 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004790 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4792 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 mbedtls_free( ssl->handshake );
4795 mbedtls_free( ssl->transform_negotiate );
4796 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004797 }
4798
Ronald Cron6f135e12021-12-08 16:57:54 +01004799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004800 mbedtls_ssl_transform_free( ssl->transform_application );
4801 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004802#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004803
Paul Bakkerc0463502013-02-14 11:19:38 +01004804 if( ssl->session )
4805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 mbedtls_ssl_session_free( ssl->session );
4807 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004808 }
4809
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004810#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004811 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004813 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004818#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004820#endif
4821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004823
Paul Bakker86f04f42013-02-14 11:20:09 +01004824 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004825 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826}
4827
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004828/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004829 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004830 */
4831void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4832{
4833 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4834}
4835
Gilles Peskineae270bf2021-06-02 00:05:29 +02004836/* The selection should be the same as mbedtls_x509_crt_profile_default in
4837 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004838 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004839 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004840 * about this list.
4841 */
Brett Warrene0edc842021-08-17 09:53:13 +01004842static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004843#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004844 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004845#endif
4846#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004847 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004848#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004849#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004850 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004851#endif
4852#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004853 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004854#endif
4855#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004856 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004857#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004858#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004859 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004860#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004861#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004862 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004863#endif
4864#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004865 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004866#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004867 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004868};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004869
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004870static int ssl_preset_suiteb_ciphersuites[] = {
4871 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4872 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4873 0
4874};
4875
Ronald Crone68ab4f2022-10-05 12:46:29 +02004876#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004877
Jerry Yu909df7b2022-01-22 11:56:27 +08004878/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004879 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004880 * rules SHOULD be upheld.
4881 * - No duplicate entries.
4882 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004883 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004884 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004885 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004886static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004887
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#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 +08004889 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4890 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004891#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004892 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004893
Andrzej Kurek25f27152022-08-17 16:09:31 -04004894#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 +08004895 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4896 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004897#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004898 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4899
Andrzej Kurek25f27152022-08-17 16:09:31 -04004900#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 +08004901 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4902 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004903#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004904 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004905
Andrzej Kurek25f27152022-08-17 16:09:31 -04004906#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 +08004907 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004908#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 +08004909
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#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 +08004911 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004912#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 +08004913
Andrzej Kurek25f27152022-08-17 16:09:31 -04004914#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 +08004915 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004916#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 +08004917
Andrzej Kurek25f27152022-08-17 16:09:31 -04004918#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 +08004919 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4920#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4921
Andrzej Kurek25f27152022-08-17 16:09:31 -04004922#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 +08004923 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4924#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4925
Andrzej Kurek25f27152022-08-17 16:09:31 -04004926#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 +08004927 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4928#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4929
Gabor Mezei15b95a62022-05-09 16:37:58 +02004930 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004931};
4932
4933/* NOTICE: see above */
4934#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4935static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004936#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004937#if defined(MBEDTLS_ECDSA_C)
4938 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004939#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004940#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4941 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4942#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004943#if defined(MBEDTLS_RSA_C)
4944 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4945#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004946#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004947#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004948#if defined(MBEDTLS_ECDSA_C)
4949 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004950#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004951#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4952 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4953#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004954#if defined(MBEDTLS_RSA_C)
4955 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4956#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004957#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004958#if defined(MBEDTLS_HAS_ALG_SHA_256_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_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +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_SHA256,
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_SHA256 ),
4967#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004968#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004969 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004970};
4971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4972/* NOTICE: see above */
4973static uint16_t ssl_preset_suiteb_sig_algs[] = {
4974
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#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 +08004976 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4977 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004978#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004979 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4980
Andrzej Kurek25f27152022-08-17 16:09:31 -04004981#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 +08004982 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4983 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004984#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004985 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004986
Andrzej Kurek25f27152022-08-17 16:09:31 -04004987#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 +08004988 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004989#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 +08004990
Andrzej Kurek25f27152022-08-17 16:09:31 -04004991#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 +08004992 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004993#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004994
Gabor Mezei15b95a62022-05-09 16:37:58 +02004995 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004996};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004997
Jerry Yu909df7b2022-01-22 11:56:27 +08004998/* NOTICE: see above */
4999#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5000static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005001#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005002#if defined(MBEDTLS_ECDSA_C)
5003 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08005004#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005005#if defined(MBEDTLS_RSA_C)
5006 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
5007#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005008#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005009#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005010#if defined(MBEDTLS_ECDSA_C)
5011 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08005012#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005013#if defined(MBEDTLS_RSA_C)
5014 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
5015#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005016#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005017 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005018};
Jerry Yu909df7b2022-01-22 11:56:27 +08005019#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5020
Ronald Crone68ab4f2022-10-05 12:46:29 +02005021#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005022
Brett Warrene0edc842021-08-17 09:53:13 +01005023static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005024#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005025 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005026#endif
5027#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005028 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005029#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005030 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005031};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005032
Ronald Crone68ab4f2022-10-05 12:46:29 +02005033#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005034/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005035 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005036MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08005037static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005038{
5039 size_t i, j;
5040 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005041
Gabor Mezei15b95a62022-05-09 16:37:58 +02005042 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005043 {
Jerry Yuf377d642022-01-25 10:43:59 +08005044 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005045 {
Jerry Yuf377d642022-01-25 10:43:59 +08005046 if( sig_algs[i] != sig_algs[j] )
5047 continue;
5048 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
5049 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5050 sig_algs[i], j, i );
5051 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005052 }
5053 }
5054 return( ret );
5055}
5056
Ronald Crone68ab4f2022-10-05 12:46:29 +02005057#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005058
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005059/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005060 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005061 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005062int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005063 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005064{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005065#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005067#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005068
Ronald Crone68ab4f2022-10-05 12:46:29 +02005069#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08005070 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005071 {
5072 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
5073 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5074 }
5075
Jerry Yuf377d642022-01-25 10:43:59 +08005076 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08005077 {
5078 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
5079 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5080 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005081
5082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08005083 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005084 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005085 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005086 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5087 }
5088
Jerry Yuf377d642022-01-25 10:43:59 +08005089 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08005090 {
Jerry Yu909df7b2022-01-22 11:56:27 +08005091 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08005092 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
5093 }
5094#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005095#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005096
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005097 /* Use the functions here so that they are covered in tests,
5098 * but otherwise access member directly for efficiency */
5099 mbedtls_ssl_conf_endpoint( conf, endpoint );
5100 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005101
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005102 /*
5103 * Things that are common to all presets
5104 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005105#if defined(MBEDTLS_SSL_CLI_C)
5106 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5107 {
5108 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5109#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5110 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5111#endif
5112 }
5113#endif
5114
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005115#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5116 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5117#endif
5118
5119#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5120 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5121#endif
5122
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005123#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005124 conf->f_cookie_write = ssl_cookie_write_dummy;
5125 conf->f_cookie_check = ssl_cookie_check_dummy;
5126#endif
5127
5128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5129 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5130#endif
5131
Janos Follath088ce432017-04-10 12:42:31 +01005132#if defined(MBEDTLS_SSL_SRV_C)
5133 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005134 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005135#endif
5136
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005137#if defined(MBEDTLS_SSL_PROTO_DTLS)
5138 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5139 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5140#endif
5141
5142#if defined(MBEDTLS_SSL_RENEGOTIATION)
5143 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005144 memset( conf->renego_period, 0x00, 2 );
5145 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005146#endif
5147
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005148#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005149 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5150 {
5151 const unsigned char dhm_p[] =
5152 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5153 const unsigned char dhm_g[] =
5154 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005155
Hanno Beckere2defad2021-07-24 05:59:17 +01005156 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5157 dhm_p, sizeof( dhm_p ),
5158 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5159 {
5160 return( ret );
5161 }
5162 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005163#endif
5164
Ronald Cron6f135e12021-12-08 16:57:54 +01005165#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005166
5167#if defined(MBEDTLS_SSL_EARLY_DATA)
5168 mbedtls_ssl_tls13_conf_early_data( conf, MBEDTLS_SSL_EARLY_DATA_DISABLED );
5169#if defined(MBEDTLS_SSL_SRV_C)
5170 mbedtls_ssl_tls13_conf_max_early_data_size(
5171 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE );
5172#endif
5173#endif /* MBEDTLS_SSL_EARLY_DATA */
5174
Jerry Yud0766ec2022-09-22 10:46:57 +08005175#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005176 mbedtls_ssl_conf_new_session_tickets(
5177 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5178#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005179 /*
5180 * Allow all TLS 1.3 key exchange modes by default.
5181 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005182 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005183#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005184
XiaokangQian4d3a6042022-04-21 13:46:17 +00005185 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5186 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005187#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005188 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005189 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005190#else
XiaokangQian060d8672022-04-21 09:24:56 +00005191 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005192#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005193 }
5194 else
5195 {
5196#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5197 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5198 {
5199 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5200 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5201 }
5202 else
5203 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5204 {
5205 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5206 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5207 }
5208#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5209 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5210 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5211#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5212 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5213 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5214#else
5215 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5216#endif
5217 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005218
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005219 /*
5220 * Preset-specific defaults
5221 */
5222 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005223 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224 /*
5225 * NSA Suite B
5226 */
5227 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005229 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005230
5231#if defined(MBEDTLS_X509_CRT_PARSE_C)
5232 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005233#endif
5234
Ronald Crone68ab4f2022-10-05 12:46:29 +02005235#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005236#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5237 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5238 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5239 else
5240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5241 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005242#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005243
Brett Warrene0edc842021-08-17 09:53:13 +01005244#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5245 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005246#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005247 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005248 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005249
5250 /*
5251 * Default
5252 */
5253 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005254
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005255 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005256
5257#if defined(MBEDTLS_X509_CRT_PARSE_C)
5258 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5259#endif
5260
Ronald Crone68ab4f2022-10-05 12:46:29 +02005261#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5263 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5264 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5265 else
5266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5267 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005268#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005269
Brett Warrene0edc842021-08-17 09:53:13 +01005270#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5271 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005272#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005273 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005274
5275#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5276 conf->dhm_min_bitlen = 1024;
5277#endif
5278 }
5279
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005280 return( 0 );
5281}
5282
5283/*
5284 * Free mbedtls_ssl_config
5285 */
5286void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5287{
5288#if defined(MBEDTLS_DHM_C)
5289 mbedtls_mpi_free( &conf->dhm_P );
5290 mbedtls_mpi_free( &conf->dhm_G );
5291#endif
5292
Ronald Cron73fe8df2022-10-05 14:31:43 +02005293#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005294#if defined(MBEDTLS_USE_PSA_CRYPTO)
5295 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5296 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005297 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5298 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005299#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005300 if( conf->psk != NULL )
5301 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005302 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005303 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005304 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005305 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005306 }
5307
5308 if( conf->psk_identity != NULL )
5309 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005310 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005311 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005312 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005313 conf->psk_identity_len = 0;
5314 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005315#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005316
5317#if defined(MBEDTLS_X509_CRT_PARSE_C)
5318 ssl_key_cert_free( conf->key_cert );
5319#endif
5320
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005321 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005322}
5323
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005324#if defined(MBEDTLS_PK_C) && \
5325 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005326/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_RSA_C)
5332 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5333 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_ECDSA_C)
5336 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5337 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005340}
5341
Hanno Becker7e5437a2017-04-28 17:15:26 +01005342unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5343{
5344 switch( type ) {
5345 case MBEDTLS_PK_RSA:
5346 return( MBEDTLS_SSL_SIG_RSA );
5347 case MBEDTLS_PK_ECDSA:
5348 case MBEDTLS_PK_ECKEY:
5349 return( MBEDTLS_SSL_SIG_ECDSA );
5350 default:
5351 return( MBEDTLS_SSL_SIG_ANON );
5352 }
5353}
5354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005356{
5357 switch( sig )
5358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#if defined(MBEDTLS_RSA_C)
5360 case MBEDTLS_SSL_SIG_RSA:
5361 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#if defined(MBEDTLS_ECDSA_C)
5364 case MBEDTLS_SSL_SIG_ECDSA:
5365 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005366#endif
5367 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005369 }
5370}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005371#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005373/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005374 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005377{
5378 switch( hash )
5379 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005380#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 case MBEDTLS_SSL_HASH_MD5:
5382 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005383#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005384#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 case MBEDTLS_SSL_HASH_SHA1:
5386 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005387#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005388#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 case MBEDTLS_SSL_HASH_SHA224:
5390 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005391#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005392#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_SSL_HASH_SHA256:
5394 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005395#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005396#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 case MBEDTLS_SSL_HASH_SHA384:
5398 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005399#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005400#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 case MBEDTLS_SSL_HASH_SHA512:
5402 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005403#endif
5404 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005406 }
5407}
5408
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005409/*
5410 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5411 */
5412unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5413{
5414 switch( md )
5415 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005416#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417 case MBEDTLS_MD_MD5:
5418 return( MBEDTLS_SSL_HASH_MD5 );
5419#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005420#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005421 case MBEDTLS_MD_SHA1:
5422 return( MBEDTLS_SSL_HASH_SHA1 );
5423#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005424#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005425 case MBEDTLS_MD_SHA224:
5426 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005427#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005428#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 case MBEDTLS_MD_SHA256:
5430 return( MBEDTLS_SSL_HASH_SHA256 );
5431#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005432#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005433 case MBEDTLS_MD_SHA384:
5434 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005435#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005436#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005437 case MBEDTLS_MD_SHA512:
5438 return( MBEDTLS_SSL_HASH_SHA512 );
5439#endif
5440 default:
5441 return( MBEDTLS_SSL_HASH_NONE );
5442 }
5443}
5444
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005445/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005447 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005449int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005450{
Brett Warrene0edc842021-08-17 09:53:13 +01005451 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005452
Brett Warrene0edc842021-08-17 09:53:13 +01005453 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005454 return( -1 );
5455
Brett Warrene0edc842021-08-17 09:53:13 +01005456 for( ; *group_list != 0; group_list++ )
5457 {
5458 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005459 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005460 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005461
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005462 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005463}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005464
5465#if defined(MBEDTLS_ECP_C)
5466/*
5467 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5468 */
5469int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5470{
Valerio Setti6eca7b42022-12-30 17:44:24 +01005471 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005472
Valerio Setti6eca7b42022-12-30 17:44:24 +01005473 if ( tls_id == 0 )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005474 return -1;
5475
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005476 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5477}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005478#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005479
Valerio Setti6eca7b42022-12-30 17:44:24 +01005480static const struct {
5481 uint16_t tls_id;
5482 mbedtls_ecp_group_id ecp_group_id;
5483 psa_ecc_family_t psa_family;
5484 uint16_t bits;
5485 const char* name;
5486} tls_id_match_table[] =
5487{
5488#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
5489 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, "secp521r1" },
5490#endif
5491#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
5492 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, "brainpoolP512r1" },
5493#endif
5494#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
5495 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, "secp384r1" },
5496#endif
5497#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
5498 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, "brainpoolP384r1" },
5499#endif
5500#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
5501 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, "secp256r1" },
5502#endif
5503#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
5504 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, "secp256k1" },
5505#endif
5506#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
5507 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, "brainpoolP256r1" },
5508#endif
5509#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
5510 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, "secp224r1" },
5511#endif
5512#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
5513 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, "secp224k1" },
5514#endif
5515#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
5516 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, "secp192r1" },
5517#endif
5518#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
5519 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, "secp192k1" },
5520#endif
5521#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
5522 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 256, "x25519" },
5523#endif
5524#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
5525 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, "x448" },
5526#endif
5527 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5528};
5529
5530int mbedtls_ssl_get_psa_curve_info_from_tls_id( uint16_t tls_id,
5531 psa_ecc_family_t *family,
5532 size_t* bits )
5533{
5534 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5535 {
5536 if( tls_id_match_table[i].tls_id == tls_id )
5537 {
5538 if( family != NULL )
5539 *family = tls_id_match_table[i].psa_family;
5540 if( bits != NULL )
5541 *bits = tls_id_match_table[i].bits;
5542 return PSA_SUCCESS;
5543 }
5544 }
5545
5546 return PSA_ERROR_NOT_SUPPORTED;
5547}
5548
5549mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id( uint16_t tls_id )
5550{
5551 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5552 {
5553 if( tls_id_match_table[i].tls_id == tls_id )
5554 return tls_id_match_table[i].ecp_group_id;
5555 }
5556
5557 return MBEDTLS_ECP_DP_NONE;
5558}
5559
5560uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id( mbedtls_ecp_group_id grp_id )
5561{
5562 for( int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5563 i++ )
5564 {
5565 if( tls_id_match_table[i].ecp_group_id == grp_id )
5566 return tls_id_match_table[i].tls_id;
5567 }
5568
5569 return 0;
5570}
5571
5572const char* mbedtls_ssl_get_curve_name_from_tls_id( uint16_t tls_id )
5573{
5574 for( int i = 0; tls_id_match_table[i].tls_id != 0; i++ )
5575 {
5576 if( tls_id_match_table[i].tls_id == tls_id )
5577 return tls_id_match_table[i].name;
5578 }
5579
5580 return NULL;
5581}
5582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_X509_CRT_PARSE_C)
5584int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5585 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005586 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005587 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005588{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005589 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005590 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005591 const char *ext_oid;
5592 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005595 {
5596 /* Server part of the key exchange */
5597 switch( ciphersuite->key_exchange )
5598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 case MBEDTLS_KEY_EXCHANGE_RSA:
5600 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005601 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005602 break;
5603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5605 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5606 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5607 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005608 break;
5609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5611 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005612 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005613 break;
5614
5615 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 case MBEDTLS_KEY_EXCHANGE_NONE:
5617 case MBEDTLS_KEY_EXCHANGE_PSK:
5618 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5619 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005620 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005621 usage = 0;
5622 }
5623 }
5624 else
5625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5627 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005628 }
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005631 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005632 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005633 ret = -1;
5634 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5639 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005640 }
5641 else
5642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5644 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005645 }
5646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005648 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005649 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005650 ret = -1;
5651 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005652
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005653 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005656
Jerry Yu148165c2021-09-24 23:20:59 +08005657#if defined(MBEDTLS_USE_PSA_CRYPTO)
5658int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5659 const mbedtls_md_type_t md,
5660 unsigned char *dst,
5661 size_t dst_len,
5662 size_t *olen )
5663{
Ronald Cronf6893e12022-01-07 22:09:01 +01005664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5665 psa_hash_operation_t *hash_operation_to_clone;
5666 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5667
Jerry Yu148165c2021-09-24 23:20:59 +08005668 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005669
5670 switch( md )
5671 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005672#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005673 case MBEDTLS_MD_SHA384:
5674 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5675 break;
5676#endif
5677
Andrzej Kurek25f27152022-08-17 16:09:31 -04005678#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005679 case MBEDTLS_MD_SHA256:
5680 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5681 break;
5682#endif
5683
5684 default:
5685 goto exit;
5686 }
5687
5688 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5689 if( status != PSA_SUCCESS )
5690 goto exit;
5691
5692 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5693 if( status != PSA_SUCCESS )
5694 goto exit;
5695
5696exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005697#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5698 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5699 (void) ssl;
5700#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005701 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005702}
5703#else /* MBEDTLS_USE_PSA_CRYPTO */
5704
Andrzej Kurek25f27152022-08-17 16:09:31 -04005705#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005706MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005707static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5708 unsigned char *dst,
5709 size_t dst_len,
5710 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005711{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005712 int ret;
5713 mbedtls_sha512_context sha512;
5714
5715 if( dst_len < 48 )
5716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5717
5718 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005719 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005720
5721 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5722 {
5723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5724 goto exit;
5725 }
5726
5727 *olen = 48;
5728
5729exit:
5730
5731 mbedtls_sha512_free( &sha512 );
5732 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005733}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005734#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005735
Andrzej Kurek25f27152022-08-17 16:09:31 -04005736#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005737MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005738static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5739 unsigned char *dst,
5740 size_t dst_len,
5741 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005742{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005743 int ret;
5744 mbedtls_sha256_context sha256;
5745
5746 if( dst_len < 32 )
5747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5748
5749 mbedtls_sha256_init( &sha256 );
5750 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005751
Jerry Yu24c0ec32021-09-09 14:21:07 +08005752 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5753 {
5754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5755 goto exit;
5756 }
5757
5758 *olen = 32;
5759
5760exit:
5761
5762 mbedtls_sha256_free( &sha256 );
5763 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005764}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005765#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005766
Jerry Yu000f9762021-09-14 11:12:51 +08005767int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5768 const mbedtls_md_type_t md,
5769 unsigned char *dst,
5770 size_t dst_len,
5771 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005772{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005773 switch( md )
5774 {
5775
Andrzej Kurek25f27152022-08-17 16:09:31 -04005776#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005777 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005778 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005779#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005780
Andrzej Kurek25f27152022-08-17 16:09:31 -04005781#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005782 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005783 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005784#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005785
5786 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005787#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5788 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5789 (void) ssl;
5790 (void) dst;
5791 (void) dst_len;
5792 (void) olen;
5793#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005794 break;
5795 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005797}
XiaokangQian647719a2021-12-07 09:16:29 +00005798
Jerry Yu148165c2021-09-24 23:20:59 +08005799#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005800
Ronald Crone68ab4f2022-10-05 12:46:29 +02005801#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005802/* mbedtls_ssl_parse_sig_alg_ext()
5803 *
5804 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5805 * value (TLS 1.3 RFC8446):
5806 * enum {
5807 * ....
5808 * ecdsa_secp256r1_sha256( 0x0403 ),
5809 * ecdsa_secp384r1_sha384( 0x0503 ),
5810 * ecdsa_secp521r1_sha512( 0x0603 ),
5811 * ....
5812 * } SignatureScheme;
5813 *
5814 * struct {
5815 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5816 * } SignatureSchemeList;
5817 *
5818 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5819 * value (TLS 1.2 RFC5246):
5820 * enum {
5821 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5822 * sha512(6), (255)
5823 * } HashAlgorithm;
5824 *
5825 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5826 * SignatureAlgorithm;
5827 *
5828 * struct {
5829 * HashAlgorithm hash;
5830 * SignatureAlgorithm signature;
5831 * } SignatureAndHashAlgorithm;
5832 *
5833 * SignatureAndHashAlgorithm
5834 * supported_signature_algorithms<2..2^16-2>;
5835 *
5836 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5837 * generalization of the TLS 1.2 signature algorithm extension.
5838 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5839 * `SignatureScheme` field of TLS 1.3
5840 *
5841 */
5842int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5843 const unsigned char *buf,
5844 const unsigned char *end )
5845{
5846 const unsigned char *p = buf;
5847 size_t supported_sig_algs_len = 0;
5848 const unsigned char *supported_sig_algs_end;
5849 uint16_t sig_alg;
5850 uint32_t common_idx = 0;
5851
5852 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5853 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5854 p += 2;
5855
5856 memset( ssl->handshake->received_sig_algs, 0,
5857 sizeof(ssl->handshake->received_sig_algs) );
5858
5859 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5860 supported_sig_algs_end = p + supported_sig_algs_len;
5861 while( p < supported_sig_algs_end )
5862 {
5863 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5864 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5865 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005866 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5867 sig_alg,
5868 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005870 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005871 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5872 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5873 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005874 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005875 }
5876#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005877
Jerry Yuf3b46b52022-06-19 16:52:27 +08005878 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5879 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005880
5881 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5882 {
5883 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5884 common_idx += 1;
5885 }
5886 }
5887 /* Check that we consumed all the message. */
5888 if( p != end )
5889 {
5890 MBEDTLS_SSL_DEBUG_MSG( 1,
5891 ( "Signature algorithms extension length misaligned" ) );
5892 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5893 MBEDTLS_ERR_SSL_DECODE_ERROR );
5894 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5895 }
5896
5897 if( common_idx == 0 )
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5900 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5901 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5902 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5903 }
5904
Gabor Mezei15b95a62022-05-09 16:37:58 +02005905 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005906 return( 0 );
5907}
5908
Ronald Crone68ab4f2022-10-05 12:46:29 +02005909#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005910
Jerry Yudc7bd172022-02-17 13:44:15 +08005911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5912
5913#if defined(MBEDTLS_USE_PSA_CRYPTO)
5914
5915static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5916 mbedtls_svc_key_id_t key,
5917 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005918 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005919 const unsigned char* seed, size_t seed_length,
5920 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005921 const unsigned char* other_secret,
5922 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005923 size_t capacity )
5924{
5925 psa_status_t status;
5926
5927 status = psa_key_derivation_setup( derivation, alg );
5928 if( status != PSA_SUCCESS )
5929 return( status );
5930
5931 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5932 {
5933 status = psa_key_derivation_input_bytes( derivation,
5934 PSA_KEY_DERIVATION_INPUT_SEED,
5935 seed, seed_length );
5936 if( status != PSA_SUCCESS )
5937 return( status );
5938
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005939 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005940 {
5941 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005942 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5943 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005944 if( status != PSA_SUCCESS )
5945 return( status );
5946 }
5947
Jerry Yudc7bd172022-02-17 13:44:15 +08005948 if( mbedtls_svc_key_id_is_null( key ) )
5949 {
5950 status = psa_key_derivation_input_bytes(
5951 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005952 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005953 }
5954 else
5955 {
5956 status = psa_key_derivation_input_key(
5957 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5958 }
5959 if( status != PSA_SUCCESS )
5960 return( status );
5961
5962 status = psa_key_derivation_input_bytes( derivation,
5963 PSA_KEY_DERIVATION_INPUT_LABEL,
5964 label, label_length );
5965 if( status != PSA_SUCCESS )
5966 return( status );
5967 }
5968 else
5969 {
5970 return( PSA_ERROR_NOT_SUPPORTED );
5971 }
5972
5973 status = psa_key_derivation_set_capacity( derivation, capacity );
5974 if( status != PSA_SUCCESS )
5975 return( status );
5976
5977 return( PSA_SUCCESS );
5978}
5979
Andrzej Kurek57d10632022-10-24 10:32:01 -04005980#if defined(PSA_WANT_ALG_SHA_384) || \
5981 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005982MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005983static int tls_prf_generic( mbedtls_md_type_t md_type,
5984 const unsigned char *secret, size_t slen,
5985 const char *label,
5986 const unsigned char *random, size_t rlen,
5987 unsigned char *dstbuf, size_t dlen )
5988{
5989 psa_status_t status;
5990 psa_algorithm_t alg;
5991 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5992 psa_key_derivation_operation_t derivation =
5993 PSA_KEY_DERIVATION_OPERATION_INIT;
5994
5995 if( md_type == MBEDTLS_MD_SHA384 )
5996 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5997 else
5998 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5999
6000 /* Normally a "secret" should be long enough to be impossible to
6001 * find by brute force, and in particular should not be empty. But
6002 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6003 * and for this use case it makes sense to have a 0-length "secret".
6004 * Since the key API doesn't allow importing a key of length 0,
6005 * keep master_key=0, which setup_psa_key_derivation() understands
6006 * to mean a 0-length "secret" input. */
6007 if( slen != 0 )
6008 {
6009 psa_key_attributes_t key_attributes = psa_key_attributes_init();
6010 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
6011 psa_set_key_algorithm( &key_attributes, alg );
6012 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
6013
6014 status = psa_import_key( &key_attributes, secret, slen, &master_key );
6015 if( status != PSA_SUCCESS )
6016 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6017 }
6018
6019 status = setup_psa_key_derivation( &derivation,
6020 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006021 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08006022 random, rlen,
6023 (unsigned char const *) label,
6024 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02006025 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08006026 dlen );
6027 if( status != PSA_SUCCESS )
6028 {
6029 psa_key_derivation_abort( &derivation );
6030 psa_destroy_key( master_key );
6031 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6032 }
6033
6034 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
6035 if( status != PSA_SUCCESS )
6036 {
6037 psa_key_derivation_abort( &derivation );
6038 psa_destroy_key( master_key );
6039 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6040 }
6041
6042 status = psa_key_derivation_abort( &derivation );
6043 if( status != PSA_SUCCESS )
6044 {
6045 psa_destroy_key( master_key );
6046 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6047 }
6048
6049 if( ! mbedtls_svc_key_id_is_null( master_key ) )
6050 status = psa_destroy_key( master_key );
6051 if( status != PSA_SUCCESS )
6052 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6053
6054 return( 0 );
6055}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006056#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006057#else /* MBEDTLS_USE_PSA_CRYPTO */
6058
Andrzej Kurek57d10632022-10-24 10:32:01 -04006059#if defined(MBEDTLS_MD_C) && \
6060 ( defined(MBEDTLS_SHA256_C) || \
6061 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006062MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006063static int tls_prf_generic( mbedtls_md_type_t md_type,
6064 const unsigned char *secret, size_t slen,
6065 const char *label,
6066 const unsigned char *random, size_t rlen,
6067 unsigned char *dstbuf, size_t dlen )
6068{
6069 size_t nb;
6070 size_t i, j, k, md_len;
6071 unsigned char *tmp;
6072 size_t tmp_len = 0;
6073 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6074 const mbedtls_md_info_t *md_info;
6075 mbedtls_md_context_t md_ctx;
6076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6077
6078 mbedtls_md_init( &md_ctx );
6079
6080 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
6081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6082
6083 md_len = mbedtls_md_get_size( md_info );
6084
6085 tmp_len = md_len + strlen( label ) + rlen;
6086 tmp = mbedtls_calloc( 1, tmp_len );
6087 if( tmp == NULL )
6088 {
6089 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6090 goto exit;
6091 }
6092
6093 nb = strlen( label );
6094 memcpy( tmp + md_len, label, nb );
6095 memcpy( tmp + md_len + nb, random, rlen );
6096 nb += rlen;
6097
6098 /*
6099 * Compute P_<hash>(secret, label + random)[0..dlen]
6100 */
6101 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
6102 goto exit;
6103
6104 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
6105 if( ret != 0 )
6106 goto exit;
6107 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
6108 if( ret != 0 )
6109 goto exit;
6110 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6111 if( ret != 0 )
6112 goto exit;
6113
6114 for( i = 0; i < dlen; i += md_len )
6115 {
6116 ret = mbedtls_md_hmac_reset ( &md_ctx );
6117 if( ret != 0 )
6118 goto exit;
6119 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
6120 if( ret != 0 )
6121 goto exit;
6122 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
6123 if( ret != 0 )
6124 goto exit;
6125
6126 ret = mbedtls_md_hmac_reset ( &md_ctx );
6127 if( ret != 0 )
6128 goto exit;
6129 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
6130 if( ret != 0 )
6131 goto exit;
6132 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
6133 if( ret != 0 )
6134 goto exit;
6135
6136 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
6137
6138 for( j = 0; j < k; j++ )
6139 dstbuf[i + j] = h_i[j];
6140 }
6141
6142exit:
6143 mbedtls_md_free( &md_ctx );
6144
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006145 if ( tmp != NULL )
6146 mbedtls_platform_zeroize( tmp, tmp_len );
6147
Jerry Yudc7bd172022-02-17 13:44:15 +08006148 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
6149
6150 mbedtls_free( tmp );
6151
6152 return( ret );
6153}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006154#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006155#endif /* MBEDTLS_USE_PSA_CRYPTO */
6156
Andrzej Kurek25f27152022-08-17 16:09:31 -04006157#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006158MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006159static int tls_prf_sha256( const unsigned char *secret, size_t slen,
6160 const char *label,
6161 const unsigned char *random, size_t rlen,
6162 unsigned char *dstbuf, size_t dlen )
6163{
6164 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
6165 label, random, rlen, dstbuf, dlen ) );
6166}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006167#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006168
Andrzej Kurek25f27152022-08-17 16:09:31 -04006169#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006170MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08006171static int tls_prf_sha384( const unsigned char *secret, size_t slen,
6172 const char *label,
6173 const unsigned char *random, size_t rlen,
6174 unsigned char *dstbuf, size_t dlen )
6175{
6176 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
6177 label, random, rlen, dstbuf, dlen ) );
6178}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006179#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006180
Jerry Yuf009d862022-02-17 14:01:37 +08006181/*
6182 * Set appropriate PRF function and other SSL / TLS1.2 functions
6183 *
6184 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006185 * - hash associated with the ciphersuite (only used by TLS 1.2)
6186 *
6187 * Outputs:
6188 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6189 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006190MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08006191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08006192 mbedtls_md_type_t hash )
6193{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006194#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01006195 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08006196 {
6197 handshake->tls_prf = tls_prf_sha384;
6198 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6199 handshake->calc_finished = ssl_calc_finished_tls_sha384;
6200 }
6201 else
6202#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006203#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006204 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006205 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006206 handshake->tls_prf = tls_prf_sha256;
6207 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6208 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6209 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006210#else
Jerry Yuf009d862022-02-17 14:01:37 +08006211 {
Jerry Yuf009d862022-02-17 14:01:37 +08006212 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006213 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6215 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006216#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006217
6218 return( 0 );
6219}
Jerry Yud6ab2352022-02-17 14:03:43 +08006220
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006221/*
6222 * Compute master secret if needed
6223 *
6224 * Parameters:
6225 * [in/out] handshake
6226 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6227 * (PSA-PSK) ciphersuite_info, psk_opaque
6228 * [out] premaster (cleared)
6229 * [out] master
6230 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6231 * debug: conf->f_dbg, conf->p_dbg
6232 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006233 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006234 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006235MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006236static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6237 unsigned char *master,
6238 const mbedtls_ssl_context *ssl )
6239{
6240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6241
6242 /* cf. RFC 5246, Section 8.1:
6243 * "The master secret is always exactly 48 bytes in length." */
6244 size_t const master_secret_len = 48;
6245
6246#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6247 unsigned char session_hash[48];
6248#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6249
6250 /* The label for the KDF used for key expansion.
6251 * This is either "master secret" or "extended master secret"
6252 * depending on whether the Extended Master Secret extension
6253 * is used. */
6254 char const *lbl = "master secret";
6255
Przemek Stekielae4ed302022-04-05 17:15:55 +02006256 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006257 * - If the Extended Master Secret extension is not used,
6258 * this is ClientHello.Random + ServerHello.Random
6259 * (see Sect. 8.1 in RFC 5246).
6260 * - If the Extended Master Secret extension is used,
6261 * this is the transcript of the handshake so far.
6262 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006263 unsigned char const *seed = handshake->randbytes;
6264 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006265
6266#if !defined(MBEDTLS_DEBUG_C) && \
6267 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6268 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6269 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6270 ssl = NULL; /* make sure we don't use it except for those cases */
6271 (void) ssl;
6272#endif
6273
6274 if( handshake->resume != 0 )
6275 {
6276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6277 return( 0 );
6278 }
6279
6280#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6281 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6282 {
6283 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006284 seed = session_hash;
6285 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006286
6287 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006288 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006289 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006290#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006291
Przemek Stekiel99114f32022-04-22 11:20:09 +02006292#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006293 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006294 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006295 {
6296 /* Perform PSK-to-MS expansion in a single step. */
6297 psa_status_t status;
6298 psa_algorithm_t alg;
6299 mbedtls_svc_key_id_t psk;
6300 psa_key_derivation_operation_t derivation =
6301 PSA_KEY_DERIVATION_OPERATION_INIT;
6302 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6303
6304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6305
6306 psk = mbedtls_ssl_get_opaque_psk( ssl );
6307
6308 if( hash_alg == MBEDTLS_MD_SHA384 )
6309 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6310 else
6311 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6312
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006313 size_t other_secret_len = 0;
6314 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006315
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006316 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006317 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006318 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006319 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006320 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006321 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006322 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006323 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006324 other_secret_len = 48;
6325 other_secret = handshake->premaster + 2;
6326 break;
6327 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006328 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006329 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6330 other_secret = handshake->premaster + 2;
6331 break;
6332 default:
6333 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006334 }
6335
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006336 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006337 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006338 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006339 (unsigned char const *) lbl,
6340 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006341 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006342 master_secret_len );
6343 if( status != PSA_SUCCESS )
6344 {
6345 psa_key_derivation_abort( &derivation );
6346 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6347 }
6348
6349 status = psa_key_derivation_output_bytes( &derivation,
6350 master,
6351 master_secret_len );
6352 if( status != PSA_SUCCESS )
6353 {
6354 psa_key_derivation_abort( &derivation );
6355 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6356 }
6357
6358 status = psa_key_derivation_abort( &derivation );
6359 if( status != PSA_SUCCESS )
6360 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6361 }
6362 else
6363#endif
6364 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006365#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
6366 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6367 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6368 {
6369 psa_status_t status;
6370 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6371 psa_key_derivation_operation_t derivation =
6372 PSA_KEY_DERIVATION_OPERATION_INIT;
6373
6374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PMS KDF for ECJPAKE" ) );
6375
6376 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6377
6378 status = psa_key_derivation_setup( &derivation, alg );
6379 if( status != PSA_SUCCESS )
6380 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6381
6382 status = psa_key_derivation_set_capacity( &derivation,
6383 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE );
6384 if( status != PSA_SUCCESS )
6385 {
6386 psa_key_derivation_abort( &derivation );
6387 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6388 }
6389
6390 status = psa_pake_get_implicit_key( &handshake->psa_pake_ctx,
6391 &derivation );
6392 if( status != PSA_SUCCESS )
6393 {
6394 psa_key_derivation_abort( &derivation );
6395 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6396 }
6397
6398 status = psa_key_derivation_output_bytes( &derivation,
6399 handshake->premaster,
6400 handshake->pmslen );
6401 if( status != PSA_SUCCESS )
6402 {
6403 psa_key_derivation_abort( &derivation );
6404 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6405 }
6406
6407 status = psa_key_derivation_abort( &derivation );
6408 if( status != PSA_SUCCESS )
6409 {
6410 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6411 }
6412 }
6413#endif
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006414 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006415 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006416 master,
6417 master_secret_len );
6418 if( ret != 0 )
6419 {
6420 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6421 return( ret );
6422 }
6423
6424 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6425 handshake->premaster,
6426 handshake->pmslen );
6427
6428 mbedtls_platform_zeroize( handshake->premaster,
6429 sizeof(handshake->premaster) );
6430 }
6431
6432 return( 0 );
6433}
6434
Jerry Yud62f87e2022-02-17 14:09:02 +08006435int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6436{
6437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6438 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6439 ssl->handshake->ciphersuite_info;
6440
6441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6442
6443 /* Set PRF, calc_verify and calc_finished function pointers */
6444 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006445 ciphersuite_info->mac );
6446 if( ret != 0 )
6447 {
6448 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6449 return( ret );
6450 }
6451
6452 /* Compute master secret if needed */
6453 ret = ssl_compute_master( ssl->handshake,
6454 ssl->session_negotiate->master,
6455 ssl );
6456 if( ret != 0 )
6457 {
6458 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6459 return( ret );
6460 }
6461
6462 /* Swap the client and server random values:
6463 * - MS derivation wanted client+server (RFC 5246 8.1)
6464 * - key derivation wants server+client (RFC 5246 6.3) */
6465 {
6466 unsigned char tmp[64];
6467 memcpy( tmp, ssl->handshake->randbytes, 64 );
6468 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6469 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6470 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6471 }
6472
6473 /* Populate transform structure */
6474 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6475 ssl->session_negotiate->ciphersuite,
6476 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006477#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006478 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006479#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006480 ssl->handshake->tls_prf,
6481 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006482 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006483 ssl->conf->endpoint,
6484 ssl );
6485 if( ret != 0 )
6486 {
6487 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6488 return( ret );
6489 }
6490
6491 /* We no longer need Server/ClientHello.random values */
6492 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6493 sizeof( ssl->handshake->randbytes ) );
6494
6495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6496
6497 return( 0 );
6498}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006499
Ronald Cron4dcbca92022-03-07 10:21:40 +01006500int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6501{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006502 switch( md )
6503 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006504#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006505 case MBEDTLS_SSL_HASH_SHA384:
6506 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6507 break;
6508#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006509#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006510 case MBEDTLS_SSL_HASH_SHA256:
6511 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6512 break;
6513#endif
6514 default:
6515 return( -1 );
6516 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006517#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6518 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6519 (void) ssl;
6520#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006521 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006522}
6523
Andrzej Kurek25f27152022-08-17 16:09:31 -04006524#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006525void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6526 unsigned char *hash,
6527 size_t *hlen )
6528{
6529#if defined(MBEDTLS_USE_PSA_CRYPTO)
6530 size_t hash_size;
6531 psa_status_t status;
6532 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6533
6534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6535 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6536 if( status != PSA_SUCCESS )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6539 return;
6540 }
6541
6542 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6543 if( status != PSA_SUCCESS )
6544 {
6545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6546 return;
6547 }
6548
6549 *hlen = 32;
6550 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6552#else
6553 mbedtls_sha256_context sha256;
6554
6555 mbedtls_sha256_init( &sha256 );
6556
6557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6558
6559 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6560 mbedtls_sha256_finish( &sha256, hash );
6561
6562 *hlen = 32;
6563
6564 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6566
6567 mbedtls_sha256_free( &sha256 );
6568#endif /* MBEDTLS_USE_PSA_CRYPTO */
6569 return;
6570}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006571#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006572
Andrzej Kurek25f27152022-08-17 16:09:31 -04006573#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006574void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6575 unsigned char *hash,
6576 size_t *hlen )
6577{
6578#if defined(MBEDTLS_USE_PSA_CRYPTO)
6579 size_t hash_size;
6580 psa_status_t status;
6581 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6582
6583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6584 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6585 if( status != PSA_SUCCESS )
6586 {
6587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6588 return;
6589 }
6590
6591 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6592 if( status != PSA_SUCCESS )
6593 {
6594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6595 return;
6596 }
6597
6598 *hlen = 48;
6599 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6601#else
6602 mbedtls_sha512_context sha512;
6603
6604 mbedtls_sha512_init( &sha512 );
6605
6606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6607
Andrzej Kureka242e832022-08-11 10:03:14 -04006608 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006609 mbedtls_sha512_finish( &sha512, hash );
6610
6611 *hlen = 48;
6612
6613 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6615
6616 mbedtls_sha512_free( &sha512 );
6617#endif /* MBEDTLS_USE_PSA_CRYPTO */
6618 return;
6619}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006620#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006621
Neil Armstrong80f6f322022-05-03 17:56:38 +02006622#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6623 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006624int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6625{
6626 unsigned char *p = ssl->handshake->premaster;
6627 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6628 const unsigned char *psk = NULL;
6629 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006630 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006631
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006632 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006633 {
6634 /*
6635 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006636 * checked before calling this function.
6637 *
6638 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006639 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006640 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006641 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6645 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006646 }
6647
6648 /*
6649 * PMS = struct {
6650 * opaque other_secret<0..2^16-1>;
6651 * opaque psk<0..2^16-1>;
6652 * };
6653 * with "other_secret" depending on the particular key exchange
6654 */
6655#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6656 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6657 {
6658 if( end - p < 2 )
6659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6660
6661 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6662 p += 2;
6663
6664 if( end < p || (size_t)( end - p ) < psk_len )
6665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6666
6667 memset( p, 0, psk_len );
6668 p += psk_len;
6669 }
6670 else
6671#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6672#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6673 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6674 {
6675 /*
6676 * other_secret already set by the ClientKeyExchange message,
6677 * and is 48 bytes long
6678 */
6679 if( end - p < 2 )
6680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6681
6682 *p++ = 0;
6683 *p++ = 48;
6684 p += 48;
6685 }
6686 else
6687#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6688#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6689 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6690 {
6691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6692 size_t len;
6693
6694 /* Write length only when we know the actual value */
6695 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6696 p + 2, end - ( p + 2 ), &len,
6697 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6698 {
6699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6700 return( ret );
6701 }
6702 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6703 p += 2 + len;
6704
6705 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6706 }
6707 else
6708#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006709#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006710 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6711 {
6712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6713 size_t zlen;
6714
6715 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6716 p + 2, end - ( p + 2 ),
6717 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6718 {
6719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6720 return( ret );
6721 }
6722
6723 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6724 p += 2 + zlen;
6725
6726 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6727 MBEDTLS_DEBUG_ECDH_Z );
6728 }
6729 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006730#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006731 {
6732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6734 }
6735
6736 /* opaque psk<0..2^16-1>; */
6737 if( end - p < 2 )
6738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6739
6740 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6741 p += 2;
6742
6743 if( end < p || (size_t)( end - p ) < psk_len )
6744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6745
6746 memcpy( p, psk, psk_len );
6747 p += psk_len;
6748
6749 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6750
6751 return( 0 );
6752}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006753#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006754
6755#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006756MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006757static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6758
6759#if defined(MBEDTLS_SSL_PROTO_DTLS)
6760int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6761{
6762 /* If renegotiation is not enforced, retransmit until we would reach max
6763 * timeout if we were using the usual handshake doubling scheme */
6764 if( ssl->conf->renego_max_records < 0 )
6765 {
6766 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6767 unsigned char doublings = 1;
6768
6769 while( ratio != 0 )
6770 {
6771 ++doublings;
6772 ratio >>= 1;
6773 }
6774
6775 if( ++ssl->renego_records_seen > doublings )
6776 {
6777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6778 return( 0 );
6779 }
6780 }
6781
6782 return( ssl_write_hello_request( ssl ) );
6783}
6784#endif
6785#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006786
Jerry Yud9526692022-02-17 14:23:47 +08006787/*
6788 * Handshake functions
6789 */
6790#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6791/* No certificate support -> dummy functions */
6792int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6793{
6794 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6795 ssl->handshake->ciphersuite_info;
6796
6797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6798
6799 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6800 {
6801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6802 ssl->state++;
6803 return( 0 );
6804 }
6805
6806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6808}
6809
6810int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6811{
6812 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6813 ssl->handshake->ciphersuite_info;
6814
6815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6816
6817 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6818 {
6819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6820 ssl->state++;
6821 return( 0 );
6822 }
6823
6824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6826}
6827
6828#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6829/* Some certificate support -> implement write and parse */
6830
6831int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6832{
6833 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6834 size_t i, n;
6835 const mbedtls_x509_crt *crt;
6836 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6837 ssl->handshake->ciphersuite_info;
6838
6839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6840
6841 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6842 {
6843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6844 ssl->state++;
6845 return( 0 );
6846 }
6847
6848#if defined(MBEDTLS_SSL_CLI_C)
6849 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6850 {
6851 if( ssl->handshake->client_auth == 0 )
6852 {
6853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6854 ssl->state++;
6855 return( 0 );
6856 }
6857 }
6858#endif /* MBEDTLS_SSL_CLI_C */
6859#if defined(MBEDTLS_SSL_SRV_C)
6860 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6861 {
6862 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6863 {
6864 /* Should never happen because we shouldn't have picked the
6865 * ciphersuite if we don't have a certificate. */
6866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6867 }
6868 }
6869#endif
6870
6871 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6872
6873 /*
6874 * 0 . 0 handshake type
6875 * 1 . 3 handshake length
6876 * 4 . 6 length of all certs
6877 * 7 . 9 length of cert. 1
6878 * 10 . n-1 peer certificate
6879 * n . n+2 length of cert. 2
6880 * n+3 . ... upper level cert, etc.
6881 */
6882 i = 7;
6883 crt = mbedtls_ssl_own_cert( ssl );
6884
6885 while( crt != NULL )
6886 {
6887 n = crt->raw.len;
6888 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6889 {
6890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6891 " > %" MBEDTLS_PRINTF_SIZET,
6892 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6893 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6894 }
6895
6896 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6897 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6898 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6899
6900 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6901 i += n; crt = crt->next;
6902 }
6903
6904 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6905 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6906 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6907
6908 ssl->out_msglen = i;
6909 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6910 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6911
6912 ssl->state++;
6913
6914 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6915 {
6916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6917 return( ret );
6918 }
6919
6920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6921
6922 return( ret );
6923}
6924
6925#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6926
6927#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006928MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006929static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6930 unsigned char *crt_buf,
6931 size_t crt_buf_len )
6932{
6933 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6934
6935 if( peer_crt == NULL )
6936 return( -1 );
6937
6938 if( peer_crt->raw.len != crt_buf_len )
6939 return( -1 );
6940
6941 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6942}
6943#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006944MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006945static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6946 unsigned char *crt_buf,
6947 size_t crt_buf_len )
6948{
6949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6950 unsigned char const * const peer_cert_digest =
6951 ssl->session->peer_cert_digest;
6952 mbedtls_md_type_t const peer_cert_digest_type =
6953 ssl->session->peer_cert_digest_type;
6954 mbedtls_md_info_t const * const digest_info =
6955 mbedtls_md_info_from_type( peer_cert_digest_type );
6956 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6957 size_t digest_len;
6958
6959 if( peer_cert_digest == NULL || digest_info == NULL )
6960 return( -1 );
6961
6962 digest_len = mbedtls_md_get_size( digest_info );
6963 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6964 return( -1 );
6965
6966 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6967 if( ret != 0 )
6968 return( -1 );
6969
6970 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6971}
6972#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6973#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6974
6975/*
6976 * Once the certificate message is read, parse it into a cert chain and
6977 * perform basic checks, but leave actual verification to the caller
6978 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006979MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006980static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6981 mbedtls_x509_crt *chain )
6982{
6983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6984#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6985 int crt_cnt=0;
6986#endif
6987 size_t i, n;
6988 uint8_t alert;
6989
6990 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6991 {
6992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6994 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6995 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6996 }
6997
6998 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6999 {
7000 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7001 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
7003 }
7004
7005 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
7006 {
7007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7009 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7010 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7011 }
7012
7013 i = mbedtls_ssl_hs_hdr_len( ssl );
7014
7015 /*
7016 * Same message structure as in mbedtls_ssl_write_certificate()
7017 */
7018 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
7019
7020 if( ssl->in_msg[i] != 0 ||
7021 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
7022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7025 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7026 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7027 }
7028
7029 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7030 i += 3;
7031
7032 /* Iterate through and parse the CRTs in the provided chain. */
7033 while( i < ssl->in_hslen )
7034 {
7035 /* Check that there's room for the next CRT's length fields. */
7036 if ( i + 3 > ssl->in_hslen ) {
7037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7038 mbedtls_ssl_send_alert_message( ssl,
7039 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7040 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7041 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7042 }
7043 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7044 * anything beyond 2**16 ~ 64K. */
7045 if( ssl->in_msg[i] != 0 )
7046 {
7047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7048 mbedtls_ssl_send_alert_message( ssl,
7049 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7050 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
7051 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
7052 }
7053
7054 /* Read length of the next CRT in the chain. */
7055 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
7056 | (unsigned int) ssl->in_msg[i + 2];
7057 i += 3;
7058
7059 if( n < 128 || i + n > ssl->in_hslen )
7060 {
7061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
7062 mbedtls_ssl_send_alert_message( ssl,
7063 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7064 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7065 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
7066 }
7067
7068 /* Check if we're handling the first CRT in the chain. */
7069#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7070 if( crt_cnt++ == 0 &&
7071 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
7072 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7073 {
7074 /* During client-side renegotiation, check that the server's
7075 * end-CRTs hasn't changed compared to the initial handshake,
7076 * mitigating the triple handshake attack. On success, reuse
7077 * the original end-CRT instead of parsing it again. */
7078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7079 if( ssl_check_peer_crt_unchanged( ssl,
7080 &ssl->in_msg[i],
7081 n ) != 0 )
7082 {
7083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
7084 mbedtls_ssl_send_alert_message( ssl,
7085 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7086 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
7087 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
7088 }
7089
7090 /* Now we can safely free the original chain. */
7091 ssl_clear_peer_cert( ssl->session );
7092 }
7093#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7094
7095 /* Parse the next certificate in the chain. */
7096#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7097 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
7098#else
7099 /* If we don't need to store the CRT chain permanently, parse
7100 * it in-place from the input buffer instead of making a copy. */
7101 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7102#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7103 switch( ret )
7104 {
7105 case 0: /*ok*/
7106 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7107 /* Ignore certificate with an unknown algorithm: maybe a
7108 prior certificate was already trusted. */
7109 break;
7110
7111 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7112 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7113 goto crt_parse_der_failed;
7114
7115 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7116 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7117 goto crt_parse_der_failed;
7118
7119 default:
7120 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7121 crt_parse_der_failed:
7122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
7123 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7124 return( ret );
7125 }
7126
7127 i += n;
7128 }
7129
7130 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
7131 return( 0 );
7132}
7133
7134#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007135MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007136static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7137{
7138 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7139 return( -1 );
7140
7141 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7142 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7143 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7144 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7145 {
Ronald Cron19385882022-06-15 16:26:13 +02007146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08007147 return( 0 );
7148 }
7149 return( -1 );
7150}
7151#endif /* MBEDTLS_SSL_SRV_C */
7152
7153/* Check if a certificate message is expected.
7154 * Return either
7155 * - SSL_CERTIFICATE_EXPECTED, or
7156 * - SSL_CERTIFICATE_SKIP
7157 * indicating whether a Certificate message is expected or not.
7158 */
7159#define SSL_CERTIFICATE_EXPECTED 0
7160#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007161MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007162static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7163 int authmode )
7164{
7165 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7166 ssl->handshake->ciphersuite_info;
7167
7168 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7169 return( SSL_CERTIFICATE_SKIP );
7170
7171#if defined(MBEDTLS_SSL_SRV_C)
7172 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7173 {
7174 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7175 return( SSL_CERTIFICATE_SKIP );
7176
7177 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7178 {
7179 ssl->session_negotiate->verify_result =
7180 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7181 return( SSL_CERTIFICATE_SKIP );
7182 }
7183 }
7184#else
7185 ((void) authmode);
7186#endif /* MBEDTLS_SSL_SRV_C */
7187
7188 return( SSL_CERTIFICATE_EXPECTED );
7189}
7190
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_verify( mbedtls_ssl_context *ssl,
7193 int authmode,
7194 mbedtls_x509_crt *chain,
7195 void *rs_ctx )
7196{
7197 int ret = 0;
7198 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7199 ssl->handshake->ciphersuite_info;
7200 int have_ca_chain = 0;
7201
7202 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7203 void *p_vrfy;
7204
7205 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7206 return( 0 );
7207
7208 if( ssl->f_vrfy != NULL )
7209 {
7210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
7211 f_vrfy = ssl->f_vrfy;
7212 p_vrfy = ssl->p_vrfy;
7213 }
7214 else
7215 {
7216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
7217 f_vrfy = ssl->conf->f_vrfy;
7218 p_vrfy = ssl->conf->p_vrfy;
7219 }
7220
7221 /*
7222 * Main check: verify certificate
7223 */
7224#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7225 if( ssl->conf->f_ca_cb != NULL )
7226 {
7227 ((void) rs_ctx);
7228 have_ca_chain = 1;
7229
7230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
7231 ret = mbedtls_x509_crt_verify_with_ca_cb(
7232 chain,
7233 ssl->conf->f_ca_cb,
7234 ssl->conf->p_ca_cb,
7235 ssl->conf->cert_profile,
7236 ssl->hostname,
7237 &ssl->session_negotiate->verify_result,
7238 f_vrfy, p_vrfy );
7239 }
7240 else
7241#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7242 {
7243 mbedtls_x509_crt *ca_chain;
7244 mbedtls_x509_crl *ca_crl;
7245
7246#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7247 if( ssl->handshake->sni_ca_chain != NULL )
7248 {
7249 ca_chain = ssl->handshake->sni_ca_chain;
7250 ca_crl = ssl->handshake->sni_ca_crl;
7251 }
7252 else
7253#endif
7254 {
7255 ca_chain = ssl->conf->ca_chain;
7256 ca_crl = ssl->conf->ca_crl;
7257 }
7258
7259 if( ca_chain != NULL )
7260 have_ca_chain = 1;
7261
7262 ret = mbedtls_x509_crt_verify_restartable(
7263 chain,
7264 ca_chain, ca_crl,
7265 ssl->conf->cert_profile,
7266 ssl->hostname,
7267 &ssl->session_negotiate->verify_result,
7268 f_vrfy, p_vrfy, rs_ctx );
7269 }
7270
7271 if( ret != 0 )
7272 {
7273 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7274 }
7275
7276#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7277 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7278 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7279#endif
7280
7281 /*
7282 * Secondary checks: always done, but change 'ret' only if it was 0
7283 */
7284
7285#if defined(MBEDTLS_ECP_C)
7286 {
7287 const mbedtls_pk_context *pk = &chain->pk;
7288
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007289 /* If certificate uses an EC key, make sure the curve is OK.
7290 * This is a public key, so it can't be opaque, so can_do() is a good
7291 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007292 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007293 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007294 /* and in the unlikely case the above assumption no longer holds
7295 * we are making sure that pk_ec() here does not return a NULL
7296 */
7297 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7298 if( ec == NULL )
7299 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7302 }
Jerry Yud9526692022-02-17 14:23:47 +08007303
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007304 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7305 {
7306 ssl->session_negotiate->verify_result |=
7307 MBEDTLS_X509_BADCERT_BAD_KEY;
7308
7309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7310 if( ret == 0 )
7311 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7312 }
Jerry Yud9526692022-02-17 14:23:47 +08007313 }
7314 }
7315#endif /* MBEDTLS_ECP_C */
7316
7317 if( mbedtls_ssl_check_cert_usage( chain,
7318 ciphersuite_info,
7319 ! ssl->conf->endpoint,
7320 &ssl->session_negotiate->verify_result ) != 0 )
7321 {
7322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7323 if( ret == 0 )
7324 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7325 }
7326
7327 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7328 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7329 * with details encoded in the verification flags. All other kinds
7330 * of error codes, including those from the user provided f_vrfy
7331 * functions, are treated as fatal and lead to a failure of
7332 * ssl_parse_certificate even if verification was optional. */
7333 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7334 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7335 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7336 {
7337 ret = 0;
7338 }
7339
7340 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7341 {
7342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7343 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7344 }
7345
7346 if( ret != 0 )
7347 {
7348 uint8_t alert;
7349
7350 /* The certificate may have been rejected for several reasons.
7351 Pick one and send the corresponding alert. Which alert to send
7352 may be a subject of debate in some cases. */
7353 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7354 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7355 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7356 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7357 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7358 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7359 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7360 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7361 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7362 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7363 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7364 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7365 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7366 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7367 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7368 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7369 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7370 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7371 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7372 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7373 else
7374 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7376 alert );
7377 }
7378
7379#if defined(MBEDTLS_DEBUG_C)
7380 if( ssl->session_negotiate->verify_result != 0 )
7381 {
7382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7383 (unsigned int) ssl->session_negotiate->verify_result ) );
7384 }
7385 else
7386 {
7387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7388 }
7389#endif /* MBEDTLS_DEBUG_C */
7390
7391 return( ret );
7392}
7393
7394#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007395MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007396static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7397 unsigned char *start, size_t len )
7398{
7399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7400 /* Remember digest of the peer's end-CRT. */
7401 ssl->session_negotiate->peer_cert_digest =
7402 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7403 if( ssl->session_negotiate->peer_cert_digest == NULL )
7404 {
7405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7406 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7407 mbedtls_ssl_send_alert_message( ssl,
7408 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7409 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7410
7411 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7412 }
7413
7414 ret = mbedtls_md( mbedtls_md_info_from_type(
7415 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7416 start, len,
7417 ssl->session_negotiate->peer_cert_digest );
7418
7419 ssl->session_negotiate->peer_cert_digest_type =
7420 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7421 ssl->session_negotiate->peer_cert_digest_len =
7422 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7423
7424 return( ret );
7425}
7426
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007427MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007428static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7429 unsigned char *start, size_t len )
7430{
7431 unsigned char *end = start + len;
7432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7433
7434 /* Make a copy of the peer's raw public key. */
7435 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7436 ret = mbedtls_pk_parse_subpubkey( &start, end,
7437 &ssl->handshake->peer_pubkey );
7438 if( ret != 0 )
7439 {
7440 /* We should have parsed the public key before. */
7441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7442 }
7443
7444 return( 0 );
7445}
7446#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7447
7448int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7449{
7450 int ret = 0;
7451 int crt_expected;
7452#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7453 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7454 ? ssl->handshake->sni_authmode
7455 : ssl->conf->authmode;
7456#else
7457 const int authmode = ssl->conf->authmode;
7458#endif
7459 void *rs_ctx = NULL;
7460 mbedtls_x509_crt *chain = NULL;
7461
7462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7463
7464 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7465 if( crt_expected == SSL_CERTIFICATE_SKIP )
7466 {
7467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7468 goto exit;
7469 }
7470
7471#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7472 if( ssl->handshake->ecrs_enabled &&
7473 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7474 {
7475 chain = ssl->handshake->ecrs_peer_cert;
7476 ssl->handshake->ecrs_peer_cert = NULL;
7477 goto crt_verify;
7478 }
7479#endif
7480
7481 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7482 {
7483 /* mbedtls_ssl_read_record may have sent an alert already. We
7484 let it decide whether to alert. */
7485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7486 goto exit;
7487 }
7488
7489#if defined(MBEDTLS_SSL_SRV_C)
7490 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7491 {
7492 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7493
7494 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7495 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7496
7497 goto exit;
7498 }
7499#endif /* MBEDTLS_SSL_SRV_C */
7500
7501 /* Clear existing peer CRT structure in case we tried to
7502 * reuse a session but it failed, and allocate a new one. */
7503 ssl_clear_peer_cert( ssl->session_negotiate );
7504
7505 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7506 if( chain == NULL )
7507 {
7508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7509 sizeof( mbedtls_x509_crt ) ) );
7510 mbedtls_ssl_send_alert_message( ssl,
7511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7512 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7513
7514 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7515 goto exit;
7516 }
7517 mbedtls_x509_crt_init( chain );
7518
7519 ret = ssl_parse_certificate_chain( ssl, chain );
7520 if( ret != 0 )
7521 goto exit;
7522
7523#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7524 if( ssl->handshake->ecrs_enabled)
7525 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7526
7527crt_verify:
7528 if( ssl->handshake->ecrs_enabled)
7529 rs_ctx = &ssl->handshake->ecrs_ctx;
7530#endif
7531
7532 ret = ssl_parse_certificate_verify( ssl, authmode,
7533 chain, rs_ctx );
7534 if( ret != 0 )
7535 goto exit;
7536
7537#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7538 {
7539 unsigned char *crt_start, *pk_start;
7540 size_t crt_len, pk_len;
7541
7542 /* We parse the CRT chain without copying, so
7543 * these pointers point into the input buffer,
7544 * and are hence still valid after freeing the
7545 * CRT chain. */
7546
7547 crt_start = chain->raw.p;
7548 crt_len = chain->raw.len;
7549
7550 pk_start = chain->pk_raw.p;
7551 pk_len = chain->pk_raw.len;
7552
7553 /* Free the CRT structures before computing
7554 * digest and copying the peer's public key. */
7555 mbedtls_x509_crt_free( chain );
7556 mbedtls_free( chain );
7557 chain = NULL;
7558
7559 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7560 if( ret != 0 )
7561 goto exit;
7562
7563 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7564 if( ret != 0 )
7565 goto exit;
7566 }
7567#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7568 /* Pass ownership to session structure. */
7569 ssl->session_negotiate->peer_cert = chain;
7570 chain = NULL;
7571#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7572
7573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7574
7575exit:
7576
7577 if( ret == 0 )
7578 ssl->state++;
7579
7580#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7581 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7582 {
7583 ssl->handshake->ecrs_peer_cert = chain;
7584 chain = NULL;
7585 }
7586#endif
7587
7588 if( chain != NULL )
7589 {
7590 mbedtls_x509_crt_free( chain );
7591 mbedtls_free( chain );
7592 }
7593
7594 return( ret );
7595}
7596#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7597
Andrzej Kurek25f27152022-08-17 16:09:31 -04007598#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007599static void ssl_calc_finished_tls_sha256(
7600 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7601{
7602 int len = 12;
7603 const char *sender;
7604 unsigned char padbuf[32];
7605#if defined(MBEDTLS_USE_PSA_CRYPTO)
7606 size_t hash_size;
7607 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7608 psa_status_t status;
7609#else
7610 mbedtls_sha256_context sha256;
7611#endif
7612
7613 mbedtls_ssl_session *session = ssl->session_negotiate;
7614 if( !session )
7615 session = ssl->session;
7616
7617 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7618 ? "client finished"
7619 : "server finished";
7620
7621#if defined(MBEDTLS_USE_PSA_CRYPTO)
7622 sha256_psa = psa_hash_operation_init();
7623
7624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7625
7626 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7627 if( status != PSA_SUCCESS )
7628 {
7629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7630 return;
7631 }
7632
7633 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7634 if( status != PSA_SUCCESS )
7635 {
7636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7637 return;
7638 }
7639 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7640#else
7641
7642 mbedtls_sha256_init( &sha256 );
7643
7644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7645
7646 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7647
7648 /*
7649 * TLSv1.2:
7650 * hash = PRF( master, finished_label,
7651 * Hash( handshake ) )[0.11]
7652 */
7653
7654#if !defined(MBEDTLS_SHA256_ALT)
7655 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7656 sha256.state, sizeof( sha256.state ) );
7657#endif
7658
7659 mbedtls_sha256_finish( &sha256, padbuf );
7660 mbedtls_sha256_free( &sha256 );
7661#endif /* MBEDTLS_USE_PSA_CRYPTO */
7662
7663 ssl->handshake->tls_prf( session->master, 48, sender,
7664 padbuf, 32, buf, len );
7665
7666 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7667
7668 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7669
7670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7671}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007672#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007673
Jerry Yub7ba49e2022-02-17 14:25:53 +08007674
Andrzej Kurek25f27152022-08-17 16:09:31 -04007675#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007676static void ssl_calc_finished_tls_sha384(
7677 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7678{
7679 int len = 12;
7680 const char *sender;
7681 unsigned char padbuf[48];
7682#if defined(MBEDTLS_USE_PSA_CRYPTO)
7683 size_t hash_size;
7684 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7685 psa_status_t status;
7686#else
7687 mbedtls_sha512_context sha512;
7688#endif
7689
7690 mbedtls_ssl_session *session = ssl->session_negotiate;
7691 if( !session )
7692 session = ssl->session;
7693
7694 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7695 ? "client finished"
7696 : "server finished";
7697
7698#if defined(MBEDTLS_USE_PSA_CRYPTO)
7699 sha384_psa = psa_hash_operation_init();
7700
7701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7702
7703 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7704 if( status != PSA_SUCCESS )
7705 {
7706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7707 return;
7708 }
7709
7710 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7711 if( status != PSA_SUCCESS )
7712 {
7713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7714 return;
7715 }
7716 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7717#else
7718 mbedtls_sha512_init( &sha512 );
7719
7720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7721
Andrzej Kureka242e832022-08-11 10:03:14 -04007722 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007723
7724 /*
7725 * TLSv1.2:
7726 * hash = PRF( master, finished_label,
7727 * Hash( handshake ) )[0.11]
7728 */
7729
7730#if !defined(MBEDTLS_SHA512_ALT)
7731 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7732 sha512.state, sizeof( sha512.state ) );
7733#endif
7734 mbedtls_sha512_finish( &sha512, padbuf );
7735
7736 mbedtls_sha512_free( &sha512 );
7737#endif
7738
7739 ssl->handshake->tls_prf( session->master, 48, sender,
7740 padbuf, 48, buf, len );
7741
7742 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7743
7744 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7745
7746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7747}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007748#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007749
Jerry Yuaef00152022-02-17 14:27:31 +08007750void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7751{
7752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7753
7754 /*
7755 * Free our handshake params
7756 */
7757 mbedtls_ssl_handshake_free( ssl );
7758 mbedtls_free( ssl->handshake );
7759 ssl->handshake = NULL;
7760
7761 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007762 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007763 */
7764 if( ssl->transform )
7765 {
7766 mbedtls_ssl_transform_free( ssl->transform );
7767 mbedtls_free( ssl->transform );
7768 }
7769 ssl->transform = ssl->transform_negotiate;
7770 ssl->transform_negotiate = NULL;
7771
7772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7773}
7774
Jerry Yu2a9fff52022-02-17 14:28:51 +08007775void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7776{
7777 int resume = ssl->handshake->resume;
7778
7779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7780
7781#if defined(MBEDTLS_SSL_RENEGOTIATION)
7782 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7783 {
7784 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7785 ssl->renego_records_seen = 0;
7786 }
7787#endif
7788
7789 /*
7790 * Free the previous session and switch in the current one
7791 */
7792 if( ssl->session )
7793 {
7794#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7795 /* RFC 7366 3.1: keep the EtM state */
7796 ssl->session_negotiate->encrypt_then_mac =
7797 ssl->session->encrypt_then_mac;
7798#endif
7799
7800 mbedtls_ssl_session_free( ssl->session );
7801 mbedtls_free( ssl->session );
7802 }
7803 ssl->session = ssl->session_negotiate;
7804 ssl->session_negotiate = NULL;
7805
7806 /*
7807 * Add cache entry
7808 */
7809 if( ssl->conf->f_set_cache != NULL &&
7810 ssl->session->id_len != 0 &&
7811 resume == 0 )
7812 {
7813 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7814 ssl->session->id,
7815 ssl->session->id_len,
7816 ssl->session ) != 0 )
7817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7818 }
7819
7820#if defined(MBEDTLS_SSL_PROTO_DTLS)
7821 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7822 ssl->handshake->flight != NULL )
7823 {
7824 /* Cancel handshake timer */
7825 mbedtls_ssl_set_timer( ssl, 0 );
7826
7827 /* Keep last flight around in case we need to resend it:
7828 * we need the handshake and transform structures for that */
7829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7830 }
7831 else
7832#endif
7833 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7834
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007835 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007836
7837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7838}
7839
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007840int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7841{
7842 int ret, hash_len;
7843
7844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7845
7846 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7847
7848 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7849
7850 /*
7851 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7852 * may define some other value. Currently (early 2016), no defined
7853 * ciphersuite does this (and this is unlikely to change as activity has
7854 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7855 */
7856 hash_len = 12;
7857
7858#if defined(MBEDTLS_SSL_RENEGOTIATION)
7859 ssl->verify_data_len = hash_len;
7860 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7861#endif
7862
7863 ssl->out_msglen = 4 + hash_len;
7864 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7865 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7866
7867 /*
7868 * In case of session resuming, invert the client and server
7869 * ChangeCipherSpec messages order.
7870 */
7871 if( ssl->handshake->resume != 0 )
7872 {
7873#if defined(MBEDTLS_SSL_CLI_C)
7874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7875 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7876#endif
7877#if defined(MBEDTLS_SSL_SRV_C)
7878 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7879 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7880#endif
7881 }
7882 else
7883 ssl->state++;
7884
7885 /*
7886 * Switch to our negotiated transform and session parameters for outbound
7887 * data.
7888 */
7889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7890
7891#if defined(MBEDTLS_SSL_PROTO_DTLS)
7892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7893 {
7894 unsigned char i;
7895
7896 /* Remember current epoch settings for resending */
7897 ssl->handshake->alt_transform_out = ssl->transform_out;
7898 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7899 sizeof( ssl->handshake->alt_out_ctr ) );
7900
7901 /* Set sequence_number to zero */
7902 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7903
7904
7905 /* Increment epoch */
7906 for( i = 2; i > 0; i-- )
7907 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7908 break;
7909
7910 /* The loop goes to its end iff the counter is wrapping */
7911 if( i == 0 )
7912 {
7913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7914 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7915 }
7916 }
7917 else
7918#endif /* MBEDTLS_SSL_PROTO_DTLS */
7919 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7920
7921 ssl->transform_out = ssl->transform_negotiate;
7922 ssl->session_out = ssl->session_negotiate;
7923
7924#if defined(MBEDTLS_SSL_PROTO_DTLS)
7925 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7926 mbedtls_ssl_send_flight_completed( ssl );
7927#endif
7928
7929 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7930 {
7931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7932 return( ret );
7933 }
7934
7935#if defined(MBEDTLS_SSL_PROTO_DTLS)
7936 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7937 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7938 {
7939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7940 return( ret );
7941 }
7942#endif
7943
7944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7945
7946 return( 0 );
7947}
7948
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007949#define SSL_MAX_HASH_LEN 12
7950
7951int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7952{
7953 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7954 unsigned int hash_len = 12;
7955 unsigned char buf[SSL_MAX_HASH_LEN];
7956
7957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7958
7959 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7960
7961 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7962 {
7963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7964 goto exit;
7965 }
7966
7967 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7968 {
7969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7971 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7972 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7973 goto exit;
7974 }
7975
7976 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7977 {
7978 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7979 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7980 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7981 goto exit;
7982 }
7983
7984 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7985 {
7986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7987 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7988 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7989 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7990 goto exit;
7991 }
7992
7993 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7994 buf, hash_len ) != 0 )
7995 {
7996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7998 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7999 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8000 goto exit;
8001 }
8002
8003#if defined(MBEDTLS_SSL_RENEGOTIATION)
8004 ssl->verify_data_len = hash_len;
8005 memcpy( ssl->peer_verify_data, buf, hash_len );
8006#endif
8007
8008 if( ssl->handshake->resume != 0 )
8009 {
8010#if defined(MBEDTLS_SSL_CLI_C)
8011 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8012 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
8013#endif
8014#if defined(MBEDTLS_SSL_SRV_C)
8015 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8016 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
8017#endif
8018 }
8019 else
8020 ssl->state++;
8021
8022#if defined(MBEDTLS_SSL_PROTO_DTLS)
8023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8024 mbedtls_ssl_recv_flight_completed( ssl );
8025#endif
8026
8027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
8028
8029exit:
8030 mbedtls_platform_zeroize( buf, hash_len );
8031 return( ret );
8032}
8033
Jerry Yu392112c2022-02-17 14:34:10 +08008034#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8035/*
8036 * Helper to get TLS 1.2 PRF from ciphersuite
8037 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8038 */
8039static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
8040{
Jerry Yu392112c2022-02-17 14:34:10 +08008041 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04008042 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
8043#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07008044 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08008045 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04008046 else
Jerry Yu392112c2022-02-17 14:34:10 +08008047#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008048#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8049 {
8050 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
8051 return( tls_prf_sha256 );
8052 }
8053#endif
8054#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8055 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8056 (void) ciphersuite_info;
8057#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008058
Andrzej Kurek894edde2022-09-29 06:31:14 -04008059 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08008060}
8061#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008062
8063static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
8064{
8065 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008066#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008067 if( tls_prf == tls_prf_sha384 )
8068 {
8069 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
8070 }
8071 else
8072#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008073#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008074 if( tls_prf == tls_prf_sha256 )
8075 {
8076 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
8077 }
8078 else
8079#endif
8080 return( MBEDTLS_SSL_TLS_PRF_NONE );
8081}
8082
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008083/*
8084 * Populate a transform structure with session keys and all the other
8085 * necessary information.
8086 *
8087 * Parameters:
8088 * - [in/out]: transform: structure to populate
8089 * [in] must be just initialised with mbedtls_ssl_transform_init()
8090 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8091 * - [in] ciphersuite
8092 * - [in] master
8093 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008094 * - [in] tls_prf: pointer to PRF to use for key derivation
8095 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008096 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097 * - [in] endpoint: client or server
8098 * - [in] ssl: used for:
8099 * - ssl->conf->{f,p}_export_keys
8100 * [in] optionally used for:
8101 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8102 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008103MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008104static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
8105 int ciphersuite,
8106 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008107#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008108 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008109#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008110 ssl_tls_prf_t tls_prf,
8111 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04008112 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008113 unsigned endpoint,
8114 const mbedtls_ssl_context *ssl )
8115{
8116 int ret = 0;
8117 unsigned char keyblk[256];
8118 unsigned char *key1;
8119 unsigned char *key2;
8120 unsigned char *mac_enc;
8121 unsigned char *mac_dec;
8122 size_t mac_key_len = 0;
8123 size_t iv_copy_len;
8124 size_t keylen;
8125 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008126 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008127#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008128 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008130#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008131
8132#if defined(MBEDTLS_USE_PSA_CRYPTO)
8133 psa_key_type_t key_type;
8134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8135 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008136 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008137 size_t key_bits;
8138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8139#endif
8140
8141#if !defined(MBEDTLS_DEBUG_C) && \
8142 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8143 if( ssl->f_export_keys == NULL )
8144 {
8145 ssl = NULL; /* make sure we don't use it except for these cases */
8146 (void) ssl;
8147 }
8148#endif
8149
8150 /*
8151 * Some data just needs copying into the structure
8152 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008153#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008154 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008155#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008156 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008157
8158#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8159 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
8160#endif
8161
8162#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04008163 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164 {
8165 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8166 * generation separate. This should never happen. */
8167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8168 }
8169#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8170
8171 /*
8172 * Get various info structures
8173 */
8174 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
8175 if( ciphersuite_info == NULL )
8176 {
8177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
8178 ciphersuite ) );
8179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8180 }
8181
Neil Armstrongab555e02022-04-04 11:07:59 +02008182 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008183#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008184 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008185#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008186 ciphersuite_info );
8187
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008188 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
8189 transform->taglen =
8190 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
8191
8192#if defined(MBEDTLS_USE_PSA_CRYPTO)
8193 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
8194 transform->taglen,
8195 &alg,
8196 &key_type,
8197 &key_bits ) ) != PSA_SUCCESS )
8198 {
8199 ret = psa_ssl_status_to_mbedtls( status );
8200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
8201 goto end;
8202 }
8203#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008204 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
8205 if( cipher_info == NULL )
8206 {
8207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
8208 ciphersuite_info->cipher ) );
8209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8210 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008211#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008212
Neil Armstronge4512952022-03-08 09:08:22 +01008213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008214 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01008215 if( mac_alg == 0 )
8216 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01008218 (unsigned) ciphersuite_info->mac ) );
8219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8220 }
8221#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008222 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
8223 if( md_info == NULL )
8224 {
8225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
8226 (unsigned) ciphersuite_info->mac ) );
8227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8228 }
Neil Armstronge4512952022-03-08 09:08:22 +01008229#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008230
8231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8232 /* Copy own and peer's CID if the use of the CID
8233 * extension has been negotiated. */
8234 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
8235 {
8236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
8237
8238 transform->in_cid_len = ssl->own_cid_len;
8239 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
8240 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
8241 transform->in_cid_len );
8242
8243 transform->out_cid_len = ssl->handshake->peer_cid_len;
8244 memcpy( transform->out_cid, ssl->handshake->peer_cid,
8245 ssl->handshake->peer_cid_len );
8246 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
8247 transform->out_cid_len );
8248 }
8249#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8250
8251 /*
8252 * Compute key block using the PRF
8253 */
8254 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
8255 if( ret != 0 )
8256 {
8257 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
8258 return( ret );
8259 }
8260
8261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
8262 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
8263 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
8264 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
8265 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
8266
8267 /*
8268 * Determine the appropriate key, IV and MAC length.
8269 */
8270
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008271#if defined(MBEDTLS_USE_PSA_CRYPTO)
8272 keylen = PSA_BITS_TO_BYTES(key_bits);
8273#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008275#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276
8277#if defined(MBEDTLS_GCM_C) || \
8278 defined(MBEDTLS_CCM_C) || \
8279 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008280 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008281 {
8282 size_t explicit_ivlen;
8283
8284 transform->maclen = 0;
8285 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008286
8287 /* All modes haves 96-bit IVs, but the length of the static parts vary
8288 * with mode and version:
8289 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8290 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8291 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8292 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8293 * sequence number).
8294 */
8295 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008296
8297 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008298#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008299 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008300#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008301 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8302 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008303#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008304
8305 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306 transform->fixed_ivlen = 12;
8307 else
8308 transform->fixed_ivlen = 4;
8309
8310 /* Minimum length of encrypted record */
8311 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8312 transform->minlen = explicit_ivlen + transform->taglen;
8313 }
8314 else
8315#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8316#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008317 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8318 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8319 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320 {
Neil Armstronge4512952022-03-08 09:08:22 +01008321#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008322 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008323#else
8324 size_t block_size = cipher_info->block_size;
8325#endif /* MBEDTLS_USE_PSA_CRYPTO */
8326
8327#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008328 /* Get MAC length */
8329 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8330#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008331 /* Initialize HMAC contexts */
8332 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8333 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8334 {
8335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8336 goto end;
8337 }
8338
8339 /* Get MAC length */
8340 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008341#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008342 transform->maclen = mac_key_len;
8343
8344 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008345#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008346 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008347#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008348 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008349#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008350
8351 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008352 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353 transform->minlen = transform->maclen;
8354 else
8355 {
8356 /*
8357 * GenericBlockCipher:
8358 * 1. if EtM is in use: one block plus MAC
8359 * otherwise: * first multiple of blocklen greater than maclen
8360 * 2. IV
8361 */
8362#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008363 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008364 {
8365 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008366 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008367 }
8368 else
8369#endif
8370 {
8371 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008372 + block_size
8373 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 }
8375
Glenn Strauss07c64162022-03-14 12:34:51 -04008376 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008377 {
8378 transform->minlen += transform->ivlen;
8379 }
8380 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008381 {
8382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8383 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8384 goto end;
8385 }
8386 }
8387 }
8388 else
8389#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8390 {
8391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8393 }
8394
8395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8396 (unsigned) keylen,
8397 (unsigned) transform->minlen,
8398 (unsigned) transform->ivlen,
8399 (unsigned) transform->maclen ) );
8400
8401 /*
8402 * Finally setup the cipher contexts, IVs and MAC secrets.
8403 */
8404#if defined(MBEDTLS_SSL_CLI_C)
8405 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8406 {
8407 key1 = keyblk + mac_key_len * 2;
8408 key2 = keyblk + mac_key_len * 2 + keylen;
8409
8410 mac_enc = keyblk;
8411 mac_dec = keyblk + mac_key_len;
8412
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008413 iv_copy_len = ( transform->fixed_ivlen ) ?
8414 transform->fixed_ivlen : transform->ivlen;
8415 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8416 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8417 iv_copy_len );
8418 }
8419 else
8420#endif /* MBEDTLS_SSL_CLI_C */
8421#if defined(MBEDTLS_SSL_SRV_C)
8422 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8423 {
8424 key1 = keyblk + mac_key_len * 2 + keylen;
8425 key2 = keyblk + mac_key_len * 2;
8426
8427 mac_enc = keyblk + mac_key_len;
8428 mac_dec = keyblk;
8429
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008430 iv_copy_len = ( transform->fixed_ivlen ) ?
8431 transform->fixed_ivlen : transform->ivlen;
8432 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8433 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8434 iv_copy_len );
8435 }
8436 else
8437#endif /* MBEDTLS_SSL_SRV_C */
8438 {
8439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8440 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8441 goto end;
8442 }
8443
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008444 if( ssl != NULL && ssl->f_export_keys != NULL )
8445 {
8446 ssl->f_export_keys( ssl->p_export_keys,
8447 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8448 master, 48,
8449 randbytes + 32,
8450 randbytes,
8451 tls_prf_get_type( tls_prf ) );
8452 }
8453
8454#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008455 transform->psa_alg = alg;
8456
8457 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8458 {
8459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8460 psa_set_key_algorithm( &attributes, alg );
8461 psa_set_key_type( &attributes, key_type );
8462
8463 if( ( status = psa_import_key( &attributes,
8464 key1,
8465 PSA_BITS_TO_BYTES( key_bits ),
8466 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8467 {
8468 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8469 ret = psa_ssl_status_to_mbedtls( status );
8470 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8471 goto end;
8472 }
8473
8474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8475
8476 if( ( status = psa_import_key( &attributes,
8477 key2,
8478 PSA_BITS_TO_BYTES( key_bits ),
8479 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8480 {
8481 ret = psa_ssl_status_to_mbedtls( status );
8482 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8483 goto end;
8484 }
8485 }
8486#else
8487 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8488 cipher_info ) ) != 0 )
8489 {
8490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8491 goto end;
8492 }
8493
8494 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8495 cipher_info ) ) != 0 )
8496 {
8497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8498 goto end;
8499 }
8500
8501 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8502 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8503 MBEDTLS_ENCRYPT ) ) != 0 )
8504 {
8505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8506 goto end;
8507 }
8508
8509 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8510 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8511 MBEDTLS_DECRYPT ) ) != 0 )
8512 {
8513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8514 goto end;
8515 }
8516
8517#if defined(MBEDTLS_CIPHER_MODE_CBC)
8518 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8519 {
8520 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8521 MBEDTLS_PADDING_NONE ) ) != 0 )
8522 {
8523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8524 goto end;
8525 }
8526
8527 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8528 MBEDTLS_PADDING_NONE ) ) != 0 )
8529 {
8530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8531 goto end;
8532 }
8533 }
8534#endif /* MBEDTLS_CIPHER_MODE_CBC */
8535#endif /* MBEDTLS_USE_PSA_CRYPTO */
8536
Neil Armstrong29c0c042022-03-17 17:47:28 +01008537#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8538 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8539 For AEAD-based ciphersuites, there is nothing to do here. */
8540 if( mac_key_len != 0 )
8541 {
8542#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008543 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008544
8545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008546 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008547 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8548
8549 if( ( status = psa_import_key( &attributes,
8550 mac_enc, mac_key_len,
8551 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8552 {
8553 ret = psa_ssl_status_to_mbedtls( status );
8554 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8555 goto end;
8556 }
8557
Ronald Cronfb39f152022-03-25 14:36:28 +01008558 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008559 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8560#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8561 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8562#endif
8563 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008564 /* mbedtls_ct_hmac() requires the key to be exportable */
8565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8566 PSA_KEY_USAGE_VERIFY_HASH );
8567 else
8568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8569
8570 if( ( status = psa_import_key( &attributes,
8571 mac_dec, mac_key_len,
8572 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8573 {
8574 ret = psa_ssl_status_to_mbedtls( status );
8575 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8576 goto end;
8577 }
8578#else
8579 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8580 if( ret != 0 )
8581 goto end;
8582 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8583 if( ret != 0 )
8584 goto end;
8585#endif /* MBEDTLS_USE_PSA_CRYPTO */
8586 }
8587#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8588
8589 ((void) mac_dec);
8590 ((void) mac_enc);
8591
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008592end:
8593 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8594 return( ret );
8595}
8596
Valerio Settia08b1a42022-11-17 15:10:02 +01008597#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8598 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008599int mbedtls_psa_ecjpake_read_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008600 psa_pake_operation_t *pake_ctx,
8601 const unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008602 size_t len, mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008603{
8604 psa_status_t status;
8605 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008606 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008607 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8608 * At round two perform a single cycle
8609 */
8610 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008611
Valerio Setti6b3dab02022-11-17 17:14:54 +01008612 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008613 {
8614 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8615 step <= PSA_PAKE_STEP_ZK_PROOF;
8616 ++step )
8617 {
8618 /* Length is stored at the first byte */
8619 size_t length = buf[input_offset];
8620 input_offset += 1;
8621
8622 if( input_offset + length > len )
8623 {
8624 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8625 }
8626
8627 status = psa_pake_input( pake_ctx, step,
8628 buf + input_offset, length );
8629 if( status != PSA_SUCCESS)
8630 {
8631 return psa_ssl_status_to_mbedtls( status );
8632 }
8633
8634 input_offset += length;
8635 }
8636 }
8637
Valerio Setti819de862022-11-17 18:05:19 +01008638 if( input_offset != len )
Valerio Setti61ea17d2022-11-18 12:11:00 +01008639 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti30ebe112022-11-17 16:23:34 +01008640
Valerio Settia08b1a42022-11-17 15:10:02 +01008641 return( 0 );
8642}
8643
Valerio Setti6b3dab02022-11-17 17:14:54 +01008644int mbedtls_psa_ecjpake_write_round(
Valerio Settia08b1a42022-11-17 15:10:02 +01008645 psa_pake_operation_t *pake_ctx,
8646 unsigned char *buf,
Valerio Setti6b3dab02022-11-17 17:14:54 +01008647 size_t len, size_t *olen,
8648 mbedtls_ecjpake_rounds_t round )
Valerio Settia08b1a42022-11-17 15:10:02 +01008649{
8650 psa_status_t status;
8651 size_t output_offset = 0;
8652 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008653 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008654 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8655 * At round two perform a single cycle
8656 */
8657 unsigned int remaining_steps = ( round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008658
Valerio Setti6b3dab02022-11-17 17:14:54 +01008659 for( ; remaining_steps > 0; remaining_steps-- )
Valerio Settia08b1a42022-11-17 15:10:02 +01008660 {
Valerio Setti6b3dab02022-11-17 17:14:54 +01008661 for( psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8662 step <= PSA_PAKE_STEP_ZK_PROOF;
Valerio Settia08b1a42022-11-17 15:10:02 +01008663 ++step )
8664 {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008665 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008666 * For each step, prepend 1 byte with the length of the data as
8667 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008668 */
Valerio Settia08b1a42022-11-17 15:10:02 +01008669 status = psa_pake_output( pake_ctx, step,
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008670 buf + output_offset + 1,
8671 len - output_offset - 1,
Valerio Settia08b1a42022-11-17 15:10:02 +01008672 &output_len );
8673 if( status != PSA_SUCCESS )
8674 {
8675 return( psa_ssl_status_to_mbedtls( status ) );
8676 }
8677
Valerio Setti99d88c12022-11-22 16:03:43 +01008678 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008679
8680 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008681 }
8682 }
8683
8684 *olen = output_offset;
8685
8686 return( 0 );
8687}
Valerio Settia08b1a42022-11-17 15:10:02 +01008688#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8689
Jerry Yuee40f9d2022-02-17 14:55:16 +08008690#if defined(MBEDTLS_USE_PSA_CRYPTO)
8691int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8692 unsigned char *hash, size_t *hashlen,
8693 unsigned char *data, size_t data_len,
8694 mbedtls_md_type_t md_alg )
8695{
8696 psa_status_t status;
8697 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008698 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008699
8700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8701
8702 if( ( status = psa_hash_setup( &hash_operation,
8703 hash_alg ) ) != PSA_SUCCESS )
8704 {
8705 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8706 goto exit;
8707 }
8708
8709 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8710 64 ) ) != PSA_SUCCESS )
8711 {
8712 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8713 goto exit;
8714 }
8715
8716 if( ( status = psa_hash_update( &hash_operation,
8717 data, data_len ) ) != PSA_SUCCESS )
8718 {
8719 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8720 goto exit;
8721 }
8722
8723 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8724 hashlen ) ) != PSA_SUCCESS )
8725 {
8726 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8727 goto exit;
8728 }
8729
8730exit:
8731 if( status != PSA_SUCCESS )
8732 {
8733 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8734 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8735 switch( status )
8736 {
8737 case PSA_ERROR_NOT_SUPPORTED:
8738 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8739 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8740 case PSA_ERROR_BUFFER_TOO_SMALL:
8741 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8742 case PSA_ERROR_INSUFFICIENT_MEMORY:
8743 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8744 default:
8745 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8746 }
8747 }
8748 return( 0 );
8749}
8750
8751#else
8752
8753int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8754 unsigned char *hash, size_t *hashlen,
8755 unsigned char *data, size_t data_len,
8756 mbedtls_md_type_t md_alg )
8757{
8758 int ret = 0;
8759 mbedtls_md_context_t ctx;
8760 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8761 *hashlen = mbedtls_md_get_size( md_info );
8762
8763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8764
8765 mbedtls_md_init( &ctx );
8766
8767 /*
8768 * digitally-signed struct {
8769 * opaque client_random[32];
8770 * opaque server_random[32];
8771 * ServerDHParams params;
8772 * };
8773 */
8774 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8775 {
8776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8777 goto exit;
8778 }
8779 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8780 {
8781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8782 goto exit;
8783 }
8784 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8785 {
8786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8787 goto exit;
8788 }
8789 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8790 {
8791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8792 goto exit;
8793 }
8794 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8795 {
8796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8797 goto exit;
8798 }
8799
8800exit:
8801 mbedtls_md_free( &ctx );
8802
8803 if( ret != 0 )
8804 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8805 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8806
8807 return( ret );
8808}
8809#endif /* MBEDTLS_USE_PSA_CRYPTO */
8810
Jerry Yud9d91da2022-02-17 14:57:06 +08008811#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8812
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008813/* Find the preferred hash for a given signature algorithm. */
8814unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8815 mbedtls_ssl_context *ssl,
8816 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008817{
Gabor Mezei078e8032022-04-27 21:17:56 +02008818 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008819 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008820
8821 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008822 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008823
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008824 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008825 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008826 unsigned int hash_alg_received =
8827 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8828 received_sig_algs[i] );
8829 unsigned int sig_alg_received =
8830 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8831 received_sig_algs[i] );
8832
8833 if( sig_alg == sig_alg_received )
8834 {
8835#if defined(MBEDTLS_USE_PSA_CRYPTO)
8836 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8837 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008838 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008839 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008840
Neil Armstrong96eceb82022-06-30 18:05:05 +02008841 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8842 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8843 PSA_ALG_ECDSA( psa_hash_alg ),
8844 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008845 continue;
8846
Neil Armstrong96eceb82022-06-30 18:05:05 +02008847 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008848 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8849 PSA_ALG_RSA_PKCS1V15_SIGN(
8850 psa_hash_alg ),
8851 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008852 continue;
8853 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008854#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008855
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008856 return( hash_alg_received );
8857 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008858 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008859
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008860 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008861}
8862
8863#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8864
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008865/* Serialization of TLS 1.2 sessions:
8866 *
8867 * struct {
8868 * uint64 start_time;
8869 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008870 * uint8 session_id_len; // at most 32
8871 * opaque session_id[32];
8872 * opaque master[48]; // fixed length in the standard
8873 * uint32 verify_result;
8874 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8875 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8876 * uint32 ticket_lifetime;
8877 * uint8 mfl_code; // up to 255 according to standard
8878 * uint8 encrypt_then_mac; // 0 or 1
8879 * } serialized_session_tls12;
8880 *
8881 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008882static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008883 unsigned char *buf,
8884 size_t buf_len )
8885{
8886 unsigned char *p = buf;
8887 size_t used = 0;
8888
8889#if defined(MBEDTLS_HAVE_TIME)
8890 uint64_t start;
8891#endif
8892#if defined(MBEDTLS_X509_CRT_PARSE_C)
8893#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8894 size_t cert_len;
8895#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8896#endif /* MBEDTLS_X509_CRT_PARSE_C */
8897
8898 /*
8899 * Time
8900 */
8901#if defined(MBEDTLS_HAVE_TIME)
8902 used += 8;
8903
8904 if( used <= buf_len )
8905 {
8906 start = (uint64_t) session->start;
8907
8908 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8909 p += 8;
8910 }
8911#endif /* MBEDTLS_HAVE_TIME */
8912
8913 /*
8914 * Basic mandatory fields
8915 */
8916 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917 + 1 /* id_len */
8918 + sizeof( session->id )
8919 + sizeof( session->master )
8920 + 4; /* verify_result */
8921
8922 if( used <= buf_len )
8923 {
8924 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8925 p += 2;
8926
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 *p++ = MBEDTLS_BYTE_0( session->id_len );
8928 memcpy( p, session->id, 32 );
8929 p += 32;
8930
8931 memcpy( p, session->master, 48 );
8932 p += 48;
8933
8934 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8935 p += 4;
8936 }
8937
8938 /*
8939 * Peer's end-entity certificate
8940 */
8941#if defined(MBEDTLS_X509_CRT_PARSE_C)
8942#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8943 if( session->peer_cert == NULL )
8944 cert_len = 0;
8945 else
8946 cert_len = session->peer_cert->raw.len;
8947
8948 used += 3 + cert_len;
8949
8950 if( used <= buf_len )
8951 {
8952 *p++ = MBEDTLS_BYTE_2( cert_len );
8953 *p++ = MBEDTLS_BYTE_1( cert_len );
8954 *p++ = MBEDTLS_BYTE_0( cert_len );
8955
8956 if( session->peer_cert != NULL )
8957 {
8958 memcpy( p, session->peer_cert->raw.p, cert_len );
8959 p += cert_len;
8960 }
8961 }
8962#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8963 if( session->peer_cert_digest != NULL )
8964 {
8965 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8966 if( used <= buf_len )
8967 {
8968 *p++ = (unsigned char) session->peer_cert_digest_type;
8969 *p++ = (unsigned char) session->peer_cert_digest_len;
8970 memcpy( p, session->peer_cert_digest,
8971 session->peer_cert_digest_len );
8972 p += session->peer_cert_digest_len;
8973 }
8974 }
8975 else
8976 {
8977 used += 2;
8978 if( used <= buf_len )
8979 {
8980 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8981 *p++ = 0;
8982 }
8983 }
8984#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8985#endif /* MBEDTLS_X509_CRT_PARSE_C */
8986
8987 /*
8988 * Session ticket if any, plus associated data
8989 */
8990#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8991 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8992
8993 if( used <= buf_len )
8994 {
8995 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8996 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8997 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8998
8999 if( session->ticket != NULL )
9000 {
9001 memcpy( p, session->ticket, session->ticket_len );
9002 p += session->ticket_len;
9003 }
9004
9005 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
9006 p += 4;
9007 }
9008#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9009
9010 /*
9011 * Misc extension-related info
9012 */
9013#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9014 used += 1;
9015
9016 if( used <= buf_len )
9017 *p++ = session->mfl_code;
9018#endif
9019
9020#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9021 used += 1;
9022
9023 if( used <= buf_len )
9024 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
9025#endif
9026
9027 return( used );
9028}
9029
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009030MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00009031static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 const unsigned char *buf,
9033 size_t len )
9034{
9035#if defined(MBEDTLS_HAVE_TIME)
9036 uint64_t start;
9037#endif
9038#if defined(MBEDTLS_X509_CRT_PARSE_C)
9039#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9040 size_t cert_len;
9041#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9042#endif /* MBEDTLS_X509_CRT_PARSE_C */
9043
9044 const unsigned char *p = buf;
9045 const unsigned char * const end = buf + len;
9046
9047 /*
9048 * Time
9049 */
9050#if defined(MBEDTLS_HAVE_TIME)
9051 if( 8 > (size_t)( end - p ) )
9052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9053
9054 start = ( (uint64_t) p[0] << 56 ) |
9055 ( (uint64_t) p[1] << 48 ) |
9056 ( (uint64_t) p[2] << 40 ) |
9057 ( (uint64_t) p[3] << 32 ) |
9058 ( (uint64_t) p[4] << 24 ) |
9059 ( (uint64_t) p[5] << 16 ) |
9060 ( (uint64_t) p[6] << 8 ) |
9061 ( (uint64_t) p[7] );
9062 p += 8;
9063
9064 session->start = (time_t) start;
9065#endif /* MBEDTLS_HAVE_TIME */
9066
9067 /*
9068 * Basic mandatory fields
9069 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01009070 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9072
9073 session->ciphersuite = ( p[0] << 8 ) | p[1];
9074 p += 2;
9075
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009076 session->id_len = *p++;
9077 memcpy( session->id, p, 32 );
9078 p += 32;
9079
9080 memcpy( session->master, p, 48 );
9081 p += 48;
9082
9083 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9084 ( (uint32_t) p[1] << 16 ) |
9085 ( (uint32_t) p[2] << 8 ) |
9086 ( (uint32_t) p[3] );
9087 p += 4;
9088
9089 /* Immediately clear invalid pointer values that have been read, in case
9090 * we exit early before we replaced them with valid ones. */
9091#if defined(MBEDTLS_X509_CRT_PARSE_C)
9092#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9093 session->peer_cert = NULL;
9094#else
9095 session->peer_cert_digest = NULL;
9096#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9097#endif /* MBEDTLS_X509_CRT_PARSE_C */
9098#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9099 session->ticket = NULL;
9100#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9101
9102 /*
9103 * Peer certificate
9104 */
9105#if defined(MBEDTLS_X509_CRT_PARSE_C)
9106#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9107 /* Deserialize CRT from the end of the ticket. */
9108 if( 3 > (size_t)( end - p ) )
9109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9110
9111 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9112 p += 3;
9113
9114 if( cert_len != 0 )
9115 {
9116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9117
9118 if( cert_len > (size_t)( end - p ) )
9119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9120
9121 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9122
9123 if( session->peer_cert == NULL )
9124 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9125
9126 mbedtls_x509_crt_init( session->peer_cert );
9127
9128 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9129 p, cert_len ) ) != 0 )
9130 {
9131 mbedtls_x509_crt_free( session->peer_cert );
9132 mbedtls_free( session->peer_cert );
9133 session->peer_cert = NULL;
9134 return( ret );
9135 }
9136
9137 p += cert_len;
9138 }
9139#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9140 /* Deserialize CRT digest from the end of the ticket. */
9141 if( 2 > (size_t)( end - p ) )
9142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9143
9144 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9145 session->peer_cert_digest_len = (size_t) *p++;
9146
9147 if( session->peer_cert_digest_len != 0 )
9148 {
9149 const mbedtls_md_info_t *md_info =
9150 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9151 if( md_info == NULL )
9152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9153 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9155
9156 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9158
9159 session->peer_cert_digest =
9160 mbedtls_calloc( 1, session->peer_cert_digest_len );
9161 if( session->peer_cert_digest == NULL )
9162 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9163
9164 memcpy( session->peer_cert_digest, p,
9165 session->peer_cert_digest_len );
9166 p += session->peer_cert_digest_len;
9167 }
9168#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9169#endif /* MBEDTLS_X509_CRT_PARSE_C */
9170
9171 /*
9172 * Session ticket and associated data
9173 */
9174#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9175 if( 3 > (size_t)( end - p ) )
9176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9177
9178 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9179 p += 3;
9180
9181 if( session->ticket_len != 0 )
9182 {
9183 if( session->ticket_len > (size_t)( end - p ) )
9184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9185
9186 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9187 if( session->ticket == NULL )
9188 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9189
9190 memcpy( session->ticket, p, session->ticket_len );
9191 p += session->ticket_len;
9192 }
9193
9194 if( 4 > (size_t)( end - p ) )
9195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9196
9197 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9198 ( (uint32_t) p[1] << 16 ) |
9199 ( (uint32_t) p[2] << 8 ) |
9200 ( (uint32_t) p[3] );
9201 p += 4;
9202#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9203
9204 /*
9205 * Misc extension-related info
9206 */
9207#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9208 if( 1 > (size_t)( end - p ) )
9209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9210
9211 session->mfl_code = *p++;
9212#endif
9213
9214#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9215 if( 1 > (size_t)( end - p ) )
9216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9217
9218 session->encrypt_then_mac = *p++;
9219#endif
9220
9221 /* Done, should have consumed entire buffer */
9222 if( p != end )
9223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9224
9225 return( 0 );
9226}
Jerry Yudc7bd172022-02-17 13:44:15 +08009227#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9228
XiaokangQian75d40ef2022-04-20 11:05:24 +00009229int mbedtls_ssl_validate_ciphersuite(
9230 const mbedtls_ssl_context *ssl,
9231 const mbedtls_ssl_ciphersuite_t *suite_info,
9232 mbedtls_ssl_protocol_version min_tls_version,
9233 mbedtls_ssl_protocol_version max_tls_version )
9234{
9235 (void) ssl;
9236
9237 if( suite_info == NULL )
9238 return( -1 );
9239
9240 if( ( suite_info->min_tls_version > max_tls_version ) ||
9241 ( suite_info->max_tls_version < min_tls_version ) )
9242 {
9243 return( -1 );
9244 }
9245
XiaokangQian060d8672022-04-21 09:24:56 +00009246#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009247#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009248#if defined(MBEDTLS_USE_PSA_CRYPTO)
9249 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9250 ssl->handshake->psa_pake_ctx_is_ok != 1 )
9251#else
XiaokangQian75d40ef2022-04-20 11:05:24 +00009252 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9253 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
Neil Armstrongca7d5062022-05-31 14:43:23 +02009254#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009255 {
9256 return( -1 );
9257 }
9258#endif
9259
9260 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9261#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
9262 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
9263 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
9264 {
9265 return( -1 );
9266 }
9267#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9268#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9269
9270 return( 0 );
9271}
9272
Ronald Crone68ab4f2022-10-05 12:46:29 +02009273#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009274/*
9275 * Function for writing a signature algorithm extension.
9276 *
9277 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9278 * value (TLS 1.3 RFC8446):
9279 * enum {
9280 * ....
9281 * ecdsa_secp256r1_sha256( 0x0403 ),
9282 * ecdsa_secp384r1_sha384( 0x0503 ),
9283 * ecdsa_secp521r1_sha512( 0x0603 ),
9284 * ....
9285 * } SignatureScheme;
9286 *
9287 * struct {
9288 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9289 * } SignatureSchemeList;
9290 *
9291 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9292 * value (TLS 1.2 RFC5246):
9293 * enum {
9294 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9295 * sha512(6), (255)
9296 * } HashAlgorithm;
9297 *
9298 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9299 * SignatureAlgorithm;
9300 *
9301 * struct {
9302 * HashAlgorithm hash;
9303 * SignatureAlgorithm signature;
9304 * } SignatureAndHashAlgorithm;
9305 *
9306 * SignatureAndHashAlgorithm
9307 * supported_signature_algorithms<2..2^16-2>;
9308 *
9309 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9310 * generalization of the TLS 1.2 signature algorithm extension.
9311 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9312 * `SignatureScheme` field of TLS 1.3
9313 *
9314 */
9315int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
9316 const unsigned char *end, size_t *out_len )
9317{
9318 unsigned char *p = buf;
9319 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9320 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9321
9322 *out_len = 0;
9323
9324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
9325
9326 /* Check if we have space for header and length field:
9327 * - extension_type (2 bytes)
9328 * - extension_data_length (2 bytes)
9329 * - supported_signature_algorithms_length (2 bytes)
9330 */
9331 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
9332 p += 6;
9333
9334 /*
9335 * Write supported_signature_algorithms
9336 */
9337 supported_sig_alg = p;
9338 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
9339 if( sig_alg == NULL )
9340 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
9341
9342 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
9343 {
Jerry Yu53f5c152022-06-22 20:24:38 +08009344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
9345 *sig_alg,
9346 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009347 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
9348 continue;
9349 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
9350 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
9351 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08009352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08009353 *sig_alg,
9354 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00009355 }
9356
9357 /* Length of supported_signature_algorithms */
9358 supported_sig_alg_len = p - supported_sig_alg;
9359 if( supported_sig_alg_len == 0 )
9360 {
9361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
9362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
9363 }
9364
XiaokangQianeaf36512022-04-24 09:07:44 +00009365 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009366 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00009367 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
9368
XiaokangQianeaf36512022-04-24 09:07:44 +00009369 *out_len = p - buf;
9370
9371#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08009372 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00009373#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009374
XiaokangQianeaf36512022-04-24 09:07:44 +00009375 return( 0 );
9376}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009377#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009378
XiaokangQian40a35232022-05-07 09:02:40 +00009379#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009380/*
9381 * mbedtls_ssl_parse_server_name_ext
9382 *
9383 * Structure of server_name extension:
9384 *
9385 * enum {
9386 * host_name(0), (255)
9387 * } NameType;
9388 * opaque HostName<1..2^16-1>;
9389 *
9390 * struct {
9391 * NameType name_type;
9392 * select (name_type) {
9393 * case host_name: HostName;
9394 * } name;
9395 * } ServerName;
9396 * struct {
9397 * ServerName server_name_list<1..2^16-1>
9398 * } ServerNameList;
9399 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009400MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009401int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9402 const unsigned char *buf,
9403 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009404{
9405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9406 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009407 size_t server_name_list_len, hostname_len;
9408 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009409
XiaokangQianf2a94202022-05-20 06:44:24 +00009410 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009411
9412 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009413 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009414 p += 2;
9415
XiaokangQian9b2b7712022-05-17 02:57:00 +00009416 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9417 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009418 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009419 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009420 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009421 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009422 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9423 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009424
9425 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9426 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009427 /* sni_name is intended to be used only during the parsing of the
9428 * ClientHello message (it is reset to NULL before the end of
9429 * the message parsing). Thus it is ok to just point to the
9430 * reception buffer and not make a copy of it.
9431 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009432 ssl->handshake->sni_name = p + 3;
9433 ssl->handshake->sni_name_len = hostname_len;
9434 if( ssl->conf->f_sni == NULL )
9435 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009436 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009437 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009438 if( ret != 0 )
9439 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009441 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9442 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009443 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9444 }
9445 return( 0 );
9446 }
9447
9448 p += hostname_len + 3;
9449 }
9450
9451 return( 0 );
9452}
9453#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9454
XiaokangQianacb39922022-06-17 10:18:48 +00009455#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009456MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009457int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9458 const unsigned char *buf,
9459 const unsigned char *end )
9460{
9461 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009462 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009463 const unsigned char *protocol_name_list;
9464 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009465 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009466
9467 /* If ALPN not configured, just ignore the extension */
9468 if( ssl->conf->alpn_list == NULL )
9469 return( 0 );
9470
9471 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009472 * RFC7301, section 3.1
9473 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009474 *
XiaokangQianc7403452022-06-23 03:24:12 +00009475 * struct {
9476 * ProtocolName protocol_name_list<2..2^16-1>
9477 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009478 */
9479
XiaokangQianc7403452022-06-23 03:24:12 +00009480 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009481 * protocol_name_list_len 2 bytes
9482 * protocol_name_len 1 bytes
9483 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009484 */
XiaokangQianacb39922022-06-17 10:18:48 +00009485 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9486
XiaokangQianc7403452022-06-23 03:24:12 +00009487 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009488 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009489 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009490 protocol_name_list = p;
9491 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009492
9493 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009494 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009495 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009496 protocol_name_len = *p++;
9497 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9498 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009499 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009500 {
9501 MBEDTLS_SSL_PEND_FATAL_ALERT(
9502 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9503 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009504 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009505 }
9506
9507 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009508 }
9509
9510 /* Use our order of preference */
9511 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9512 {
9513 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009514 p = protocol_name_list;
9515 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009516 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009517 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009518 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009519 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009520 {
9521 ssl->alpn_chosen = *alpn;
9522 return( 0 );
9523 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009524
9525 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009526 }
9527 }
9528
XiaokangQian95d5f542022-06-24 02:29:26 +00009529 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009530 MBEDTLS_SSL_PEND_FATAL_ALERT(
9531 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9532 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9533 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9534}
9535
9536int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9537 unsigned char *buf,
9538 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009539 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009540{
9541 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009542 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009543 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009544
9545 if( ssl->alpn_chosen == NULL )
9546 {
9547 return( 0 );
9548 }
9549
XiaokangQian95d5f542022-06-24 02:29:26 +00009550 protocol_name_len = strlen( ssl->alpn_chosen );
9551 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009552
9553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9554 /*
9555 * 0 . 1 ext identifier
9556 * 2 . 3 ext length
9557 * 4 . 5 protocol list length
9558 * 6 . 6 protocol name length
9559 * 7 . 7+n protocol name
9560 */
9561 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9562
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009564
XiaokangQian95d5f542022-06-24 02:29:26 +00009565 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9566 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009567 /* Note: the length of the chosen protocol has been checked to be less
9568 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9569 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009570 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009571
XiaokangQian95d5f542022-06-24 02:29:26 +00009572 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009573
9574#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9575 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9576#endif
9577
XiaokangQianacb39922022-06-17 10:18:48 +00009578 return ( 0 );
9579}
9580#endif /* MBEDTLS_SSL_ALPN */
9581
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009582#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009583 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009584 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009585 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009586int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9587 const char *hostname )
9588{
9589 /* Initialize to suppress unnecessary compiler warning */
9590 size_t hostname_len = 0;
9591
9592 /* Check if new hostname is valid before
9593 * making any change to current one */
9594 if( hostname != NULL )
9595 {
9596 hostname_len = strlen( hostname );
9597
9598 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9600 }
9601
9602 /* Now it's clear that we will overwrite the old hostname,
9603 * so we can free it safely */
9604 if( session->hostname != NULL )
9605 {
9606 mbedtls_platform_zeroize( session->hostname,
9607 strlen( session->hostname ) );
9608 mbedtls_free( session->hostname );
9609 }
9610
9611 /* Passing NULL as hostname shall clear the old one */
9612 if( hostname == NULL )
9613 {
9614 session->hostname = NULL;
9615 }
9616 else
9617 {
9618 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9619 if( session->hostname == NULL )
9620 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9621
9622 memcpy( session->hostname, hostname, hostname_len );
9623 }
9624
9625 return( 0 );
9626}
Xiaokang Qian03409292022-10-12 02:49:52 +00009627#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9628 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009629 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009630 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632#endif /* MBEDTLS_SSL_TLS_C */