blob: 945a2e9bdeed5139be699ba7c6f1d90442b0cf24 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Paul Elliott27b0d942022-03-18 21:55:32 +0000171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
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)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200910 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200911#if defined(MBEDTLS_SSL_CLI_C)
912 handshake->ecjpake_cache = NULL;
913 handshake->ecjpake_cache_len = 0;
914#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200915#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200916
Gilles Peskineeccd8882020-03-10 12:19:08 +0100917#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +0200918 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200919#endif
920
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200921#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
922 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
923#endif
Hanno Becker75173122019-02-06 16:18:31 +0000924
925#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
926 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
927 mbedtls_pk_init( &handshake->peer_pubkey );
928#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200929}
930
Hanno Beckera18d1322018-01-03 14:27:32 +0000931void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200934
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100935#if defined(MBEDTLS_USE_PSA_CRYPTO)
936 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
937 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100938#else
939 mbedtls_cipher_init( &transform->cipher_ctx_enc );
940 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100941#endif
942
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000943#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100944#if defined(MBEDTLS_USE_PSA_CRYPTO)
945 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
946 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100947#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_md_init( &transform->md_ctx_enc );
949 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +0000950#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100951#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200952}
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957}
958
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200959MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +0000961{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +0000963 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +0200968 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969
970 /*
971 * Either the pointers are now NULL or cleared properly and can be freed.
972 * Now allocate missing structures.
973 */
974 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200975 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200976 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200977 }
Paul Bakker48916f92012-09-16 19:57:18 +0000978
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200981 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200982 }
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakker82788fb2014-10-20 13:59:19 +0200984 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200986 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500988#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
989 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400990
Andrzej Kurek4a063792020-10-21 15:08:44 +0200991 handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
992 MBEDTLS_SSL_OUT_BUFFER_LEN );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500993#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000994
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200995 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +0000996 if( ssl->handshake == NULL ||
997 ssl->transform_negotiate == NULL ||
998 ssl->session_negotiate == NULL )
999 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_free( ssl->handshake );
1003 mbedtls_free( ssl->transform_negotiate );
1004 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001005
1006 ssl->handshake = NULL;
1007 ssl->transform_negotiate = NULL;
1008 ssl->session_negotiate = NULL;
1009
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001010 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00001011 }
1012
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00001015 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02001016 ssl_handshake_params_init( ssl->handshake );
1017
Jerry Yud0766ec2022-09-22 10:46:57 +08001018#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1019 defined(MBEDTLS_SSL_SRV_C) && \
1020 defined(MBEDTLS_SSL_SESSION_TICKETS)
1021 ssl->handshake->new_session_tickets_count =
1022 ssl->conf->new_session_tickets_count ;
1023#endif
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1027 {
1028 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001029
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1031 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
1032 else
1033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001034
Hanno Becker0f57a652020-02-05 10:37:26 +00001035 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001036 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001037#endif
1038
Brett Warrene0edc842021-08-17 09:53:13 +01001039/*
1040 * curve_list is translated to IANA TLS group identifiers here because
1041 * mbedtls_ssl_conf_curves returns void and so can't return
1042 * any error codes.
1043 */
1044#if defined(MBEDTLS_ECP_C)
1045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1046 /* Heap allocate and translate curve_list from internal to IANA group ids */
1047 if ( ssl->conf->curve_list != NULL )
1048 {
1049 size_t length;
1050 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1051
1052 for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) &&
1053 ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {}
1054
1055 /* Leave room for zero termination */
1056 uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) );
1057 if ( group_list == NULL )
1058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1059
1060 for( size_t i = 0; i < length; i++ )
1061 {
1062 const mbedtls_ecp_curve_info *info =
1063 mbedtls_ecp_curve_info_from_grp_id( curve_list[i] );
1064 if ( info == NULL )
1065 {
1066 mbedtls_free( group_list );
1067 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1068 }
1069 group_list[i] = info->tls_id;
1070 }
1071
1072 group_list[length] = 0;
1073
1074 ssl->handshake->group_list = group_list;
1075 ssl->handshake->group_list_heap_allocated = 1;
1076 }
1077 else
1078 {
1079 ssl->handshake->group_list = ssl->conf->group_list;
1080 ssl->handshake->group_list_heap_allocated = 0;
1081 }
1082#endif /* MBEDTLS_DEPRECATED_REMOVED */
1083#endif /* MBEDTLS_ECP_C */
1084
Ronald Crone68ab4f2022-10-05 12:46:29 +02001085#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001086#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001088 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1089 signature algorithms IANA identifiers. */
1090 if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) &&
Jerry Yuf017ee42022-01-12 15:49:48 +08001091 ssl->conf->sig_hashes != NULL )
1092 {
1093 const int *md;
1094 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001095 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001096 uint16_t *p;
1097
Jerry Yub476a442022-01-21 18:14:45 +08001098#if defined(static_assert)
1099 static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1100 <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ),
1101 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" );
Jerry Yua68dca22022-01-20 16:28:27 +08001102#endif
1103
Jerry Yuf017ee42022-01-12 15:49:48 +08001104 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1105 {
1106 if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE )
1107 continue;
Jerry Yub476a442022-01-21 18:14:45 +08001108#if defined(MBEDTLS_ECDSA_C)
1109 sig_algs_len += sizeof( uint16_t );
1110#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001111
Jerry Yub476a442022-01-21 18:14:45 +08001112#if defined(MBEDTLS_RSA_C)
1113 sig_algs_len += sizeof( uint16_t );
1114#endif
1115 if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN )
1116 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
Jerry Yuf017ee42022-01-12 15:49:48 +08001117 }
1118
Jerry Yub476a442022-01-21 18:14:45 +08001119 if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN )
Jerry Yuf017ee42022-01-12 15:49:48 +08001120 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1121
Jerry Yub476a442022-01-21 18:14:45 +08001122 ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len +
1123 sizeof( uint16_t ));
Jerry Yuf017ee42022-01-12 15:49:48 +08001124 if( ssl->handshake->sig_algs == NULL )
1125 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1126
1127 p = (uint16_t *)ssl->handshake->sig_algs;
1128 for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
1129 {
1130 unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md );
1131 if( hash == MBEDTLS_SSL_HASH_NONE )
1132 continue;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001133#if defined(MBEDTLS_ECDSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001134 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA);
1135 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001136#endif
1137#if defined(MBEDTLS_RSA_C)
Jerry Yuf017ee42022-01-12 15:49:48 +08001138 *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA);
1139 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001140#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001141 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001142 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001143 ssl->handshake->sig_algs_heap_allocated = 1;
1144 }
1145 else
Jerry Yua69269a2022-01-17 21:06:01 +08001146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001147 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001148 ssl->handshake->sig_algs_heap_allocated = 0;
1149 }
Jerry Yucc539102022-06-27 16:27:35 +08001150#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001151#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001152 return( 0 );
1153}
1154
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001155#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001156/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001157MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001158static int ssl_cookie_write_dummy( void *ctx,
1159 unsigned char **p, unsigned char *end,
1160 const unsigned char *cli_id, size_t cli_id_len )
1161{
1162 ((void) ctx);
1163 ((void) p);
1164 ((void) end);
1165 ((void) cli_id);
1166 ((void) cli_id_len);
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001169}
1170
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001171MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001172static int ssl_cookie_check_dummy( void *ctx,
1173 const unsigned char *cookie, size_t cookie_len,
1174 const unsigned char *cli_id, size_t cli_id_len )
1175{
1176 ((void) ctx);
1177 ((void) cookie);
1178 ((void) cookie_len);
1179 ((void) cli_id);
1180 ((void) cli_id_len);
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001183}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001184#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001185
Paul Bakker5121ce52009-01-03 21:22:43 +00001186/*
1187 * Initialize an SSL context
1188 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001189void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
1190{
1191 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
1192}
1193
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001194MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001195static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
1196{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001197 const mbedtls_ssl_config *conf = ssl->conf;
1198
Ronald Cron6f135e12021-12-08 16:57:54 +01001199#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001200 if( mbedtls_ssl_conf_is_tls13_only( conf ) )
1201 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001202 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1203 {
1204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) );
1205 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1206 }
1207
Jerry Yu60835a82021-08-04 10:13:52 +08001208 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
1209 return( 0 );
1210 }
1211#endif
1212
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001214 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001215 {
1216 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
1217 return( 0 );
1218 }
1219#endif
1220
Ronald Cron6f135e12021-12-08 16:57:54 +01001221#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron086ee0b2022-03-15 15:18:51 +01001222 if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) )
Jerry Yu60835a82021-08-04 10:13:52 +08001223 {
XiaokangQianed582dd2022-04-13 08:21:05 +00001224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
1225 {
1226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) );
1227 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1228 }
1229
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001230 if( conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1231 {
1232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) );
1233 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1234 }
1235
1236
Ronald Crone1d3f062022-02-10 14:50:54 +01001237 MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) );
1238 return( 0 );
Jerry Yu60835a82021-08-04 10:13:52 +08001239 }
1240#endif
1241
1242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) );
1243 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1244}
1245
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001246MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001247static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1248{
1249 int ret;
1250 ret = ssl_conf_version_check( ssl );
1251 if( ret != 0 )
1252 return( ret );
1253
Jerry Yudef7ae42022-10-30 14:13:19 +08001254#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1255 /* RFC 8446 section 4.4.3
1256 *
1257 * If the verification fails, the receiver MUST terminate the handshake with
1258 * a "decrypt_error" alert.
1259 *
1260 * If the client is configured as TLS 1.3 only with optional verify, return
1261 * bad config.
1262 *
1263 */
1264 if( mbedtls_ssl_conf_tls13_ephemeral_enabled(
1265 (mbedtls_ssl_context *)ssl ) &&
1266 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1267 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1268 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1269 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
1270 {
1271 MBEDTLS_SSL_DEBUG_MSG(
Dave Rodgmane8734d82022-10-31 14:30:24 +00001272 1, ( "Optional verify auth mode "
Jerry Yudef7ae42022-10-30 14:13:19 +08001273 "is not available for TLS 1.3 client" ) );
1274 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
1275 }
1276#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1277
Jerry Yu60835a82021-08-04 10:13:52 +08001278 /* Space for further checks */
1279
1280 return( 0 );
1281}
1282
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001283/*
1284 * Setup an SSL context
1285 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001286
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001287int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02001288 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289{
Janos Follath865b3eb2019-12-16 11:46:15 +00001290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001291 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1292 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001294 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001295
Jerry Yu60835a82021-08-04 10:13:52 +08001296 if( ( ret = ssl_conf_check( ssl ) ) != 0 )
1297 return( ret );
1298
Paul Bakker62f2dee2012-09-28 07:31:51 +00001299 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001300 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001301 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001302
1303 /* Set to NULL in case of an error condition */
1304 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001305
Darryl Greenb33cc762019-11-28 14:29:44 +00001306#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1307 ssl->in_buf_len = in_buf_len;
1308#endif
1309 ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001310 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001313 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001314 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001315 }
1316
Darryl Greenb33cc762019-11-28 14:29:44 +00001317#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1318 ssl->out_buf_len = out_buf_len;
1319#endif
1320 ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
Angus Grattond8213d02016-05-25 20:56:48 +10001321 if( ssl->out_buf == NULL )
1322 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02001324 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001325 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001328 mbedtls_ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001329
Johan Pascalb62bb512015-12-03 21:56:45 +01001330#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldor3adb9922017-12-21 10:15:08 +02001331 memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
Johan Pascalb62bb512015-12-03 21:56:45 +01001332#endif
1333
Paul Bakker48916f92012-09-16 19:57:18 +00001334 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02001335 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
1337 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02001338
1339error:
1340 mbedtls_free( ssl->in_buf );
1341 mbedtls_free( ssl->out_buf );
1342
1343 ssl->conf = NULL;
1344
Darryl Greenb33cc762019-11-28 14:29:44 +00001345#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1346 ssl->in_buf_len = 0;
1347 ssl->out_buf_len = 0;
1348#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001349 ssl->in_buf = NULL;
1350 ssl->out_buf = NULL;
1351
1352 ssl->in_hdr = NULL;
1353 ssl->in_ctr = NULL;
1354 ssl->in_len = NULL;
1355 ssl->in_iv = NULL;
1356 ssl->in_msg = NULL;
1357
1358 ssl->out_hdr = NULL;
1359 ssl->out_ctr = NULL;
1360 ssl->out_len = NULL;
1361 ssl->out_iv = NULL;
1362 ssl->out_msg = NULL;
1363
k-stachowiak9f7798e2018-07-31 16:52:32 +02001364 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365}
1366
1367/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001368 * Reset an initialized and used SSL context for re-use while retaining
1369 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001370 *
1371 * If partial is non-zero, keep data in the input buffer and client ID.
1372 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001373 */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001374void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl,
1375 int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00001376{
Darryl Greenb33cc762019-11-28 14:29:44 +00001377#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1378 size_t in_buf_len = ssl->in_buf_len;
1379 size_t out_buf_len = ssl->out_buf_len;
1380#else
1381 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1382 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1383#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001384
Hanno Beckerb0302c42021-08-03 09:39:42 +01001385#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1386 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001387#endif
1388
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001389 /* Cancel any possibly running timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00001390 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001391
Hanno Beckerb0302c42021-08-03 09:39:42 +01001392 mbedtls_ssl_reset_in_out_pointers( ssl );
1393
1394 /* Reset incoming message parsing */
1395 ssl->in_offt = NULL;
1396 ssl->nb_zero = 0;
1397 ssl->in_msgtype = 0;
1398 ssl->in_msglen = 0;
1399 ssl->in_hslen = 0;
1400 ssl->keep_current_message = 0;
1401 ssl->transform_in = NULL;
1402
1403#if defined(MBEDTLS_SSL_PROTO_DTLS)
1404 ssl->next_record_offset = 0;
1405 ssl->in_epoch = 0;
1406#endif
1407
1408 /* Keep current datagram if partial == 1 */
1409 if( partial == 0 )
1410 {
1411 ssl->in_left = 0;
1412 memset( ssl->in_buf, 0, in_buf_len );
1413 }
1414
Ronald Cronad8c17b2022-06-10 17:18:09 +02001415 ssl->send_alert = 0;
1416
Hanno Beckerb0302c42021-08-03 09:39:42 +01001417 /* Reset outgoing message writing */
1418 ssl->out_msgtype = 0;
1419 ssl->out_msglen = 0;
1420 ssl->out_left = 0;
1421 memset( ssl->out_buf, 0, out_buf_len );
1422 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
1423 ssl->transform_out = NULL;
1424
1425#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
1426 mbedtls_ssl_dtls_replay_reset( ssl );
1427#endif
1428
XiaokangQian2b01dc32022-01-21 02:53:13 +00001429#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001430 if( ssl->transform )
1431 {
1432 mbedtls_ssl_transform_free( ssl->transform );
1433 mbedtls_free( ssl->transform );
1434 ssl->transform = NULL;
1435 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001436#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1437
XiaokangQian2b01dc32022-01-21 02:53:13 +00001438#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1439 mbedtls_ssl_transform_free( ssl->transform_application );
1440 mbedtls_free( ssl->transform_application );
1441 ssl->transform_application = NULL;
1442
1443 if( ssl->handshake != NULL )
1444 {
1445 mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata );
1446 mbedtls_free( ssl->handshake->transform_earlydata );
1447 ssl->handshake->transform_earlydata = NULL;
1448
1449 mbedtls_ssl_transform_free( ssl->handshake->transform_handshake );
1450 mbedtls_free( ssl->handshake->transform_handshake );
1451 ssl->handshake->transform_handshake = NULL;
1452 }
1453
XiaokangQian2b01dc32022-01-21 02:53:13 +00001454#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001455}
1456
1457int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
1458{
1459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1460
1461 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1462
XiaokangQian78b1fa72022-01-19 06:56:30 +00001463 mbedtls_ssl_session_reset_msg_layer( ssl, partial );
Hanno Beckerb0302c42021-08-03 09:39:42 +01001464
1465 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_RENEGOTIATION)
1467 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001468 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001469
1470 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
1472 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Hanno Beckerb0302c42021-08-03 09:39:42 +01001476 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001477 ssl->session_out = NULL;
Paul Bakkerc0463502013-02-14 11:19:38 +01001478 if( ssl->session )
1479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_ssl_session_free( ssl->session );
1481 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01001482 ssl->session = NULL;
1483 }
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001486 ssl->alpn_chosen = NULL;
1487#endif
1488
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001489#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001490 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001491#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
David Horstmann3b2276a2022-10-06 14:49:08 +01001492 free_cli_id = ( partial == 0 );
Hanno Becker4ccbf062018-08-10 11:20:38 +01001493#endif
David Horstmann3b2276a2022-10-06 14:49:08 +01001494 if( free_cli_id )
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001495 {
1496 mbedtls_free( ssl->cli_id );
1497 ssl->cli_id = NULL;
1498 ssl->cli_id_len = 0;
1499 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001500#endif
1501
Paul Bakker48916f92012-09-16 19:57:18 +00001502 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
1503 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504
1505 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00001506}
1507
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001508/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001509 * Reset an initialized and used SSL context for re-use while retaining
1510 * all application-set variables, function pointers and data.
1511 */
1512int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
1513{
Hanno Becker43aefe22020-02-05 10:44:56 +00001514 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001515}
1516
1517/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 * SSL set accessors
1519 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001520void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00001521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001522 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001523}
1524
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02001525void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001527 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001528}
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001531void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001532{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001533 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001534}
1535#endif
1536
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001537void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001539 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001540}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001543
Hanno Becker1841b0a2018-08-24 11:13:57 +01001544void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
1545 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01001546{
1547 ssl->disable_datagram_packing = !allow_packing;
1548}
1549
1550void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
1551 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001552{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001553 conf->hs_timeout_min = min;
1554 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001555}
1556#endif
1557
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001558void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00001559{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001560 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001561}
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001564void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001565 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001566 void *p_vrfy )
1567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001568 conf->f_vrfy = f_vrfy;
1569 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001572
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001573void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001574 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 void *p_rng )
1576{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001577 conf->f_rng = f_rng;
1578 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001579}
1580
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001581void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02001582 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 void *p_dbg )
1584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001585 conf->f_dbg = f_dbg;
1586 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001587}
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001590 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00001591 mbedtls_ssl_send_t *f_send,
1592 mbedtls_ssl_recv_t *f_recv,
1593 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001594{
1595 ssl->p_bio = p_bio;
1596 ssl->f_send = f_send;
1597 ssl->f_recv = f_recv;
1598 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001599}
1600
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001601#if defined(MBEDTLS_SSL_PROTO_DTLS)
1602void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
1603{
1604 ssl->mtu = mtu;
1605}
1606#endif
1607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001608void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001609{
1610 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001611}
1612
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001613void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
1614 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00001615 mbedtls_ssl_set_timer_t *f_set_timer,
1616 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001617{
1618 ssl->p_timer = p_timer;
1619 ssl->f_set_timer = f_set_timer;
1620 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001621
1622 /* Make sure we start with no timer running */
Hanno Becker0f57a652020-02-05 10:37:26 +00001623 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001624}
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001627void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Hanno Beckera637ff62021-04-15 08:42:48 +01001628 void *p_cache,
1629 mbedtls_ssl_cache_get_t *f_get_cache,
1630 mbedtls_ssl_cache_set_t *f_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00001631{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001632 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001633 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001634 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_CLI_C)
1639int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00001640{
Janos Follath865b3eb2019-12-16 11:46:15 +00001641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001642
1643 if( ssl == NULL ||
1644 session == NULL ||
1645 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001646 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001649 }
1650
Hanno Beckere810bbc2021-05-14 16:01:05 +01001651 if( ssl->handshake->resume == 1 )
1652 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1653
Jerry Yu21092062022-10-10 21:21:31 +08001654#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1655 if( session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu40afab62022-10-08 10:42:13 +08001656 {
Jerry Yu21092062022-10-10 21:21:31 +08001657 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1658 mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1659
1660 if( mbedtls_ssl_validate_ciphersuite(
1661 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
1662 MBEDTLS_SSL_VERSION_TLS1_3 ) != 0 )
1663 {
1664 MBEDTLS_SSL_DEBUG_MSG( 4, ( "%d is not a valid TLS 1.3 ciphersuite.",
1665 session->ciphersuite ) );
1666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1667 }
Jerry Yu40afab62022-10-08 10:42:13 +08001668 }
Jerry Yu21092062022-10-10 21:21:31 +08001669#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001670
Hanno Becker52055ae2019-02-06 14:30:46 +00001671 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
1672 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001673 return( ret );
1674
Paul Bakker0a597072012-09-25 21:55:46 +00001675 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001676
1677 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001681void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
1682 const int *ciphersuites )
1683{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001684 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
1686
Ronald Cron6f135e12021-12-08 16:57:54 +01001687#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yucadebe52021-08-24 10:36:45 +08001688void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf,
Hanno Becker71f1ed62021-07-24 06:01:47 +01001689 const int kex_modes )
1690{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001691 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001692}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001693
1694#if defined(MBEDTLS_SSL_EARLY_DATA)
1695void mbedtls_ssl_tls13_conf_early_data( mbedtls_ssl_config *conf,
Xiaokang Qian72dbfef2022-10-26 06:33:57 +00001696 int early_data_enabled )
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001697{
1698 conf->early_data_enabled = early_data_enabled;
1699}
1700#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001701#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001704void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01001705 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001706{
1707 conf->cert_profile = profile;
1708}
1709
Glenn Strauss36872db2022-01-22 05:06:31 -05001710static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
1711{
1712 mbedtls_ssl_key_cert *cur = key_cert, *next;
1713
1714 while( cur != NULL )
1715 {
1716 next = cur->next;
1717 mbedtls_free( cur );
1718 cur = next;
1719 }
1720}
1721
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001722/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001723MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001724static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
1725 mbedtls_x509_crt *cert,
1726 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001727{
niisato8ee24222018-06-25 19:05:48 +09001728 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001729
Glenn Strauss36872db2022-01-22 05:06:31 -05001730 if( cert == NULL )
1731 {
1732 /* Free list if cert is null */
1733 ssl_key_cert_free( *head );
1734 *head = NULL;
1735 return( 0 );
1736 }
1737
niisato8ee24222018-06-25 19:05:48 +09001738 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
1739 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001740 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001741
niisato8ee24222018-06-25 19:05:48 +09001742 new_cert->cert = cert;
1743 new_cert->key = key;
1744 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745
Glenn Strauss36872db2022-01-22 05:06:31 -05001746 /* Update head if the list was null, else add to the end */
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001747 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01001748 {
niisato8ee24222018-06-25 19:05:48 +09001749 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01001750 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001751 else
1752 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001753 mbedtls_ssl_key_cert *cur = *head;
1754 while( cur->next != NULL )
1755 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09001756 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001757 }
1758
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001759 return( 0 );
1760}
1761
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001762int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001763 mbedtls_x509_crt *own_cert,
1764 mbedtls_pk_context *pk_key )
1765{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02001766 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001767}
1768
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001769void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001770 mbedtls_x509_crt *ca_chain,
1771 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001772{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001773 conf->ca_chain = ca_chain;
1774 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001775
1776#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1777 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1778 * cannot be used together. */
1779 conf->f_ca_cb = NULL;
1780 conf->p_ca_cb = NULL;
1781#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001782}
Hanno Becker5adaad92019-03-27 16:54:37 +00001783
1784#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1785void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00001786 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00001787 void *p_ca_cb )
1788{
1789 conf->f_ca_cb = f_ca_cb;
1790 conf->p_ca_cb = p_ca_cb;
1791
1792 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1793 * cannot be used together. */
1794 conf->ca_chain = NULL;
1795 conf->ca_crl = NULL;
1796}
1797#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001799
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001800#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Glenn Strauss69894072022-01-24 12:58:00 -05001801const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl,
1802 size_t *name_len )
1803{
1804 *name_len = ssl->handshake->sni_name_len;
1805 return( ssl->handshake->sni_name );
1806}
1807
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001808int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
1809 mbedtls_x509_crt *own_cert,
1810 mbedtls_pk_context *pk_key )
1811{
1812 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
1813 own_cert, pk_key ) );
1814}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001815
1816void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
1817 mbedtls_x509_crt *ca_chain,
1818 mbedtls_x509_crl *ca_crl )
1819{
1820 ssl->handshake->sni_ca_chain = ca_chain;
1821 ssl->handshake->sni_ca_crl = ca_crl;
1822}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001823
Glenn Strauss999ef702022-03-11 01:37:23 -05001824#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
1825void mbedtls_ssl_set_hs_dn_hints( mbedtls_ssl_context *ssl,
1826 const mbedtls_x509_crt *crt)
1827{
1828 ssl->handshake->dn_hints = crt;
1829}
1830#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1831
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001832void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
1833 int authmode )
1834{
1835 ssl->handshake->sni_authmode = authmode;
1836}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001837#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1838
Hanno Becker8927c832019-04-03 12:52:50 +01001839#if defined(MBEDTLS_X509_CRT_PARSE_C)
1840void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
1841 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1842 void *p_vrfy )
1843{
1844 ssl->f_vrfy = f_vrfy;
1845 ssl->p_vrfy = p_vrfy;
1846}
1847#endif
1848
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001849#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001850/*
1851 * Set EC J-PAKE password for current handshake
1852 */
1853int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
1854 const unsigned char *pw,
1855 size_t pw_len )
1856{
1857 mbedtls_ecjpake_role role;
1858
Janos Follath8eb64132016-06-03 15:40:57 +01001859 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1861
1862 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
1863 role = MBEDTLS_ECJPAKE_SERVER;
1864 else
1865 role = MBEDTLS_ECJPAKE_CLIENT;
1866
1867 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
1868 role,
1869 MBEDTLS_MD_SHA256,
1870 MBEDTLS_ECP_DP_SECP256R1,
1871 pw, pw_len ) );
1872}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001873#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001874
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001875#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1876int mbedtls_ssl_tls13_has_configured_ticket( mbedtls_ssl_context *ssl )
1877{
1878 mbedtls_ssl_session *session = ssl->session_negotiate;
1879 return( ssl->handshake->resume &&
1880 session != NULL && session->ticket != NULL );
1881}
1882#endif
1883
Ronald Cron73fe8df2022-10-05 14:31:43 +02001884#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001885int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001886{
Ronald Crond29e13e2022-10-19 10:33:48 +02001887 if( conf->psk_identity == NULL ||
1888 conf->psk_identity_len == 0 )
1889 {
1890 return( 0 );
1891 }
1892
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001893#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001894 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001895 return( 1 );
1896#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001897
1898 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001899 return( 1 );
1900
1901 return( 0 );
1902}
1903
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001904static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1905{
1906 /* Remove reference to existing PSK, if any. */
1907#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001908 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001909 {
1910 /* The maintenance of the PSK key slot is the
1911 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001912 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001913 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001914#endif /* MBEDTLS_USE_PSA_CRYPTO */
1915 if( conf->psk != NULL )
1916 {
1917 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1918
1919 mbedtls_free( conf->psk );
1920 conf->psk = NULL;
1921 conf->psk_len = 0;
1922 }
1923
1924 /* Remove reference to PSK identity, if any. */
1925 if( conf->psk_identity != NULL )
1926 {
1927 mbedtls_free( conf->psk_identity );
1928 conf->psk_identity = NULL;
1929 conf->psk_identity_len = 0;
1930 }
1931}
1932
Hanno Becker7390c712018-11-15 13:33:04 +00001933/* This function assumes that PSK identity in the SSL config is unset.
1934 * It checks that the provided identity is well-formed and attempts
1935 * to make a copy of it in the SSL config.
1936 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001937MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001938static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1939 unsigned char const *psk_identity,
1940 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001942 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001943 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001944 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001945 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001946 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001947 {
1948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1949 }
1950
Hanno Becker7390c712018-11-15 13:33:04 +00001951 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1952 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001953 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001954
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001955 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001956 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001957
1958 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001959}
1960
Hanno Becker7390c712018-11-15 13:33:04 +00001961int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1962 const unsigned char *psk, size_t psk_len,
1963 const unsigned char *psk_identity, size_t psk_identity_len )
1964{
Janos Follath865b3eb2019-12-16 11:46:15 +00001965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001966
1967 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001968 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001969 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001970
1971 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001972 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001974 if( psk_len == 0 )
1975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1976 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1978
Hanno Becker7390c712018-11-15 13:33:04 +00001979 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1980 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1981 conf->psk_len = psk_len;
1982 memcpy( conf->psk, psk, conf->psk_len );
1983
1984 /* Check and set PSK Identity */
1985 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1986 if( ret != 0 )
1987 ssl_conf_remove_psk( conf );
1988
1989 return( ret );
1990}
1991
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001992static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1993{
1994#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001995 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001996 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001997 /* The maintenance of the external PSK key slot is the
1998 * user's responsibility. */
1999 if( ssl->handshake->psk_opaque_is_internal )
2000 {
2001 psa_destroy_key( ssl->handshake->psk_opaque );
2002 ssl->handshake->psk_opaque_is_internal = 0;
2003 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002004 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002005 }
Neil Armstronge952a302022-05-03 10:22:14 +02002006#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002007 if( ssl->handshake->psk != NULL )
2008 {
2009 mbedtls_platform_zeroize( ssl->handshake->psk,
2010 ssl->handshake->psk_len );
2011 mbedtls_free( ssl->handshake->psk );
2012 ssl->handshake->psk_len = 0;
2013 }
Neil Armstronge952a302022-05-03 10:22:14 +02002014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002015}
2016
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002017int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2018 const unsigned char *psk, size_t psk_len )
2019{
Neil Armstrong501c9322022-05-03 09:35:09 +02002020#if defined(MBEDTLS_USE_PSA_CRYPTO)
2021 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002023 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002025#endif /* MBEDTLS_USE_PSA_CRYPTO */
2026
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002027 if( psk == NULL || ssl->handshake == NULL )
2028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2029
2030 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2032
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002033 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002034
Neil Armstrong501c9322022-05-03 09:35:09 +02002035#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002036#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2037 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2038 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002039 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002040 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2041 else
2042 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2043 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2044 }
2045#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002046
Jerry Yu568ec252022-07-22 21:27:34 +08002047#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002048 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2049 {
2050 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2051 psa_set_key_usage_flags( &key_attributes,
2052 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2053 }
2054#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2055
Neil Armstrong501c9322022-05-03 09:35:09 +02002056 psa_set_key_algorithm( &key_attributes, alg );
2057 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2058
2059 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2060 if( status != PSA_SUCCESS )
2061 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2062
2063 /* Allow calling psa_destroy_key() on psk remove */
2064 ssl->handshake->psk_opaque_is_internal = 1;
2065 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2066#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002067 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002069
2070 ssl->handshake->psk_len = psk_len;
2071 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2072
2073 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002074#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002075}
2076
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002077#if defined(MBEDTLS_USE_PSA_CRYPTO)
2078int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002079 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002080 const unsigned char *psk_identity,
2081 size_t psk_identity_len )
2082{
Janos Follath865b3eb2019-12-16 11:46:15 +00002083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002084
2085 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002086 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002087 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002088
Hanno Becker7390c712018-11-15 13:33:04 +00002089 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002090 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002092 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002093
2094 /* Check and set PSK Identity */
2095 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2096 psk_identity_len );
2097 if( ret != 0 )
2098 ssl_conf_remove_psk( conf );
2099
2100 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002101}
2102
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002103int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2104 mbedtls_svc_key_id_t psk )
2105{
2106 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2107 ( ssl->handshake == NULL ) )
2108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2109
2110 ssl_remove_psk( ssl );
2111 ssl->handshake->psk_opaque = psk;
2112 return( 0 );
2113}
2114#endif /* MBEDTLS_USE_PSA_CRYPTO */
2115
Jerry Yu8897c072022-08-12 13:56:53 +08002116#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002117void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2118 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2119 size_t),
2120 void *p_psk )
2121{
2122 conf->f_psk = f_psk;
2123 conf->p_psk = p_psk;
2124}
Jerry Yu8897c072022-08-12 13:56:53 +08002125#endif /* MBEDTLS_SSL_SRV_C */
2126
Ronald Cron73fe8df2022-10-05 14:31:43 +02002127#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002128
2129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002130static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2131 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002132{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002133#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002134 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002135 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002136#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002137 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002138 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002139 return( MBEDTLS_SSL_MODE_STREAM );
2140}
2141
2142#else /* MBEDTLS_USE_PSA_CRYPTO */
2143
2144static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2145 mbedtls_cipher_mode_t mode )
2146{
2147#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2148 if( mode == MBEDTLS_MODE_CBC )
2149 return( MBEDTLS_SSL_MODE_CBC );
2150#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2151
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002152#if defined(MBEDTLS_GCM_C) || \
2153 defined(MBEDTLS_CCM_C) || \
2154 defined(MBEDTLS_CHACHAPOLY_C)
2155 if( mode == MBEDTLS_MODE_GCM ||
2156 mode == MBEDTLS_MODE_CCM ||
2157 mode == MBEDTLS_MODE_CHACHAPOLY )
2158 return( MBEDTLS_SSL_MODE_AEAD );
2159#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002160
2161 return( MBEDTLS_SSL_MODE_STREAM );
2162}
Gilles Peskine301711e2022-04-26 16:57:05 +02002163#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002164
Gilles Peskinee108d982022-04-26 16:50:40 +02002165static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2166 mbedtls_ssl_mode_t base_mode,
2167 int encrypt_then_mac )
2168{
2169#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2170 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2171 base_mode == MBEDTLS_SSL_MODE_CBC )
2172 {
2173 return( MBEDTLS_SSL_MODE_CBC_ETM );
2174 }
2175#else
2176 (void) encrypt_then_mac;
2177#endif
2178 return( base_mode );
2179}
2180
Neil Armstrongab555e02022-04-04 11:07:59 +02002181mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002182 const mbedtls_ssl_transform *transform )
2183{
Gilles Peskinee108d982022-04-26 16:50:40 +02002184 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002185#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002186 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002187#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002188 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2189#endif
2190 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002191
Gilles Peskinee108d982022-04-26 16:50:40 +02002192 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002193#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002194 encrypt_then_mac = transform->encrypt_then_mac;
2195#endif
2196 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002197}
2198
Neil Armstrongab555e02022-04-04 11:07:59 +02002199mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002200#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002201 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002202#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002203 const mbedtls_ssl_ciphersuite_t *suite )
2204{
Gilles Peskinee108d982022-04-26 16:50:40 +02002205 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2206
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002207#if defined(MBEDTLS_USE_PSA_CRYPTO)
2208 psa_status_t status;
2209 psa_algorithm_t alg;
2210 psa_key_type_t type;
2211 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002212 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2213 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002214 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002215#else
2216 const mbedtls_cipher_info_t *cipher =
2217 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002218 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002219 {
2220 base_mode =
2221 mbedtls_ssl_get_base_mode(
2222 mbedtls_cipher_info_get_mode( cipher ) );
2223 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002224#endif /* MBEDTLS_USE_PSA_CRYPTO */
2225
Gilles Peskinee108d982022-04-26 16:50:40 +02002226#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2227 int encrypt_then_mac = 0;
2228#endif
2229 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002230}
2231
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002232#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002233
2234#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002235/* Serialization of TLS 1.3 sessions:
2236 *
2237 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002238 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002239 * uint64 ticket_received;
2240 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002241 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002242 * } ClientOnlyData;
2243 *
2244 * struct {
2245 * uint8 endpoint;
2246 * uint8 ciphersuite[2];
2247 * uint32 ticket_age_add;
2248 * uint8 ticket_flags;
2249 * opaque resumption_key<0..255>;
2250 * select ( endpoint ) {
2251 * case client: ClientOnlyData;
2252 * case server: uint64 start_time;
2253 * };
2254 * } serialized_session_tls13;
2255 *
2256 */
2257#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002258MBEDTLS_CHECK_RETURN_CRITICAL
2259static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2260 unsigned char *buf,
2261 size_t buf_len,
2262 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002263{
2264 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002265#if defined(MBEDTLS_SSL_CLI_C) && \
2266 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2267 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002268 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002269#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002270 size_t needed = 1 /* endpoint */
2271 + 2 /* ciphersuite */
2272 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002273 + 1 /* ticket_flags */
2274 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002275 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002276
2277 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2279 needed += session->resumption_key_len; /* resumption_key */
2280
Jerry Yu438ddd82022-07-07 06:55:50 +00002281#if defined(MBEDTLS_HAVE_TIME)
2282 needed += 8; /* start_time or ticket_received */
2283#endif
2284
2285#if defined(MBEDTLS_SSL_CLI_C)
2286 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2287 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002288#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002289 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002290 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002291#endif
2292
Jerry Yu438ddd82022-07-07 06:55:50 +00002293 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002294 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002295
2296 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002297 if( session->ticket_len > SIZE_MAX - needed )
2298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002299
Jerry Yue28d9742022-08-18 15:44:03 +08002300 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002301 }
2302#endif /* MBEDTLS_SSL_CLI_C */
2303
Jerry Yue36fdd62022-08-17 21:31:36 +08002304 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002305 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002306 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002307
2308 p[0] = session->endpoint;
2309 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2310 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2311 p[7] = session->ticket_flags;
2312
2313 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002314 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002315 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002316 memcpy( p, session->resumption_key, session->resumption_key_len );
2317 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002318
2319#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2320 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2321 {
2322 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2323 p += 8;
2324 }
2325#endif /* MBEDTLS_HAVE_TIME */
2326
2327#if defined(MBEDTLS_SSL_CLI_C)
2328 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2329 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002330#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2331 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2332 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002333 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002334 {
2335 /* save host name */
2336 memcpy( p, session->hostname, hostname_len );
2337 p += hostname_len;
2338 }
2339#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2340
Jerry Yu438ddd82022-07-07 06:55:50 +00002341#if defined(MBEDTLS_HAVE_TIME)
2342 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2343 p += 8;
2344#endif
2345 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2346 p += 4;
2347
2348 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2349 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002350
2351 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002352 {
2353 memcpy( p, session->ticket, session->ticket_len );
2354 p += session->ticket_len;
2355 }
2356 }
2357#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002358 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002359}
2360
2361MBEDTLS_CHECK_RETURN_CRITICAL
2362static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2363 const unsigned char *buf,
2364 size_t len )
2365{
2366 const unsigned char *p = buf;
2367 const unsigned char *end = buf + len;
2368
2369 if( end - p < 9 )
2370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2371 session->endpoint = p[0];
2372 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2373 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2374 session->ticket_flags = p[7];
2375
2376 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002377 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002378 p += 9;
2379
Jerry Yubc7c1a42022-07-21 22:57:37 +08002380 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2382
Jerry Yubc7c1a42022-07-21 22:57:37 +08002383 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002384 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002385 memcpy( session->resumption_key, p, session->resumption_key_len );
2386 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002387
2388#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2389 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2390 {
2391 if( end - p < 8 )
2392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2393 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2394 p += 8;
2395 }
2396#endif /* MBEDTLS_HAVE_TIME */
2397
2398#if defined(MBEDTLS_SSL_CLI_C)
2399 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2400 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002401#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2402 defined(MBEDTLS_SSL_SESSION_TICKETS)
2403 size_t hostname_len;
2404 /* load host name */
2405 if( end - p < 2 )
2406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2407 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2408 p += 2;
2409
2410 if( end - p < ( long int )hostname_len )
2411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2412 if( hostname_len > 0 )
2413 {
2414 session->hostname = mbedtls_calloc( 1, hostname_len );
2415 if( session->hostname == NULL )
2416 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2417 memcpy( session->hostname, p, hostname_len );
2418 p += hostname_len;
2419 }
2420#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2421 MBEDTLS_SSL_SESSION_TICKETS */
2422
Jerry Yu438ddd82022-07-07 06:55:50 +00002423#if defined(MBEDTLS_HAVE_TIME)
2424 if( end - p < 8 )
2425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2426 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2427 p += 8;
2428#endif
2429 if( end - p < 4 )
2430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2431 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2432 p += 4;
2433
2434 if( end - p < 2 )
2435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2436 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2437 p += 2;
2438
2439 if( end - p < ( long int )session->ticket_len )
2440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2441 if( session->ticket_len > 0 )
2442 {
2443 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2444 if( session->ticket == NULL )
2445 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2446 memcpy( session->ticket, p, session->ticket_len );
2447 p += session->ticket_len;
2448 }
2449 }
2450#endif /* MBEDTLS_SSL_CLI_C */
2451
2452 return( 0 );
2453
2454}
2455#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002456MBEDTLS_CHECK_RETURN_CRITICAL
2457static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2458 unsigned char *buf,
2459 size_t buf_len,
2460 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002461{
2462 ((void) session);
2463 ((void) buf);
2464 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002465 *olen = 0;
2466 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002467}
Jerry Yu438ddd82022-07-07 06:55:50 +00002468
2469static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2470 unsigned char *buf,
2471 size_t buf_len )
2472{
2473 ((void) session);
2474 ((void) buf);
2475 ((void) buf_len);
2476 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2477}
2478#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002479#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2480
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002481psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002482 size_t taglen,
2483 psa_algorithm_t *alg,
2484 psa_key_type_t *key_type,
2485 size_t *key_size )
2486{
2487 switch ( mbedtls_cipher_type )
2488 {
2489 case MBEDTLS_CIPHER_AES_128_CBC:
2490 *alg = PSA_ALG_CBC_NO_PADDING;
2491 *key_type = PSA_KEY_TYPE_AES;
2492 *key_size = 128;
2493 break;
2494 case MBEDTLS_CIPHER_AES_128_CCM:
2495 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2496 *key_type = PSA_KEY_TYPE_AES;
2497 *key_size = 128;
2498 break;
2499 case MBEDTLS_CIPHER_AES_128_GCM:
2500 *alg = PSA_ALG_GCM;
2501 *key_type = PSA_KEY_TYPE_AES;
2502 *key_size = 128;
2503 break;
2504 case MBEDTLS_CIPHER_AES_192_CCM:
2505 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2506 *key_type = PSA_KEY_TYPE_AES;
2507 *key_size = 192;
2508 break;
2509 case MBEDTLS_CIPHER_AES_192_GCM:
2510 *alg = PSA_ALG_GCM;
2511 *key_type = PSA_KEY_TYPE_AES;
2512 *key_size = 192;
2513 break;
2514 case MBEDTLS_CIPHER_AES_256_CBC:
2515 *alg = PSA_ALG_CBC_NO_PADDING;
2516 *key_type = PSA_KEY_TYPE_AES;
2517 *key_size = 256;
2518 break;
2519 case MBEDTLS_CIPHER_AES_256_CCM:
2520 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2521 *key_type = PSA_KEY_TYPE_AES;
2522 *key_size = 256;
2523 break;
2524 case MBEDTLS_CIPHER_AES_256_GCM:
2525 *alg = PSA_ALG_GCM;
2526 *key_type = PSA_KEY_TYPE_AES;
2527 *key_size = 256;
2528 break;
2529 case MBEDTLS_CIPHER_ARIA_128_CBC:
2530 *alg = PSA_ALG_CBC_NO_PADDING;
2531 *key_type = PSA_KEY_TYPE_ARIA;
2532 *key_size = 128;
2533 break;
2534 case MBEDTLS_CIPHER_ARIA_128_CCM:
2535 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2536 *key_type = PSA_KEY_TYPE_ARIA;
2537 *key_size = 128;
2538 break;
2539 case MBEDTLS_CIPHER_ARIA_128_GCM:
2540 *alg = PSA_ALG_GCM;
2541 *key_type = PSA_KEY_TYPE_ARIA;
2542 *key_size = 128;
2543 break;
2544 case MBEDTLS_CIPHER_ARIA_192_CCM:
2545 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2546 *key_type = PSA_KEY_TYPE_ARIA;
2547 *key_size = 192;
2548 break;
2549 case MBEDTLS_CIPHER_ARIA_192_GCM:
2550 *alg = PSA_ALG_GCM;
2551 *key_type = PSA_KEY_TYPE_ARIA;
2552 *key_size = 192;
2553 break;
2554 case MBEDTLS_CIPHER_ARIA_256_CBC:
2555 *alg = PSA_ALG_CBC_NO_PADDING;
2556 *key_type = PSA_KEY_TYPE_ARIA;
2557 *key_size = 256;
2558 break;
2559 case MBEDTLS_CIPHER_ARIA_256_CCM:
2560 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2561 *key_type = PSA_KEY_TYPE_ARIA;
2562 *key_size = 256;
2563 break;
2564 case MBEDTLS_CIPHER_ARIA_256_GCM:
2565 *alg = PSA_ALG_GCM;
2566 *key_type = PSA_KEY_TYPE_ARIA;
2567 *key_size = 256;
2568 break;
2569 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2570 *alg = PSA_ALG_CBC_NO_PADDING;
2571 *key_type = PSA_KEY_TYPE_CAMELLIA;
2572 *key_size = 128;
2573 break;
2574 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2575 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2576 *key_type = PSA_KEY_TYPE_CAMELLIA;
2577 *key_size = 128;
2578 break;
2579 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2580 *alg = PSA_ALG_GCM;
2581 *key_type = PSA_KEY_TYPE_CAMELLIA;
2582 *key_size = 128;
2583 break;
2584 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2585 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2586 *key_type = PSA_KEY_TYPE_CAMELLIA;
2587 *key_size = 192;
2588 break;
2589 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2590 *alg = PSA_ALG_GCM;
2591 *key_type = PSA_KEY_TYPE_CAMELLIA;
2592 *key_size = 192;
2593 break;
2594 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2595 *alg = PSA_ALG_CBC_NO_PADDING;
2596 *key_type = PSA_KEY_TYPE_CAMELLIA;
2597 *key_size = 256;
2598 break;
2599 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2600 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2601 *key_type = PSA_KEY_TYPE_CAMELLIA;
2602 *key_size = 256;
2603 break;
2604 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2605 *alg = PSA_ALG_GCM;
2606 *key_type = PSA_KEY_TYPE_CAMELLIA;
2607 *key_size = 256;
2608 break;
2609 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2610 *alg = PSA_ALG_CHACHA20_POLY1305;
2611 *key_type = PSA_KEY_TYPE_CHACHA20;
2612 *key_size = 256;
2613 break;
2614 case MBEDTLS_CIPHER_NULL:
2615 *alg = MBEDTLS_SSL_NULL_CIPHER;
2616 *key_type = 0;
2617 *key_size = 0;
2618 break;
2619 default:
2620 return PSA_ERROR_NOT_SUPPORTED;
2621 }
2622
2623 return PSA_SUCCESS;
2624}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002625#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002626
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002627#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002628int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2629 const unsigned char *dhm_P, size_t P_len,
2630 const unsigned char *dhm_G, size_t G_len )
2631{
Janos Follath865b3eb2019-12-16 11:46:15 +00002632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002633
Glenn Strausscee11292021-12-20 01:43:17 -05002634 mbedtls_mpi_free( &conf->dhm_P );
2635 mbedtls_mpi_free( &conf->dhm_G );
2636
Hanno Beckera90658f2017-10-04 15:29:08 +01002637 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2638 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2639 {
2640 mbedtls_mpi_free( &conf->dhm_P );
2641 mbedtls_mpi_free( &conf->dhm_G );
2642 return( ret );
2643 }
2644
2645 return( 0 );
2646}
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002648int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002649{
Janos Follath865b3eb2019-12-16 11:46:15 +00002650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002651
Glenn Strausscee11292021-12-20 01:43:17 -05002652 mbedtls_mpi_free( &conf->dhm_P );
2653 mbedtls_mpi_free( &conf->dhm_G );
2654
Gilles Peskinee5702482021-06-11 21:59:08 +02002655 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2656 &conf->dhm_P ) ) != 0 ||
2657 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2658 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002659 {
2660 mbedtls_mpi_free( &conf->dhm_P );
2661 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002662 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002663 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002664
2665 return( 0 );
2666}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002667#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002668
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002669#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2670/*
2671 * Set the minimum length for Diffie-Hellman parameters
2672 */
2673void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2674 unsigned int bitlen )
2675{
2676 conf->dhm_min_bitlen = bitlen;
2677}
2678#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2679
Ronald Crone68ab4f2022-10-05 12:46:29 +02002680#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002681#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002682/*
2683 * Set allowed/preferred hashes for handshake signatures
2684 */
2685void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2686 const int *hashes )
2687{
2688 conf->sig_hashes = hashes;
2689}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002690#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002691
Jerry Yuf017ee42022-01-12 15:49:48 +08002692/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002693void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2694 const uint16_t* sig_algs )
2695{
Jerry Yuf017ee42022-01-12 15:49:48 +08002696#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2697 conf->sig_hashes = NULL;
2698#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2699 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002700}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002701#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002702
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002703#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002704#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002705/*
2706 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002707 *
2708 * mbedtls_ssl_setup() takes the provided list
2709 * and translates it to a list of IANA TLS group identifiers,
2710 * stored in ssl->handshake->group_list.
2711 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002712 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002713void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002714 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002715{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002716 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002717 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002718}
Brett Warrene0edc842021-08-17 09:53:13 +01002719#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002720#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002721
Brett Warrene0edc842021-08-17 09:53:13 +01002722/*
2723 * Set the allowed groups
2724 */
2725void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2726 const uint16_t *group_list )
2727{
2728#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2729 conf->curve_list = NULL;
2730#endif
2731 conf->group_list = group_list;
2732}
2733
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002734#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002736{
Hanno Becker947194e2017-04-07 13:25:49 +01002737 /* Initialize to suppress unnecessary compiler warning */
2738 size_t hostname_len = 0;
2739
2740 /* Check if new hostname is valid before
2741 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002742 if( hostname != NULL )
2743 {
2744 hostname_len = strlen( hostname );
2745
2746 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2748 }
2749
2750 /* Now it's clear that we will overwrite the old hostname,
2751 * so we can free it safely */
2752
2753 if( ssl->hostname != NULL )
2754 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002755 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002756 mbedtls_free( ssl->hostname );
2757 }
2758
2759 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002760
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002762 {
2763 ssl->hostname = NULL;
2764 }
2765 else
2766 {
2767 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002768 if( ssl->hostname == NULL )
2769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002770
Hanno Becker947194e2017-04-07 13:25:49 +01002771 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002772
Hanno Becker947194e2017-04-07 13:25:49 +01002773 ssl->hostname[hostname_len] = '\0';
2774 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
2776 return( 0 );
2777}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002778#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002780#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002781void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002783 const unsigned char *, size_t),
2784 void *p_sni )
2785{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002786 conf->f_sni = f_sni;
2787 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002792int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002793{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002794 size_t cur_len, tot_len;
2795 const char **p;
2796
2797 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002798 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2799 * MUST NOT be truncated."
2800 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002801 */
2802 tot_len = 0;
2803 for( p = protos; *p != NULL; p++ )
2804 {
2805 cur_len = strlen( *p );
2806 tot_len += cur_len;
2807
Ronald Cron8216dd32020-04-23 16:41:44 +02002808 if( ( cur_len == 0 ) ||
2809 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2810 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002812 }
2813
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002814 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002815
2816 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002817}
2818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002820{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002821 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002824
Johan Pascalb62bb512015-12-03 21:56:45 +01002825#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002826void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2827 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002828{
2829 conf->dtls_srtp_mki_support = support_mki_value;
2830}
2831
Ron Eldoref72faf2018-07-12 11:54:20 +03002832int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2833 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002834 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002835{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002836 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002837 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002839 }
Ron Eldor591f1622018-01-22 12:30:04 +02002840
2841 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002842 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002843 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002844 }
Ron Eldor591f1622018-01-22 12:30:04 +02002845
2846 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2847 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002848 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002849}
2850
Ron Eldoref72faf2018-07-12 11:54:20 +03002851int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002852 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002853{
Johan Pascal253d0262020-09-22 13:04:45 +02002854 const mbedtls_ssl_srtp_profile *p;
2855 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002856
Johan Pascal253d0262020-09-22 13:04:45 +02002857 /* check the profiles list: all entry must be valid,
2858 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002859 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2860 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2861 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002862 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002863 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002864 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002865 list_size++;
2866 }
2867 else
2868 {
2869 /* unsupported value, stop parsing and set the size to an error value */
2870 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002871 }
2872 }
2873
Johan Pascal5ef72d22020-10-28 17:05:47 +01002874 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002875 {
Johan Pascal253d0262020-09-22 13:04:45 +02002876 conf->dtls_srtp_profile_list = NULL;
2877 conf->dtls_srtp_profile_list_len = 0;
2878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2879 }
2880
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002881 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002882 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002883
2884 return( 0 );
2885}
2886
Johan Pascal5ef72d22020-10-28 17:05:47 +01002887void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2888 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002889{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002890 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2891 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002892 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002893 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002894 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002895 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002896 else
2897 {
2898 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002899 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2900 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002901 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002902}
Johan Pascalb62bb512015-12-03 21:56:45 +01002903#endif /* MBEDTLS_SSL_DTLS_SRTP */
2904
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302905#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002906void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002907{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002908 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002909}
2910
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002911void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002912{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002913 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002914}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302915#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002916
Janos Follath088ce432017-04-10 12:42:31 +01002917#if defined(MBEDTLS_SSL_SRV_C)
2918void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2919 char cert_req_ca_list )
2920{
2921 conf->cert_req_ca_list = cert_req_ca_list;
2922}
2923#endif
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002926void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002928 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002929}
2930#endif
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002933void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002934{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002935 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002936}
2937#endif
2938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002940int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002943 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002946 }
2947
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002948 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002949
2950 return( 0 );
2951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002953
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002954void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002955{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002956 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002957}
2958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002960void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002961{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002962 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002963}
2964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002965void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002967 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002968}
2969
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002970void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002971 const unsigned char period[8] )
2972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002973 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002978#if defined(MBEDTLS_SSL_CLI_C)
2979void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002980{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002981 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002982}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002983#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002984
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002985#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002986
Jerry Yud0766ec2022-09-22 10:46:57 +08002987#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002988void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2989 uint16_t num_tickets )
2990{
Jerry Yud0766ec2022-09-22 10:46:57 +08002991 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002992}
2993#endif
2994
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002995void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2996 mbedtls_ssl_ticket_write_t *f_ticket_write,
2997 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2998 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002999{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003000 conf->f_ticket_write = f_ticket_write;
3001 conf->f_ticket_parse = f_ticket_parse;
3002 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003003}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003006
Hanno Becker7e6c1782021-06-08 09:24:55 +01003007void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
3008 mbedtls_ssl_export_keys_t *f_export_keys,
3009 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003010{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003011 ssl->f_export_keys = f_export_keys;
3012 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003013}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003014
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003015#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003016void mbedtls_ssl_conf_async_private_cb(
3017 mbedtls_ssl_config *conf,
3018 mbedtls_ssl_async_sign_t *f_async_sign,
3019 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3020 mbedtls_ssl_async_resume_t *f_async_resume,
3021 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003022 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003023{
3024 conf->f_async_sign_start = f_async_sign;
3025 conf->f_async_decrypt_start = f_async_decrypt;
3026 conf->f_async_resume = f_async_resume;
3027 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003028 conf->p_async_config_data = async_config_data;
3029}
3030
Gilles Peskine8f97af72018-04-26 11:46:10 +02003031void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3032{
3033 return( conf->p_async_config_data );
3034}
3035
Gilles Peskine1febfef2018-04-30 11:54:39 +02003036void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003037{
3038 if( ssl->handshake == NULL )
3039 return( NULL );
3040 else
3041 return( ssl->handshake->user_async_ctx );
3042}
3043
Gilles Peskine1febfef2018-04-30 11:54:39 +02003044void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003045 void *ctx )
3046{
3047 if( ssl->handshake != NULL )
3048 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003049}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003050#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003051
Paul Bakker5121ce52009-01-03 21:22:43 +00003052/*
3053 * SSL get accessors
3054 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003055uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003057 if( ssl->session != NULL )
3058 return( ssl->session->verify_result );
3059
3060 if( ssl->session_negotiate != NULL )
3061 return( ssl->session_negotiate->verify_result );
3062
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003063 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003064}
3065
Glenn Strauss8f526902022-01-13 00:04:49 -05003066int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3067{
3068 if( ssl == NULL || ssl->session == NULL )
3069 return( 0 );
3070
3071 return( ssl->session->ciphersuite );
3072}
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003075{
Paul Bakker926c8e42013-03-06 10:23:34 +01003076 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003077 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003080}
3081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003086 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003087 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003088 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003089 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003090 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003091 default:
3092 return( "unknown (DTLS)" );
3093 }
3094 }
3095#endif
3096
Glenn Strauss60bfe602022-03-14 19:04:24 -04003097 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003098 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003099 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003101 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003102 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003103 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003104 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003105 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003106}
3107
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003108#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003109size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3110{
David Horstmann95d516f2021-05-04 18:36:56 +01003111 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003112 size_t read_mfl;
3113
3114 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3116 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3117 {
3118 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3119 }
3120
3121 /* Check if a smaller max length was negotiated */
3122 if( ssl->session_out != NULL )
3123 {
3124 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3125 if( read_mfl < max_len )
3126 {
3127 max_len = read_mfl;
3128 }
3129 }
3130
3131 // During a handshake, use the value being negotiated
3132 if( ssl->session_negotiate != NULL )
3133 {
3134 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3135 if( read_mfl < max_len )
3136 {
3137 max_len = read_mfl;
3138 }
3139 }
3140
3141 return( max_len );
3142}
3143
3144size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003145{
3146 size_t max_len;
3147
3148 /*
3149 * Assume mfl_code is correct since it was checked when set
3150 */
Angus Grattond8213d02016-05-25 20:56:48 +10003151 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003152
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003153 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003154 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003155 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003156 {
Angus Grattond8213d02016-05-25 20:56:48 +10003157 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003158 }
3159
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003160 /* During a handshake, use the value being negotiated */
3161 if( ssl->session_negotiate != NULL &&
3162 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3163 {
3164 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3165 }
3166
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003167 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003168}
3169#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3170
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003172size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003173{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003174 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3176 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3177 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3178 return ( 0 );
3179
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003180 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3181 return( ssl->mtu );
3182
3183 if( ssl->mtu == 0 )
3184 return( ssl->handshake->mtu );
3185
3186 return( ssl->mtu < ssl->handshake->mtu ?
3187 ssl->mtu : ssl->handshake->mtu );
3188}
3189#endif /* MBEDTLS_SSL_PROTO_DTLS */
3190
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003191int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3192{
3193 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3194
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003195#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3196 !defined(MBEDTLS_SSL_PROTO_DTLS)
3197 (void) ssl;
3198#endif
3199
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003201 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003202
3203 if( max_len > mfl )
3204 max_len = mfl;
3205#endif
3206
3207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003208 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003209 {
Hanno Becker89490712020-02-05 10:50:12 +00003210 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003211 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3212 const size_t overhead = (size_t) ret;
3213
3214 if( ret < 0 )
3215 return( ret );
3216
3217 if( mtu <= overhead )
3218 {
3219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3220 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3221 }
3222
3223 if( max_len > mtu - overhead )
3224 max_len = mtu - overhead;
3225 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003226#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003227
Hanno Becker0defedb2018-08-10 12:35:02 +01003228#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3229 !defined(MBEDTLS_SSL_PROTO_DTLS)
3230 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003231#endif
3232
3233 return( (int) max_len );
3234}
3235
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003236int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3237{
3238 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3239
3240#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3241 (void) ssl;
3242#endif
3243
3244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3245 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3246
3247 if( max_len > mfl )
3248 max_len = mfl;
3249#endif
3250
3251 return( (int) max_len );
3252}
3253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_X509_CRT_PARSE_C)
3255const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003256{
3257 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003258 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003259
Hanno Beckere6824572019-02-07 13:18:46 +00003260#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003261 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003262#else
3263 return( NULL );
3264#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003269int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3270 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003271{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003272 int ret;
3273
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003274 if( ssl == NULL ||
3275 dst == NULL ||
3276 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003277 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003280 }
3281
Hanno Beckere810bbc2021-05-14 16:01:05 +01003282 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3283 * idempotent: Each session can only be exported once.
3284 *
3285 * (This is in preparation for TLS 1.3 support where we will
3286 * need the ability to export multiple sessions (aka tickets),
3287 * which will be achieved by calling mbedtls_ssl_get_session()
3288 * multiple times until it fails.)
3289 *
3290 * Check whether we have already exported the current session,
3291 * and fail if so.
3292 */
3293 if( ssl->session->exported == 1 )
3294 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3295
3296 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3297 if( ret != 0 )
3298 return( ret );
3299
3300 /* Remember that we've exported the session. */
3301 ssl->session->exported = 1;
3302 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003305
Paul Bakker5121ce52009-01-03 21:22:43 +00003306/*
Hanno Beckera835da52019-05-16 12:39:07 +01003307 * Define ticket header determining Mbed TLS version
3308 * and structure of the ticket.
3309 */
3310
Hanno Becker94ef3b32019-05-16 12:50:45 +01003311/*
Hanno Becker50b59662019-05-28 14:30:45 +01003312 * Define bitflag determining compile-time settings influencing
3313 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003314 */
3315
Hanno Becker50b59662019-05-28 14:30:45 +01003316#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003317#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003318#else
Hanno Becker3e088662019-05-29 11:10:18 +01003319#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003320#endif /* MBEDTLS_HAVE_TIME */
3321
3322#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003323#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003324#else
Hanno Becker3e088662019-05-29 11:10:18 +01003325#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003326#endif /* MBEDTLS_X509_CRT_PARSE_C */
3327
3328#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003329#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003330#else
Hanno Becker3e088662019-05-29 11:10:18 +01003331#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003332#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3333
3334#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003335#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003336#else
Hanno Becker3e088662019-05-29 11:10:18 +01003337#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003338#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3339
Hanno Becker94ef3b32019-05-16 12:50:45 +01003340#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003341#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003342#else
Hanno Becker3e088662019-05-29 11:10:18 +01003343#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003344#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3345
Hanno Becker94ef3b32019-05-16 12:50:45 +01003346#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3347#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3348#else
3349#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3350#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3351
Hanno Becker3e088662019-05-29 11:10:18 +01003352#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3353#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3354#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3355#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003356#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3357#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003358
Hanno Becker50b59662019-05-28 14:30:45 +01003359#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003360 ( (uint16_t) ( \
3361 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3362 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3363 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3364 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003365 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003366 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003367
Hanno Beckerf8787072019-05-16 12:41:07 +01003368static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003369 MBEDTLS_VERSION_MAJOR,
3370 MBEDTLS_VERSION_MINOR,
3371 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003372 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3373 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003374};
Hanno Beckera835da52019-05-16 12:39:07 +01003375
3376/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003377 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003378 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003379 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003380 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003381 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003382 * opaque mbedtls_version[3]; // library version: major, minor, patch
3383 * opaque session_format[2]; // library-version specific 16-bit field
3384 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003385 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003386 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003387 * Note: When updating the format, remember to keep
3388 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003389 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003390 * // In this version, `session_format` determines
3391 * // the setting of those compile-time
3392 * // configuration options which influence
3393 * // the structure of mbedtls_ssl_session.
3394 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003395 * uint8_t minor_ver; // Protocol minor version. Possible values:
3396 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003397 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003398 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003399 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003400 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003401 * serialized_session_tls12 data;
3402 *
3403 * };
3404 *
3405 * } serialized_session;
3406 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003407 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003408
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003409MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003410static int ssl_session_save( const mbedtls_ssl_session *session,
3411 unsigned char omit_header,
3412 unsigned char *buf,
3413 size_t buf_len,
3414 size_t *olen )
3415{
3416 unsigned char *p = buf;
3417 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003418 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003419#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3420 size_t out_len;
3421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3422#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003423 if( session == NULL )
3424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3425
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003426 if( !omit_header )
3427 {
3428 /*
3429 * Add Mbed TLS version identifier
3430 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003431 used += sizeof( ssl_serialized_session_header );
3432
3433 if( used <= buf_len )
3434 {
3435 memcpy( p, ssl_serialized_session_header,
3436 sizeof( ssl_serialized_session_header ) );
3437 p += sizeof( ssl_serialized_session_header );
3438 }
3439 }
3440
3441 /*
3442 * TLS version identifier
3443 */
3444 used += 1;
3445 if( used <= buf_len )
3446 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003447 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003448 }
3449
3450 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003451 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003452 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003453 {
3454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003455 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003456 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003457 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3459
Jerry Yu251a12e2022-07-13 15:15:48 +08003460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3461 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003462 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3463 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003464 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003465 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003466 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003467#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3468
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003469 default:
3470 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3471 }
3472
3473 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003474 if( used > buf_len )
3475 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003476
3477 return( 0 );
3478}
3479
3480/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003481 * Public wrapper for ssl_session_save()
3482 */
3483int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3484 unsigned char *buf,
3485 size_t buf_len,
3486 size_t *olen )
3487{
3488 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3489}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003490
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003491/*
3492 * Deserialize session, see mbedtls_ssl_session_save() for format.
3493 *
3494 * This internal version is wrapped by a public function that cleans up in
3495 * case of error, and has an extra option omit_header.
3496 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003497MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003498static int ssl_session_load( mbedtls_ssl_session *session,
3499 unsigned char omit_header,
3500 const unsigned char *buf,
3501 size_t len )
3502{
3503 const unsigned char *p = buf;
3504 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003505 size_t remaining_len;
3506
3507
3508 if( session == NULL )
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003510
3511 if( !omit_header )
3512 {
3513 /*
3514 * Check Mbed TLS version identifier
3515 */
3516
3517 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3519
3520 if( memcmp( p, ssl_serialized_session_header,
3521 sizeof( ssl_serialized_session_header ) ) != 0 )
3522 {
3523 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3524 }
3525 p += sizeof( ssl_serialized_session_header );
3526 }
3527
3528 /*
3529 * TLS version identifier
3530 */
3531 if( 1 > (size_t)( end - p ) )
3532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003533 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003534
3535 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003536 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003537 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003538 {
3539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003540 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003541 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3543
Jerry Yu438ddd82022-07-07 06:55:50 +00003544#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3545 case MBEDTLS_SSL_VERSION_TLS1_3:
3546 return( ssl_tls13_session_load( session, p, remaining_len ) );
3547#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3548
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003549 default:
3550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3551 }
3552}
3553
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003554/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003555 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003556 */
3557int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3558 const unsigned char *buf,
3559 size_t len )
3560{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003561 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003562
3563 if( ret != 0 )
3564 mbedtls_ssl_session_free( session );
3565
3566 return( ret );
3567}
3568
3569/*
Paul Bakker1961b702013-01-25 14:49:24 +01003570 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003572MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003573static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3574{
3575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3576
Ronald Cron66dbf912022-02-02 15:33:46 +01003577 /*
3578 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003579 * that were written into the output buffer by the previous handshake step,
3580 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003581 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3582 * We proceed to the next handshake step only when all data from the
3583 * previous one have been sent to the peer, thus we make sure that this is
3584 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3585 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3586 * we have to wait before to go ahead.
3587 * In the case of TLS 1.3, handshake step handlers do not send data to the
3588 * peer. Data are only sent here and through
3589 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003590 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003591 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003592 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3593 return( ret );
3594
3595#if defined(MBEDTLS_SSL_PROTO_DTLS)
3596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3597 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3598 {
3599 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3600 return( ret );
3601 }
3602#endif /* MBEDTLS_SSL_PROTO_DTLS */
3603
3604 return( ret );
3605}
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608{
Hanno Becker41934dd2021-08-07 19:13:43 +01003609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Hanno Becker41934dd2021-08-07 19:13:43 +01003611 if( ssl == NULL ||
3612 ssl->conf == NULL ||
3613 ssl->handshake == NULL ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003614 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003615 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003617 }
3618
3619 ret = ssl_prepare_handshake_step( ssl );
3620 if( ret != 0 )
3621 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003622
Jerry Yue7047812021-09-13 19:26:39 +08003623 ret = mbedtls_ssl_handle_pending_alert( ssl );
3624 if( ret != 0 )
3625 goto cleanup;
3626
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003627 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3628 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3629 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003633 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3635 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003636
Ronald Cron9f0fba32022-02-10 16:45:15 +01003637 switch( ssl->state )
3638 {
3639 case MBEDTLS_SSL_HELLO_REQUEST:
3640 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003641 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003642 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003643
Ronald Cron9f0fba32022-02-10 16:45:15 +01003644 case MBEDTLS_SSL_CLIENT_HELLO:
3645 ret = mbedtls_ssl_write_client_hello( ssl );
3646 break;
3647
3648 default:
3649#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003650 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003651 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3652 else
3653 ret = mbedtls_ssl_handshake_client_step( ssl );
3654#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3655 ret = mbedtls_ssl_handshake_client_step( ssl );
3656#else
3657 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3658#endif
3659 }
Jerry Yub9930e72021-08-06 17:11:51 +08003660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003661#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003663 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003664 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003665#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003666 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003667 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003668#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003669
3670#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3671 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3672 ret = mbedtls_ssl_handshake_server_step( ssl );
3673#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003675#endif
3676
Jerry Yue7047812021-09-13 19:26:39 +08003677 if( ret != 0 )
3678 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003679 /* handshake_step return error. And it is same
3680 * with alert_reason.
3681 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003682 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003683 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003684 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003685 goto cleanup;
3686 }
3687 }
3688
3689cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003690 return( ret );
3691}
3692
3693/*
3694 * Perform the SSL handshake
3695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003697{
3698 int ret = 0;
3699
Hanno Beckera817ea42020-10-20 15:20:23 +01003700 /* Sanity checks */
3701
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003702 if( ssl == NULL || ssl->conf == NULL )
3703 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3704
Hanno Beckera817ea42020-10-20 15:20:23 +01003705#if defined(MBEDTLS_SSL_PROTO_DTLS)
3706 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3707 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3710 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3711 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3712 }
3713#endif /* MBEDTLS_SSL_PROTO_DTLS */
3714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003716
Hanno Beckera817ea42020-10-20 15:20:23 +01003717 /* Main handshake loop */
Paul Elliott27b0d942022-03-18 21:55:32 +00003718 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003721
3722 if( ret != 0 )
3723 break;
3724 }
3725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
3728 return( ret );
3729}
3730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_SSL_RENEGOTIATION)
3732#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003733/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003734 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003735 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003736MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003738{
Janos Follath865b3eb2019-12-16 11:46:15 +00003739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003742
3743 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3745 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003746
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003747 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003748 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003750 return( ret );
3751 }
3752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003754
3755 return( 0 );
3756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003758
3759/*
3760 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 * - any side: calling mbedtls_ssl_renegotiate(),
3762 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3763 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003764 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003765 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003767 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003768int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003769{
Janos Follath865b3eb2019-12-16 11:46:15 +00003770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003773
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003774 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3775 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003776
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003777 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3778 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003782 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003783 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003784 ssl->handshake->out_msg_seq = 1;
3785 else
3786 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003787 }
3788#endif
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3791 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003796 return( ret );
3797 }
3798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003800
3801 return( 0 );
3802}
3803
3804/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003805 * Renegotiate current connection on client,
3806 * or request renegotiation on server
3807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003809{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003811
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003812 if( ssl == NULL || ssl->conf == NULL )
3813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003816 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003817 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003818 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003819 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003823
3824 /* Did we already try/start sending HelloRequest? */
3825 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003827
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003828 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003833 /*
3834 * On client, either start the renegotiation process or,
3835 * if already in progress, continue the handshake
3836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003838 {
Paul Elliott27b0d942022-03-18 21:55:32 +00003839 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003841
Hanno Becker40cdaa12020-02-05 10:48:27 +00003842 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003843 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003845 return( ret );
3846 }
3847 }
3848 else
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003853 return( ret );
3854 }
3855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003857
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003858 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861
Gilles Peskine9b562d52018-04-25 20:32:43 +02003862void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003863{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003864 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3865
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003866 if( handshake == NULL )
3867 return;
3868
Brett Warrene0edc842021-08-17 09:53:13 +01003869#if defined(MBEDTLS_ECP_C)
3870#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3871 if ( ssl->handshake->group_list_heap_allocated )
3872 mbedtls_free( (void*) handshake->group_list );
3873 handshake->group_list = NULL;
3874#endif /* MBEDTLS_DEPRECATED_REMOVED */
3875#endif /* MBEDTLS_ECP_C */
3876
Ronald Crone68ab4f2022-10-05 12:46:29 +02003877#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003878#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3879 if ( ssl->handshake->sig_algs_heap_allocated )
3880 mbedtls_free( (void*) handshake->sig_algs );
3881 handshake->sig_algs = NULL;
3882#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003883#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3884 if( ssl->handshake->certificate_request_context )
3885 {
3886 mbedtls_free( (void*) handshake->certificate_request_context );
3887 }
3888#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003889#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003890
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003891#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3892 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3893 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003894 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003895 handshake->async_in_progress = 0;
3896 }
3897#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3898
Andrzej Kurek25f27152022-08-17 16:09:31 -04003899#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003900#if defined(MBEDTLS_USE_PSA_CRYPTO)
3901 psa_hash_abort( &handshake->fin_sha256_psa );
3902#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003903 mbedtls_sha256_free( &handshake->fin_sha256 );
3904#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003905#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003906#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003907#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003908 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003909#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003910 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003911#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003912#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914#if defined(MBEDTLS_DHM_C)
3915 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003916#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003917#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003919#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003920#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003921 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003922#if defined(MBEDTLS_SSL_CLI_C)
3923 mbedtls_free( handshake->ecjpake_cache );
3924 handshake->ecjpake_cache = NULL;
3925 handshake->ecjpake_cache_len = 0;
3926#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003927#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003928
Janos Follath4ae5c292016-02-10 11:27:43 +00003929#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3930 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003931 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003933#endif
3934
Ronald Cron73fe8df2022-10-05 14:31:43 +02003935#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003936#if defined(MBEDTLS_USE_PSA_CRYPTO)
3937 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3938 {
3939 /* The maintenance of the external PSK key slot is the
3940 * user's responsibility. */
3941 if( ssl->handshake->psk_opaque_is_internal )
3942 {
3943 psa_destroy_key( ssl->handshake->psk_opaque );
3944 ssl->handshake->psk_opaque_is_internal = 0;
3945 }
3946 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3947 }
Neil Armstronge952a302022-05-03 10:22:14 +02003948#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003949 if( handshake->psk != NULL )
3950 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003951 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003952 mbedtls_free( handshake->psk );
3953 }
Neil Armstronge952a302022-05-03 10:22:14 +02003954#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003955#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3958 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003959 /*
3960 * Free only the linked list wrapper, not the keys themselves
3961 * since the belong to the SNI callback
3962 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003963 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003965
Gilles Peskineeccd8882020-03-10 12:19:08 +01003966#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003967 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003968 if( handshake->ecrs_peer_cert != NULL )
3969 {
3970 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3971 mbedtls_free( handshake->ecrs_peer_cert );
3972 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003973#endif
3974
Hanno Becker75173122019-02-06 16:18:31 +00003975#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3976 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3977 mbedtls_pk_free( &handshake->peer_pubkey );
3978#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3979
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003980#if defined(MBEDTLS_SSL_CLI_C) && \
3981 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3982 mbedtls_free( handshake->cookie );
3983#endif /* MBEDTLS_SSL_CLI_C &&
3984 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003985
3986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003987 mbedtls_ssl_flight_free( handshake->flight );
3988 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003989#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003990
Ronald Cronf12b81d2022-03-15 10:42:41 +01003991#if defined(MBEDTLS_ECDH_C) && \
3992 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003993 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003994 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003995#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3996
Ronald Cron6f135e12021-12-08 16:57:54 +01003997#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003998 mbedtls_ssl_transform_free( handshake->transform_handshake );
3999 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08004000 mbedtls_free( handshake->transform_earlydata );
4001 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01004002#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004003
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004004
4005#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4006 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4007 * processes datagrams and the fact that a datagram is allowed to have
4008 * several records in it, it is possible that the I/O buffers are not
4009 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004010 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4011 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004012#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004013
Jerry Yuba9c7272021-10-30 11:54:10 +08004014 /* mbedtls_platform_zeroize MUST be last one in this function */
4015 mbedtls_platform_zeroize( handshake,
4016 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004017}
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004020{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004021 if( session == NULL )
4022 return;
4023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004025 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004026#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004027
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004028#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004029#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4030 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004031 mbedtls_free( session->hostname );
4032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004034#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004035
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004036 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004037}
4038
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004039#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004040
4041#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4042#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4043#else
4044#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4045#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4046
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004047#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004048
4049#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4050#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4051#else
4052#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4053#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4054
4055#if defined(MBEDTLS_SSL_ALPN)
4056#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4057#else
4058#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4059#endif /* MBEDTLS_SSL_ALPN */
4060
4061#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4062#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4063#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4064#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4065
4066#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4067 ( (uint32_t) ( \
4068 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4069 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4070 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4071 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4072 0u ) )
4073
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004074static unsigned char ssl_serialized_context_header[] = {
4075 MBEDTLS_VERSION_MAJOR,
4076 MBEDTLS_VERSION_MINOR,
4077 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004078 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4079 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4080 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4081 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4082 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004083};
4084
Paul Bakker5121ce52009-01-03 21:22:43 +00004085/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004086 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004087 *
4088 * The format of the serialized data is:
4089 * (in the presentation language of TLS, RFC 8446 section 3)
4090 *
4091 * // header
4092 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004093 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004094 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004095 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004096 * Note: When updating the format, remember to keep these
4097 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004098 *
4099 * // session sub-structure
4100 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4101 * // transform sub-structure
4102 * uint8 random[64]; // ServerHello.random+ClientHello.random
4103 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4104 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4105 * // fields from ssl_context
4106 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4107 * uint64 in_window_top; // DTLS: last validated record seq_num
4108 * uint64 in_window; // DTLS: bitmask for replay protection
4109 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4110 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4111 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4112 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4113 *
4114 * Note that many fields of the ssl_context or sub-structures are not
4115 * serialized, as they fall in one of the following categories:
4116 *
4117 * 1. forced value (eg in_left must be 0)
4118 * 2. pointer to dynamically-allocated memory (eg session, transform)
4119 * 3. value can be re-derived from other data (eg session keys from MS)
4120 * 4. value was temporary (eg content of input buffer)
4121 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004122 */
4123int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4124 unsigned char *buf,
4125 size_t buf_len,
4126 size_t *olen )
4127{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004128 unsigned char *p = buf;
4129 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004130 size_t session_len;
4131 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004132
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004133 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004134 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4135 * this function's documentation.
4136 *
4137 * These are due to assumptions/limitations in the implementation. Some of
4138 * them are likely to stay (no handshake in progress) some might go away
4139 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004140 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004141 /* The initial handshake must be over */
Paul Elliott27b0d942022-03-18 21:55:32 +00004142 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004143 {
4144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004146 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004147 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004148 {
4149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004151 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004152 /* Double-check that sub-structures are indeed ready */
4153 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004157 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004158 /* There must be no pending incoming or outgoing data */
4159 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004160 {
4161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004163 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004164 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004168 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004169 /* Protocol must be DLTS, not TLS */
4170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004171 {
4172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004174 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004175 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004176 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004180 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004181 /* We must be using an AEAD ciphersuite */
4182 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004183 {
4184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004186 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004187 /* Renegotiation must not be enabled */
4188#if defined(MBEDTLS_SSL_RENEGOTIATION)
4189 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004190 {
4191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004193 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004194#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004195
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004196 /*
4197 * Version and format identifier
4198 */
4199 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004200
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004201 if( used <= buf_len )
4202 {
4203 memcpy( p, ssl_serialized_context_header,
4204 sizeof( ssl_serialized_context_header ) );
4205 p += sizeof( ssl_serialized_context_header );
4206 }
4207
4208 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004209 * Session (length + data)
4210 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004211 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004212 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4213 return( ret );
4214
4215 used += 4 + session_len;
4216 if( used <= buf_len )
4217 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004218 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4219 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004220
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004221 ret = ssl_session_save( ssl->session, 1,
4222 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004223 if( ret != 0 )
4224 return( ret );
4225
4226 p += session_len;
4227 }
4228
4229 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004230 * Transform
4231 */
4232 used += sizeof( ssl->transform->randbytes );
4233 if( used <= buf_len )
4234 {
4235 memcpy( p, ssl->transform->randbytes,
4236 sizeof( ssl->transform->randbytes ) );
4237 p += sizeof( ssl->transform->randbytes );
4238 }
4239
4240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4241 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4242 if( used <= buf_len )
4243 {
4244 *p++ = ssl->transform->in_cid_len;
4245 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4246 p += ssl->transform->in_cid_len;
4247
4248 *p++ = ssl->transform->out_cid_len;
4249 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4250 p += ssl->transform->out_cid_len;
4251 }
4252#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4253
4254 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004255 * Saved fields from top-level ssl_context structure
4256 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004257 used += 4;
4258 if( used <= buf_len )
4259 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004260 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4261 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004262 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004263
4264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4265 used += 16;
4266 if( used <= buf_len )
4267 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004268 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4269 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004270
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004271 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4272 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004273 }
4274#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4275
4276#if defined(MBEDTLS_SSL_PROTO_DTLS)
4277 used += 1;
4278 if( used <= buf_len )
4279 {
4280 *p++ = ssl->disable_datagram_packing;
4281 }
4282#endif /* MBEDTLS_SSL_PROTO_DTLS */
4283
Jerry Yuae0b2e22021-10-08 15:21:19 +08004284 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004285 if( used <= buf_len )
4286 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004287 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4288 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004289 }
4290
4291#if defined(MBEDTLS_SSL_PROTO_DTLS)
4292 used += 2;
4293 if( used <= buf_len )
4294 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004295 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4296 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004297 }
4298#endif /* MBEDTLS_SSL_PROTO_DTLS */
4299
4300#if defined(MBEDTLS_SSL_ALPN)
4301 {
4302 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004303 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004304 : 0;
4305
4306 used += 1 + alpn_len;
4307 if( used <= buf_len )
4308 {
4309 *p++ = alpn_len;
4310
4311 if( ssl->alpn_chosen != NULL )
4312 {
4313 memcpy( p, ssl->alpn_chosen, alpn_len );
4314 p += alpn_len;
4315 }
4316 }
4317 }
4318#endif /* MBEDTLS_SSL_ALPN */
4319
4320 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004321 * Done
4322 */
4323 *olen = used;
4324
4325 if( used > buf_len )
4326 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004327
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004328 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4329
Hanno Becker43aefe22020-02-05 10:44:56 +00004330 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004331}
4332
4333/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004334 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004335 *
4336 * This internal version is wrapped by a public function that cleans up in
4337 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004338 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004339MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004340static int ssl_context_load( mbedtls_ssl_context *ssl,
4341 const unsigned char *buf,
4342 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004343{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004344 const unsigned char *p = buf;
4345 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004346 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004348#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004349 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004350#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004351
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004352 /*
4353 * The context should have been freshly setup or reset.
4354 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004355 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004356 * renegotiating, or if the user mistakenly loaded a session first.)
4357 */
4358 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4359 ssl->session != NULL )
4360 {
4361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4362 }
4363
4364 /*
4365 * We can't check that the config matches the initial one, but we can at
4366 * least check it matches the requirements for serializing.
4367 */
4368 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004369 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4370 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004371#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004372 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004373#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004374 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004375 {
4376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4377 }
4378
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004379 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4380
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004381 /*
4382 * Check version identifier
4383 */
4384 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4386
4387 if( memcmp( p, ssl_serialized_context_header,
4388 sizeof( ssl_serialized_context_header ) ) != 0 )
4389 {
4390 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4391 }
4392 p += sizeof( ssl_serialized_context_header );
4393
4394 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004395 * Session
4396 */
4397 if( (size_t)( end - p ) < 4 )
4398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4399
4400 session_len = ( (size_t) p[0] << 24 ) |
4401 ( (size_t) p[1] << 16 ) |
4402 ( (size_t) p[2] << 8 ) |
4403 ( (size_t) p[3] );
4404 p += 4;
4405
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004406 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004407 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004408 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004409 ssl->session_in = ssl->session;
4410 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004411 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004412
4413 if( (size_t)( end - p ) < session_len )
4414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4415
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004416 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004417 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004418 {
4419 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004420 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004421 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004422
4423 p += session_len;
4424
4425 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004426 * Transform
4427 */
4428
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004429 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004430 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004431 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004432 ssl->transform_in = ssl->transform;
4433 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004434 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004435
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004436#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004437 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4438 if( prf_func == NULL )
4439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4440
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004441 /* Read random bytes and populate structure */
4442 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004444
Hanno Beckerbd257552021-03-22 06:59:27 +00004445 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004446 ssl->session->ciphersuite,
4447 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004448#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004449 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004450#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004451 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004452 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004453 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004454 ssl->conf->endpoint,
4455 ssl );
4456 if( ret != 0 )
4457 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004459 p += sizeof( ssl->transform->randbytes );
4460
4461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4462 /* Read connection IDs and store them */
4463 if( (size_t)( end - p ) < 1 )
4464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4465
4466 ssl->transform->in_cid_len = *p++;
4467
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004468 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4470
4471 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4472 p += ssl->transform->in_cid_len;
4473
4474 ssl->transform->out_cid_len = *p++;
4475
4476 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4478
4479 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4480 p += ssl->transform->out_cid_len;
4481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4482
4483 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004484 * Saved fields from top-level ssl_context structure
4485 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004486 if( (size_t)( end - p ) < 4 )
4487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4488
4489 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4490 ( (uint32_t) p[1] << 16 ) |
4491 ( (uint32_t) p[2] << 8 ) |
4492 ( (uint32_t) p[3] );
4493 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004494
4495#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4496 if( (size_t)( end - p ) < 16 )
4497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4498
4499 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4500 ( (uint64_t) p[1] << 48 ) |
4501 ( (uint64_t) p[2] << 40 ) |
4502 ( (uint64_t) p[3] << 32 ) |
4503 ( (uint64_t) p[4] << 24 ) |
4504 ( (uint64_t) p[5] << 16 ) |
4505 ( (uint64_t) p[6] << 8 ) |
4506 ( (uint64_t) p[7] );
4507 p += 8;
4508
4509 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4510 ( (uint64_t) p[1] << 48 ) |
4511 ( (uint64_t) p[2] << 40 ) |
4512 ( (uint64_t) p[3] << 32 ) |
4513 ( (uint64_t) p[4] << 24 ) |
4514 ( (uint64_t) p[5] << 16 ) |
4515 ( (uint64_t) p[6] << 8 ) |
4516 ( (uint64_t) p[7] );
4517 p += 8;
4518#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4519
4520#if defined(MBEDTLS_SSL_PROTO_DTLS)
4521 if( (size_t)( end - p ) < 1 )
4522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4523
4524 ssl->disable_datagram_packing = *p++;
4525#endif /* MBEDTLS_SSL_PROTO_DTLS */
4526
Jerry Yud9a94fe2021-09-28 18:58:59 +08004527 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004529 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4530 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004531
4532#if defined(MBEDTLS_SSL_PROTO_DTLS)
4533 if( (size_t)( end - p ) < 2 )
4534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4535
4536 ssl->mtu = ( p[0] << 8 ) | p[1];
4537 p += 2;
4538#endif /* MBEDTLS_SSL_PROTO_DTLS */
4539
4540#if defined(MBEDTLS_SSL_ALPN)
4541 {
4542 uint8_t alpn_len;
4543 const char **cur;
4544
4545 if( (size_t)( end - p ) < 1 )
4546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4547
4548 alpn_len = *p++;
4549
4550 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4551 {
4552 /* alpn_chosen should point to an item in the configured list */
4553 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4554 {
4555 if( strlen( *cur ) == alpn_len &&
4556 memcmp( p, cur, alpn_len ) == 0 )
4557 {
4558 ssl->alpn_chosen = *cur;
4559 break;
4560 }
4561 }
4562 }
4563
4564 /* can only happen on conf mismatch */
4565 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4567
4568 p += alpn_len;
4569 }
4570#endif /* MBEDTLS_SSL_ALPN */
4571
4572 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004573 * Forced fields from top-level ssl_context structure
4574 *
4575 * Most of them already set to the correct value by mbedtls_ssl_init() and
4576 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4577 */
4578 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004579 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004580
Hanno Becker361b10d2019-08-30 10:42:49 +01004581 /* Adjust pointers for header fields of outgoing records to
4582 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004583 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004584
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004585#if defined(MBEDTLS_SSL_PROTO_DTLS)
4586 ssl->in_epoch = 1;
4587#endif
4588
4589 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4590 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004591 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4592 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004593 if( ssl->handshake != NULL )
4594 {
4595 mbedtls_ssl_handshake_free( ssl );
4596 mbedtls_free( ssl->handshake );
4597 ssl->handshake = NULL;
4598 }
4599
4600 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004601 * Done - should have consumed entire buffer
4602 */
4603 if( p != end )
4604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004605
4606 return( 0 );
4607}
4608
4609/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004610 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004611 */
4612int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4613 const unsigned char *buf,
4614 size_t len )
4615{
4616 int ret = ssl_context_load( context, buf, len );
4617
4618 if( ret != 0 )
4619 mbedtls_ssl_free( context );
4620
4621 return( ret );
4622}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004623#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004624
4625/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 * Free an SSL context
4627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004630 if( ssl == NULL )
4631 return;
4632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004634
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004635 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
sander-visserb8aa2072020-05-06 22:05:13 +02004637#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4638 size_t out_buf_len = ssl->out_buf_len;
4639#else
4640 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4641#endif
4642
Darryl Greenb33cc762019-11-28 14:29:44 +00004643 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004645 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 }
4647
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004648 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
sander-visserb8aa2072020-05-06 22:05:13 +02004650#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4651 size_t in_buf_len = ssl->in_buf_len;
4652#else
4653 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4654#endif
4655
Darryl Greenb33cc762019-11-28 14:29:44 +00004656 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004658 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004659 }
4660
Paul Bakker48916f92012-09-16 19:57:18 +00004661 if( ssl->transform )
4662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 mbedtls_ssl_transform_free( ssl->transform );
4664 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004665 }
4666
4667 if( ssl->handshake )
4668 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004669 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4671 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673 mbedtls_free( ssl->handshake );
4674 mbedtls_free( ssl->transform_negotiate );
4675 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004676 }
4677
Ronald Cron6f135e12021-12-08 16:57:54 +01004678#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004679 mbedtls_ssl_transform_free( ssl->transform_application );
4680 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004681#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004682
Paul Bakkerc0463502013-02-14 11:19:38 +01004683 if( ssl->session )
4684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 mbedtls_ssl_session_free( ssl->session );
4686 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004687 }
4688
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004689#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004690 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004692 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004695#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004696
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004697#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004699#endif
4700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004702
Paul Bakker86f04f42013-02-14 11:20:09 +01004703 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004704 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705}
4706
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004707/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004708 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004709 */
4710void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4711{
4712 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4713}
4714
Gilles Peskineae270bf2021-06-02 00:05:29 +02004715/* The selection should be the same as mbedtls_x509_crt_profile_default in
4716 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004717 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004718 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004719 * about this list.
4720 */
Brett Warrene0edc842021-08-17 09:53:13 +01004721static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004722#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004723 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004724#endif
4725#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004726 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004727#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004728#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004729 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004730#endif
4731#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004732 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004733#endif
4734#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004735 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004736#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004737#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004738 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004739#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004740#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004741 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004742#endif
4743#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004744 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004745#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004746 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004747};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004748
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004749static int ssl_preset_suiteb_ciphersuites[] = {
4750 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4751 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4752 0
4753};
4754
Ronald Crone68ab4f2022-10-05 12:46:29 +02004755#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004756
Jerry Yu909df7b2022-01-22 11:56:27 +08004757/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004758 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004759 * rules SHOULD be upheld.
4760 * - No duplicate entries.
4761 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004762 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004763 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004764 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004765static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004766
Andrzej Kurek25f27152022-08-17 16:09:31 -04004767#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 +08004768 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4769 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004770#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004771 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004772
Andrzej Kurek25f27152022-08-17 16:09:31 -04004773#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 +08004774 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4775 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004776#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004777 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4778
Andrzej Kurek25f27152022-08-17 16:09:31 -04004779#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 +08004780 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4781 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004782#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004783 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004784
Andrzej Kurek25f27152022-08-17 16:09:31 -04004785#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 +08004786 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004787#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 +08004788
Andrzej Kurek25f27152022-08-17 16:09:31 -04004789#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 +08004790 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004791#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 +08004792
Andrzej Kurek25f27152022-08-17 16:09:31 -04004793#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 +08004794 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004795#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 +08004796
Andrzej Kurek25f27152022-08-17 16:09:31 -04004797#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 +08004798 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4799#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4800
Andrzej Kurek25f27152022-08-17 16:09:31 -04004801#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 +08004802 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4803#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4804
Andrzej Kurek25f27152022-08-17 16:09:31 -04004805#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 +08004806 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4807#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4808
Gabor Mezei15b95a62022-05-09 16:37:58 +02004809 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004810};
4811
4812/* NOTICE: see above */
4813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4814static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004815#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004816#if defined(MBEDTLS_ECDSA_C)
4817 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004818#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004819#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4820 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4821#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004822#if defined(MBEDTLS_RSA_C)
4823 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4824#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004825#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004826#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004827#if defined(MBEDTLS_ECDSA_C)
4828 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004829#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004830#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4831 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4832#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004833#if defined(MBEDTLS_RSA_C)
4834 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4835#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004836#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004837#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004838#if defined(MBEDTLS_ECDSA_C)
4839 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004840#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004841#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4842 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4843#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004844#if defined(MBEDTLS_RSA_C)
4845 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4846#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004847#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004848 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004849};
4850#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4851/* NOTICE: see above */
4852static uint16_t ssl_preset_suiteb_sig_algs[] = {
4853
Andrzej Kurek25f27152022-08-17 16:09:31 -04004854#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 +08004855 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4856 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004857#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004858 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4859
Andrzej Kurek25f27152022-08-17 16:09:31 -04004860#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 +08004861 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4862 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004863#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004864 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004865
Andrzej Kurek25f27152022-08-17 16:09:31 -04004866#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 +08004867 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004868#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 +08004869
Andrzej Kurek25f27152022-08-17 16:09:31 -04004870#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 +08004871 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004872#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004873
Gabor Mezei15b95a62022-05-09 16:37:58 +02004874 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004875};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004876
Jerry Yu909df7b2022-01-22 11:56:27 +08004877/* NOTICE: see above */
4878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4879static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004880#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004881#if defined(MBEDTLS_ECDSA_C)
4882 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004883#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004884#if defined(MBEDTLS_RSA_C)
4885 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4886#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004887#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004888#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004889#if defined(MBEDTLS_ECDSA_C)
4890 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004891#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004892#if defined(MBEDTLS_RSA_C)
4893 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4894#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004895#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004896 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004897};
Jerry Yu909df7b2022-01-22 11:56:27 +08004898#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4899
Ronald Crone68ab4f2022-10-05 12:46:29 +02004900#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004901
Brett Warrene0edc842021-08-17 09:53:13 +01004902static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004903#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004904 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004905#endif
4906#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004907 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004908#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004909 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004910};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004911
Ronald Crone68ab4f2022-10-05 12:46:29 +02004912#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004913/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004914 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004915MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004916static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004917{
4918 size_t i, j;
4919 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004920
Gabor Mezei15b95a62022-05-09 16:37:58 +02004921 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004922 {
Jerry Yuf377d642022-01-25 10:43:59 +08004923 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004924 {
Jerry Yuf377d642022-01-25 10:43:59 +08004925 if( sig_algs[i] != sig_algs[j] )
4926 continue;
4927 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4928 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4929 sig_algs[i], j, i );
4930 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004931 }
4932 }
4933 return( ret );
4934}
4935
Ronald Crone68ab4f2022-10-05 12:46:29 +02004936#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004937
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004938/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004939 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004940 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004941int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004942 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004943{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004944#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004946#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004947
Ronald Crone68ab4f2022-10-05 12:46:29 +02004948#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004949 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004950 {
4951 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4952 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4953 }
4954
Jerry Yuf377d642022-01-25 10:43:59 +08004955 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004956 {
4957 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4958 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4959 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004960
4961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004962 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004963 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004964 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004965 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4966 }
4967
Jerry Yuf377d642022-01-25 10:43:59 +08004968 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004969 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004970 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004971 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4972 }
4973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004974#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004975
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004976 /* Use the functions here so that they are covered in tests,
4977 * but otherwise access member directly for efficiency */
4978 mbedtls_ssl_conf_endpoint( conf, endpoint );
4979 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004980
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004981 /*
4982 * Things that are common to all presets
4983 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004984#if defined(MBEDTLS_SSL_CLI_C)
4985 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4986 {
4987 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4988#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4989 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4990#endif
4991 }
4992#endif
4993
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004994#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4995 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4996#endif
4997
4998#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4999 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5000#endif
5001
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005002#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005003 conf->f_cookie_write = ssl_cookie_write_dummy;
5004 conf->f_cookie_check = ssl_cookie_check_dummy;
5005#endif
5006
5007#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5008 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5009#endif
5010
Janos Follath088ce432017-04-10 12:42:31 +01005011#if defined(MBEDTLS_SSL_SRV_C)
5012 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005013 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005014#endif
5015
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005016#if defined(MBEDTLS_SSL_PROTO_DTLS)
5017 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5018 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5019#endif
5020
5021#if defined(MBEDTLS_SSL_RENEGOTIATION)
5022 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005023 memset( conf->renego_period, 0x00, 2 );
5024 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005025#endif
5026
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005027#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005028 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5029 {
5030 const unsigned char dhm_p[] =
5031 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5032 const unsigned char dhm_g[] =
5033 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005034
Hanno Beckere2defad2021-07-24 05:59:17 +01005035 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5036 dhm_p, sizeof( dhm_p ),
5037 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5038 {
5039 return( ret );
5040 }
5041 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005042#endif
5043
Ronald Cron6f135e12021-12-08 16:57:54 +01005044#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005045#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005046 mbedtls_ssl_conf_new_session_tickets(
5047 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5048#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005049 /*
5050 * Allow all TLS 1.3 key exchange modes by default.
5051 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005052 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005053#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005054
XiaokangQian4d3a6042022-04-21 13:46:17 +00005055 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5056 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005057#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005058 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005059 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005060#else
XiaokangQian060d8672022-04-21 09:24:56 +00005061 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005062#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005063 }
5064 else
5065 {
5066#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5067 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5068 {
5069 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5070 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5071 }
5072 else
5073 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5074 {
5075 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5076 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5077 }
5078#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5079 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5080 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5081#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5082 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5083 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5084#else
5085 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5086#endif
5087 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005088
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005089 /*
5090 * Preset-specific defaults
5091 */
5092 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005093 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005094 /*
5095 * NSA Suite B
5096 */
5097 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005098
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005099 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005100
5101#if defined(MBEDTLS_X509_CRT_PARSE_C)
5102 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005103#endif
5104
Ronald Crone68ab4f2022-10-05 12:46:29 +02005105#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005106#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5107 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5108 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5109 else
5110#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5111 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005112#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005113
Brett Warrene0edc842021-08-17 09:53:13 +01005114#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5115 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005116#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005117 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005118 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005119
5120 /*
5121 * Default
5122 */
5123 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005124
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005125 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005126
5127#if defined(MBEDTLS_X509_CRT_PARSE_C)
5128 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5129#endif
5130
Ronald Crone68ab4f2022-10-05 12:46:29 +02005131#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005132#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5133 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5134 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5135 else
5136#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5137 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005138#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005139
Brett Warrene0edc842021-08-17 09:53:13 +01005140#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5141 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005142#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005143 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005144
5145#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5146 conf->dhm_min_bitlen = 1024;
5147#endif
5148 }
5149
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005150 return( 0 );
5151}
5152
5153/*
5154 * Free mbedtls_ssl_config
5155 */
5156void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5157{
5158#if defined(MBEDTLS_DHM_C)
5159 mbedtls_mpi_free( &conf->dhm_P );
5160 mbedtls_mpi_free( &conf->dhm_G );
5161#endif
5162
Ronald Cron73fe8df2022-10-05 14:31:43 +02005163#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005164#if defined(MBEDTLS_USE_PSA_CRYPTO)
5165 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5166 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005167 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5168 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005169#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005170 if( conf->psk != NULL )
5171 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005172 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005173 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005174 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005175 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005176 }
5177
5178 if( conf->psk_identity != NULL )
5179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005180 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005181 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005182 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005183 conf->psk_identity_len = 0;
5184 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005185#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005186
5187#if defined(MBEDTLS_X509_CRT_PARSE_C)
5188 ssl_key_cert_free( conf->key_cert );
5189#endif
5190
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005191 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005192}
5193
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005194#if defined(MBEDTLS_PK_C) && \
5195 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005196/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201#if defined(MBEDTLS_RSA_C)
5202 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5203 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205#if defined(MBEDTLS_ECDSA_C)
5206 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5207 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005210}
5211
Hanno Becker7e5437a2017-04-28 17:15:26 +01005212unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5213{
5214 switch( type ) {
5215 case MBEDTLS_PK_RSA:
5216 return( MBEDTLS_SSL_SIG_RSA );
5217 case MBEDTLS_PK_ECDSA:
5218 case MBEDTLS_PK_ECKEY:
5219 return( MBEDTLS_SSL_SIG_ECDSA );
5220 default:
5221 return( MBEDTLS_SSL_SIG_ANON );
5222 }
5223}
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005226{
5227 switch( sig )
5228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#if defined(MBEDTLS_RSA_C)
5230 case MBEDTLS_SSL_SIG_RSA:
5231 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233#if defined(MBEDTLS_ECDSA_C)
5234 case MBEDTLS_SSL_SIG_ECDSA:
5235 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005236#endif
5237 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005239 }
5240}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005241#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005242
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005243/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005244 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005247{
5248 switch( hash )
5249 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005250#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 case MBEDTLS_SSL_HASH_MD5:
5252 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005253#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005254#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 case MBEDTLS_SSL_HASH_SHA1:
5256 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005257#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005258#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 case MBEDTLS_SSL_HASH_SHA224:
5260 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005261#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005262#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 case MBEDTLS_SSL_HASH_SHA256:
5264 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005265#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005266#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 case MBEDTLS_SSL_HASH_SHA384:
5268 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005269#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005270#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 case MBEDTLS_SSL_HASH_SHA512:
5272 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005273#endif
5274 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005276 }
5277}
5278
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005279/*
5280 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5281 */
5282unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5283{
5284 switch( md )
5285 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005286#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005287 case MBEDTLS_MD_MD5:
5288 return( MBEDTLS_SSL_HASH_MD5 );
5289#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005290#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005291 case MBEDTLS_MD_SHA1:
5292 return( MBEDTLS_SSL_HASH_SHA1 );
5293#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005294#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005295 case MBEDTLS_MD_SHA224:
5296 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005297#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005298#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005299 case MBEDTLS_MD_SHA256:
5300 return( MBEDTLS_SSL_HASH_SHA256 );
5301#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005302#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005303 case MBEDTLS_MD_SHA384:
5304 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005305#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005306#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005307 case MBEDTLS_MD_SHA512:
5308 return( MBEDTLS_SSL_HASH_SHA512 );
5309#endif
5310 default:
5311 return( MBEDTLS_SSL_HASH_NONE );
5312 }
5313}
5314
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005315/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005316 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005317 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005318 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005319int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005320{
Brett Warrene0edc842021-08-17 09:53:13 +01005321 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005322
Brett Warrene0edc842021-08-17 09:53:13 +01005323 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005324 return( -1 );
5325
Brett Warrene0edc842021-08-17 09:53:13 +01005326 for( ; *group_list != 0; group_list++ )
5327 {
5328 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005329 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005330 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005331
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005332 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005333}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005334
5335#if defined(MBEDTLS_ECP_C)
5336/*
5337 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5338 */
5339int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5340{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005341 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005342 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005343
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005344 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005345 return -1;
5346
5347 uint16_t tls_id = grp_info->tls_id;
5348
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005349 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5350}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005351#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if defined(MBEDTLS_X509_CRT_PARSE_C)
5354int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5355 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005356 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005357 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005358{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005359 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005360 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005361 const char *ext_oid;
5362 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005365 {
5366 /* Server part of the key exchange */
5367 switch( ciphersuite->key_exchange )
5368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 case MBEDTLS_KEY_EXCHANGE_RSA:
5370 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005371 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005372 break;
5373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5375 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5376 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5377 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005378 break;
5379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5381 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005382 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005383 break;
5384
5385 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 case MBEDTLS_KEY_EXCHANGE_NONE:
5387 case MBEDTLS_KEY_EXCHANGE_PSK:
5388 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5389 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005390 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005391 usage = 0;
5392 }
5393 }
5394 else
5395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5397 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005398 }
5399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005401 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005402 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005403 ret = -1;
5404 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5409 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005410 }
5411 else
5412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5414 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005415 }
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005418 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005419 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005420 ret = -1;
5421 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005422
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005423 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005426
Jerry Yu148165c2021-09-24 23:20:59 +08005427#if defined(MBEDTLS_USE_PSA_CRYPTO)
5428int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5429 const mbedtls_md_type_t md,
5430 unsigned char *dst,
5431 size_t dst_len,
5432 size_t *olen )
5433{
Ronald Cronf6893e12022-01-07 22:09:01 +01005434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5435 psa_hash_operation_t *hash_operation_to_clone;
5436 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5437
Jerry Yu148165c2021-09-24 23:20:59 +08005438 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005439
5440 switch( md )
5441 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005442#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005443 case MBEDTLS_MD_SHA384:
5444 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5445 break;
5446#endif
5447
Andrzej Kurek25f27152022-08-17 16:09:31 -04005448#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005449 case MBEDTLS_MD_SHA256:
5450 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5451 break;
5452#endif
5453
5454 default:
5455 goto exit;
5456 }
5457
5458 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5459 if( status != PSA_SUCCESS )
5460 goto exit;
5461
5462 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5463 if( status != PSA_SUCCESS )
5464 goto exit;
5465
5466exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005467#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5468 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5469 (void) ssl;
5470#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005471 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005472}
5473#else /* MBEDTLS_USE_PSA_CRYPTO */
5474
Andrzej Kurek25f27152022-08-17 16:09:31 -04005475#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005476MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005477static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5478 unsigned char *dst,
5479 size_t dst_len,
5480 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005481{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005482 int ret;
5483 mbedtls_sha512_context sha512;
5484
5485 if( dst_len < 48 )
5486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5487
5488 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005489 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005490
5491 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5492 {
5493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5494 goto exit;
5495 }
5496
5497 *olen = 48;
5498
5499exit:
5500
5501 mbedtls_sha512_free( &sha512 );
5502 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005503}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005504#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005505
Andrzej Kurek25f27152022-08-17 16:09:31 -04005506#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005507MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005508static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5509 unsigned char *dst,
5510 size_t dst_len,
5511 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005512{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005513 int ret;
5514 mbedtls_sha256_context sha256;
5515
5516 if( dst_len < 32 )
5517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5518
5519 mbedtls_sha256_init( &sha256 );
5520 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005521
Jerry Yu24c0ec32021-09-09 14:21:07 +08005522 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5523 {
5524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5525 goto exit;
5526 }
5527
5528 *olen = 32;
5529
5530exit:
5531
5532 mbedtls_sha256_free( &sha256 );
5533 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005534}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005535#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005536
Jerry Yu000f9762021-09-14 11:12:51 +08005537int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5538 const mbedtls_md_type_t md,
5539 unsigned char *dst,
5540 size_t dst_len,
5541 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005542{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005543 switch( md )
5544 {
5545
Andrzej Kurek25f27152022-08-17 16:09:31 -04005546#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005547 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005548 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005549#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005550
Andrzej Kurek25f27152022-08-17 16:09:31 -04005551#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005552 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005553 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005554#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005555
5556 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005557#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5558 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5559 (void) ssl;
5560 (void) dst;
5561 (void) dst_len;
5562 (void) olen;
5563#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005564 break;
5565 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005567}
XiaokangQian647719a2021-12-07 09:16:29 +00005568
Jerry Yu148165c2021-09-24 23:20:59 +08005569#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005570
Ronald Crone68ab4f2022-10-05 12:46:29 +02005571#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005572/* mbedtls_ssl_parse_sig_alg_ext()
5573 *
5574 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5575 * value (TLS 1.3 RFC8446):
5576 * enum {
5577 * ....
5578 * ecdsa_secp256r1_sha256( 0x0403 ),
5579 * ecdsa_secp384r1_sha384( 0x0503 ),
5580 * ecdsa_secp521r1_sha512( 0x0603 ),
5581 * ....
5582 * } SignatureScheme;
5583 *
5584 * struct {
5585 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5586 * } SignatureSchemeList;
5587 *
5588 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5589 * value (TLS 1.2 RFC5246):
5590 * enum {
5591 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5592 * sha512(6), (255)
5593 * } HashAlgorithm;
5594 *
5595 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5596 * SignatureAlgorithm;
5597 *
5598 * struct {
5599 * HashAlgorithm hash;
5600 * SignatureAlgorithm signature;
5601 * } SignatureAndHashAlgorithm;
5602 *
5603 * SignatureAndHashAlgorithm
5604 * supported_signature_algorithms<2..2^16-2>;
5605 *
5606 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5607 * generalization of the TLS 1.2 signature algorithm extension.
5608 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5609 * `SignatureScheme` field of TLS 1.3
5610 *
5611 */
5612int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5613 const unsigned char *buf,
5614 const unsigned char *end )
5615{
5616 const unsigned char *p = buf;
5617 size_t supported_sig_algs_len = 0;
5618 const unsigned char *supported_sig_algs_end;
5619 uint16_t sig_alg;
5620 uint32_t common_idx = 0;
5621
5622 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5623 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5624 p += 2;
5625
5626 memset( ssl->handshake->received_sig_algs, 0,
5627 sizeof(ssl->handshake->received_sig_algs) );
5628
5629 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5630 supported_sig_algs_end = p + supported_sig_algs_len;
5631 while( p < supported_sig_algs_end )
5632 {
5633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5634 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5635 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005636 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5637 sig_alg,
5638 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005640 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005641 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5642 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5643 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005644 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005645 }
5646#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005647
Jerry Yuf3b46b52022-06-19 16:52:27 +08005648 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5649 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005650
5651 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5652 {
5653 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5654 common_idx += 1;
5655 }
5656 }
5657 /* Check that we consumed all the message. */
5658 if( p != end )
5659 {
5660 MBEDTLS_SSL_DEBUG_MSG( 1,
5661 ( "Signature algorithms extension length misaligned" ) );
5662 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5663 MBEDTLS_ERR_SSL_DECODE_ERROR );
5664 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5665 }
5666
5667 if( common_idx == 0 )
5668 {
5669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5670 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5671 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5672 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5673 }
5674
Gabor Mezei15b95a62022-05-09 16:37:58 +02005675 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005676 return( 0 );
5677}
5678
Ronald Crone68ab4f2022-10-05 12:46:29 +02005679#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005680
Jerry Yudc7bd172022-02-17 13:44:15 +08005681#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5682
5683#if defined(MBEDTLS_USE_PSA_CRYPTO)
5684
5685static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5686 mbedtls_svc_key_id_t key,
5687 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005688 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005689 const unsigned char* seed, size_t seed_length,
5690 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005691 const unsigned char* other_secret,
5692 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005693 size_t capacity )
5694{
5695 psa_status_t status;
5696
5697 status = psa_key_derivation_setup( derivation, alg );
5698 if( status != PSA_SUCCESS )
5699 return( status );
5700
5701 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5702 {
5703 status = psa_key_derivation_input_bytes( derivation,
5704 PSA_KEY_DERIVATION_INPUT_SEED,
5705 seed, seed_length );
5706 if( status != PSA_SUCCESS )
5707 return( status );
5708
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005709 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005710 {
5711 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005712 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5713 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005714 if( status != PSA_SUCCESS )
5715 return( status );
5716 }
5717
Jerry Yudc7bd172022-02-17 13:44:15 +08005718 if( mbedtls_svc_key_id_is_null( key ) )
5719 {
5720 status = psa_key_derivation_input_bytes(
5721 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005722 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005723 }
5724 else
5725 {
5726 status = psa_key_derivation_input_key(
5727 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5728 }
5729 if( status != PSA_SUCCESS )
5730 return( status );
5731
5732 status = psa_key_derivation_input_bytes( derivation,
5733 PSA_KEY_DERIVATION_INPUT_LABEL,
5734 label, label_length );
5735 if( status != PSA_SUCCESS )
5736 return( status );
5737 }
5738 else
5739 {
5740 return( PSA_ERROR_NOT_SUPPORTED );
5741 }
5742
5743 status = psa_key_derivation_set_capacity( derivation, capacity );
5744 if( status != PSA_SUCCESS )
5745 return( status );
5746
5747 return( PSA_SUCCESS );
5748}
5749
Andrzej Kurek57d10632022-10-24 10:32:01 -04005750#if defined(PSA_WANT_ALG_SHA_384) || \
5751 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005752MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005753static int tls_prf_generic( mbedtls_md_type_t md_type,
5754 const unsigned char *secret, size_t slen,
5755 const char *label,
5756 const unsigned char *random, size_t rlen,
5757 unsigned char *dstbuf, size_t dlen )
5758{
5759 psa_status_t status;
5760 psa_algorithm_t alg;
5761 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5762 psa_key_derivation_operation_t derivation =
5763 PSA_KEY_DERIVATION_OPERATION_INIT;
5764
5765 if( md_type == MBEDTLS_MD_SHA384 )
5766 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5767 else
5768 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5769
5770 /* Normally a "secret" should be long enough to be impossible to
5771 * find by brute force, and in particular should not be empty. But
5772 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5773 * and for this use case it makes sense to have a 0-length "secret".
5774 * Since the key API doesn't allow importing a key of length 0,
5775 * keep master_key=0, which setup_psa_key_derivation() understands
5776 * to mean a 0-length "secret" input. */
5777 if( slen != 0 )
5778 {
5779 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5780 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5781 psa_set_key_algorithm( &key_attributes, alg );
5782 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5783
5784 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5785 if( status != PSA_SUCCESS )
5786 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5787 }
5788
5789 status = setup_psa_key_derivation( &derivation,
5790 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005791 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005792 random, rlen,
5793 (unsigned char const *) label,
5794 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005795 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005796 dlen );
5797 if( status != PSA_SUCCESS )
5798 {
5799 psa_key_derivation_abort( &derivation );
5800 psa_destroy_key( master_key );
5801 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5802 }
5803
5804 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5805 if( status != PSA_SUCCESS )
5806 {
5807 psa_key_derivation_abort( &derivation );
5808 psa_destroy_key( master_key );
5809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5810 }
5811
5812 status = psa_key_derivation_abort( &derivation );
5813 if( status != PSA_SUCCESS )
5814 {
5815 psa_destroy_key( master_key );
5816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5817 }
5818
5819 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5820 status = psa_destroy_key( master_key );
5821 if( status != PSA_SUCCESS )
5822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5823
5824 return( 0 );
5825}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005826#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005827#else /* MBEDTLS_USE_PSA_CRYPTO */
5828
Andrzej Kurek57d10632022-10-24 10:32:01 -04005829#if defined(MBEDTLS_MD_C) && \
5830 ( defined(MBEDTLS_SHA256_C) || \
5831 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005832MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005833static int tls_prf_generic( mbedtls_md_type_t md_type,
5834 const unsigned char *secret, size_t slen,
5835 const char *label,
5836 const unsigned char *random, size_t rlen,
5837 unsigned char *dstbuf, size_t dlen )
5838{
5839 size_t nb;
5840 size_t i, j, k, md_len;
5841 unsigned char *tmp;
5842 size_t tmp_len = 0;
5843 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5844 const mbedtls_md_info_t *md_info;
5845 mbedtls_md_context_t md_ctx;
5846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5847
5848 mbedtls_md_init( &md_ctx );
5849
5850 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5852
5853 md_len = mbedtls_md_get_size( md_info );
5854
5855 tmp_len = md_len + strlen( label ) + rlen;
5856 tmp = mbedtls_calloc( 1, tmp_len );
5857 if( tmp == NULL )
5858 {
5859 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5860 goto exit;
5861 }
5862
5863 nb = strlen( label );
5864 memcpy( tmp + md_len, label, nb );
5865 memcpy( tmp + md_len + nb, random, rlen );
5866 nb += rlen;
5867
5868 /*
5869 * Compute P_<hash>(secret, label + random)[0..dlen]
5870 */
5871 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5872 goto exit;
5873
5874 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5875 if( ret != 0 )
5876 goto exit;
5877 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5878 if( ret != 0 )
5879 goto exit;
5880 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5881 if( ret != 0 )
5882 goto exit;
5883
5884 for( i = 0; i < dlen; i += md_len )
5885 {
5886 ret = mbedtls_md_hmac_reset ( &md_ctx );
5887 if( ret != 0 )
5888 goto exit;
5889 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5890 if( ret != 0 )
5891 goto exit;
5892 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5893 if( ret != 0 )
5894 goto exit;
5895
5896 ret = mbedtls_md_hmac_reset ( &md_ctx );
5897 if( ret != 0 )
5898 goto exit;
5899 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5900 if( ret != 0 )
5901 goto exit;
5902 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5903 if( ret != 0 )
5904 goto exit;
5905
5906 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5907
5908 for( j = 0; j < k; j++ )
5909 dstbuf[i + j] = h_i[j];
5910 }
5911
5912exit:
5913 mbedtls_md_free( &md_ctx );
5914
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005915 if ( tmp != NULL )
5916 mbedtls_platform_zeroize( tmp, tmp_len );
5917
Jerry Yudc7bd172022-02-17 13:44:15 +08005918 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5919
5920 mbedtls_free( tmp );
5921
5922 return( ret );
5923}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005924#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005925#endif /* MBEDTLS_USE_PSA_CRYPTO */
5926
Andrzej Kurek25f27152022-08-17 16:09:31 -04005927#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005928MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005929static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5930 const char *label,
5931 const unsigned char *random, size_t rlen,
5932 unsigned char *dstbuf, size_t dlen )
5933{
5934 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5935 label, random, rlen, dstbuf, dlen ) );
5936}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005937#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005938
Andrzej Kurek25f27152022-08-17 16:09:31 -04005939#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005940MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005941static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5942 const char *label,
5943 const unsigned char *random, size_t rlen,
5944 unsigned char *dstbuf, size_t dlen )
5945{
5946 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5947 label, random, rlen, dstbuf, dlen ) );
5948}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005949#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005950
Jerry Yuf009d862022-02-17 14:01:37 +08005951/*
5952 * Set appropriate PRF function and other SSL / TLS1.2 functions
5953 *
5954 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005955 * - hash associated with the ciphersuite (only used by TLS 1.2)
5956 *
5957 * Outputs:
5958 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5959 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005961static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005962 mbedtls_md_type_t hash )
5963{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005964#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005965 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005966 {
5967 handshake->tls_prf = tls_prf_sha384;
5968 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5969 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5970 }
5971 else
5972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005973#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005974 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005975 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005976 handshake->tls_prf = tls_prf_sha256;
5977 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5978 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5979 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005980#else
Jerry Yuf009d862022-02-17 14:01:37 +08005981 {
Jerry Yuf009d862022-02-17 14:01:37 +08005982 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005983 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5985 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005986#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005987
5988 return( 0 );
5989}
Jerry Yud6ab2352022-02-17 14:03:43 +08005990
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005991/*
5992 * Compute master secret if needed
5993 *
5994 * Parameters:
5995 * [in/out] handshake
5996 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5997 * (PSA-PSK) ciphersuite_info, psk_opaque
5998 * [out] premaster (cleared)
5999 * [out] master
6000 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6001 * debug: conf->f_dbg, conf->p_dbg
6002 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006003 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006004 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006005MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006006static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
6007 unsigned char *master,
6008 const mbedtls_ssl_context *ssl )
6009{
6010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6011
6012 /* cf. RFC 5246, Section 8.1:
6013 * "The master secret is always exactly 48 bytes in length." */
6014 size_t const master_secret_len = 48;
6015
6016#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6017 unsigned char session_hash[48];
6018#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6019
6020 /* The label for the KDF used for key expansion.
6021 * This is either "master secret" or "extended master secret"
6022 * depending on whether the Extended Master Secret extension
6023 * is used. */
6024 char const *lbl = "master secret";
6025
Przemek Stekielae4ed302022-04-05 17:15:55 +02006026 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006027 * - If the Extended Master Secret extension is not used,
6028 * this is ClientHello.Random + ServerHello.Random
6029 * (see Sect. 8.1 in RFC 5246).
6030 * - If the Extended Master Secret extension is used,
6031 * this is the transcript of the handshake so far.
6032 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006033 unsigned char const *seed = handshake->randbytes;
6034 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006035
6036#if !defined(MBEDTLS_DEBUG_C) && \
6037 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6038 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6039 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6040 ssl = NULL; /* make sure we don't use it except for those cases */
6041 (void) ssl;
6042#endif
6043
6044 if( handshake->resume != 0 )
6045 {
6046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6047 return( 0 );
6048 }
6049
6050#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6051 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6052 {
6053 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006054 seed = session_hash;
6055 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006056
6057 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006058 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006059 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006060#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006061
Przemek Stekiel99114f32022-04-22 11:20:09 +02006062#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006063 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006064 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006065 {
6066 /* Perform PSK-to-MS expansion in a single step. */
6067 psa_status_t status;
6068 psa_algorithm_t alg;
6069 mbedtls_svc_key_id_t psk;
6070 psa_key_derivation_operation_t derivation =
6071 PSA_KEY_DERIVATION_OPERATION_INIT;
6072 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6073
6074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6075
6076 psk = mbedtls_ssl_get_opaque_psk( ssl );
6077
6078 if( hash_alg == MBEDTLS_MD_SHA384 )
6079 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6080 else
6081 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6082
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006083 size_t other_secret_len = 0;
6084 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006085
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006086 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006087 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006088 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006089 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006090 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006091 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006092 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006093 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006094 other_secret_len = 48;
6095 other_secret = handshake->premaster + 2;
6096 break;
6097 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006098 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006099 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6100 other_secret = handshake->premaster + 2;
6101 break;
6102 default:
6103 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006104 }
6105
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006106 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006107 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006108 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006109 (unsigned char const *) lbl,
6110 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006111 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006112 master_secret_len );
6113 if( status != PSA_SUCCESS )
6114 {
6115 psa_key_derivation_abort( &derivation );
6116 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6117 }
6118
6119 status = psa_key_derivation_output_bytes( &derivation,
6120 master,
6121 master_secret_len );
6122 if( status != PSA_SUCCESS )
6123 {
6124 psa_key_derivation_abort( &derivation );
6125 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6126 }
6127
6128 status = psa_key_derivation_abort( &derivation );
6129 if( status != PSA_SUCCESS )
6130 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6131 }
6132 else
6133#endif
6134 {
6135 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006136 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006137 master,
6138 master_secret_len );
6139 if( ret != 0 )
6140 {
6141 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6142 return( ret );
6143 }
6144
6145 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6146 handshake->premaster,
6147 handshake->pmslen );
6148
6149 mbedtls_platform_zeroize( handshake->premaster,
6150 sizeof(handshake->premaster) );
6151 }
6152
6153 return( 0 );
6154}
6155
Jerry Yud62f87e2022-02-17 14:09:02 +08006156int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6157{
6158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6159 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6160 ssl->handshake->ciphersuite_info;
6161
6162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6163
6164 /* Set PRF, calc_verify and calc_finished function pointers */
6165 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006166 ciphersuite_info->mac );
6167 if( ret != 0 )
6168 {
6169 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6170 return( ret );
6171 }
6172
6173 /* Compute master secret if needed */
6174 ret = ssl_compute_master( ssl->handshake,
6175 ssl->session_negotiate->master,
6176 ssl );
6177 if( ret != 0 )
6178 {
6179 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6180 return( ret );
6181 }
6182
6183 /* Swap the client and server random values:
6184 * - MS derivation wanted client+server (RFC 5246 8.1)
6185 * - key derivation wants server+client (RFC 5246 6.3) */
6186 {
6187 unsigned char tmp[64];
6188 memcpy( tmp, ssl->handshake->randbytes, 64 );
6189 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6190 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6191 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6192 }
6193
6194 /* Populate transform structure */
6195 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6196 ssl->session_negotiate->ciphersuite,
6197 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006198#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006199 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006200#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006201 ssl->handshake->tls_prf,
6202 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006203 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006204 ssl->conf->endpoint,
6205 ssl );
6206 if( ret != 0 )
6207 {
6208 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6209 return( ret );
6210 }
6211
6212 /* We no longer need Server/ClientHello.random values */
6213 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6214 sizeof( ssl->handshake->randbytes ) );
6215
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6217
6218 return( 0 );
6219}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006220
Ronald Cron4dcbca92022-03-07 10:21:40 +01006221int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6222{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006223 switch( md )
6224 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006225#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006226 case MBEDTLS_SSL_HASH_SHA384:
6227 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6228 break;
6229#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006230#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006231 case MBEDTLS_SSL_HASH_SHA256:
6232 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6233 break;
6234#endif
6235 default:
6236 return( -1 );
6237 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006238#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6239 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6240 (void) ssl;
6241#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006242 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006243}
6244
Andrzej Kurek25f27152022-08-17 16:09:31 -04006245#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006246void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6247 unsigned char *hash,
6248 size_t *hlen )
6249{
6250#if defined(MBEDTLS_USE_PSA_CRYPTO)
6251 size_t hash_size;
6252 psa_status_t status;
6253 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6254
6255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6256 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6257 if( status != PSA_SUCCESS )
6258 {
6259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6260 return;
6261 }
6262
6263 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6264 if( status != PSA_SUCCESS )
6265 {
6266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6267 return;
6268 }
6269
6270 *hlen = 32;
6271 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6273#else
6274 mbedtls_sha256_context sha256;
6275
6276 mbedtls_sha256_init( &sha256 );
6277
6278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6279
6280 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6281 mbedtls_sha256_finish( &sha256, hash );
6282
6283 *hlen = 32;
6284
6285 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6287
6288 mbedtls_sha256_free( &sha256 );
6289#endif /* MBEDTLS_USE_PSA_CRYPTO */
6290 return;
6291}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006292#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006293
Andrzej Kurek25f27152022-08-17 16:09:31 -04006294#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006295void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6296 unsigned char *hash,
6297 size_t *hlen )
6298{
6299#if defined(MBEDTLS_USE_PSA_CRYPTO)
6300 size_t hash_size;
6301 psa_status_t status;
6302 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6303
6304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6305 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6306 if( status != PSA_SUCCESS )
6307 {
6308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6309 return;
6310 }
6311
6312 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6313 if( status != PSA_SUCCESS )
6314 {
6315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6316 return;
6317 }
6318
6319 *hlen = 48;
6320 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6322#else
6323 mbedtls_sha512_context sha512;
6324
6325 mbedtls_sha512_init( &sha512 );
6326
6327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6328
Andrzej Kureka242e832022-08-11 10:03:14 -04006329 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006330 mbedtls_sha512_finish( &sha512, hash );
6331
6332 *hlen = 48;
6333
6334 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6336
6337 mbedtls_sha512_free( &sha512 );
6338#endif /* MBEDTLS_USE_PSA_CRYPTO */
6339 return;
6340}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006341#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006342
Neil Armstrong80f6f322022-05-03 17:56:38 +02006343#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6344 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006345int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6346{
6347 unsigned char *p = ssl->handshake->premaster;
6348 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6349 const unsigned char *psk = NULL;
6350 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006351 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006352
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006353 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006354 {
6355 /*
6356 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006357 * checked before calling this function.
6358 *
6359 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006360 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006361 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006362 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6366 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006367 }
6368
6369 /*
6370 * PMS = struct {
6371 * opaque other_secret<0..2^16-1>;
6372 * opaque psk<0..2^16-1>;
6373 * };
6374 * with "other_secret" depending on the particular key exchange
6375 */
6376#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6377 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6378 {
6379 if( end - p < 2 )
6380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6381
6382 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6383 p += 2;
6384
6385 if( end < p || (size_t)( end - p ) < psk_len )
6386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6387
6388 memset( p, 0, psk_len );
6389 p += psk_len;
6390 }
6391 else
6392#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6393#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6394 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6395 {
6396 /*
6397 * other_secret already set by the ClientKeyExchange message,
6398 * and is 48 bytes long
6399 */
6400 if( end - p < 2 )
6401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6402
6403 *p++ = 0;
6404 *p++ = 48;
6405 p += 48;
6406 }
6407 else
6408#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6409#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6410 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6411 {
6412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6413 size_t len;
6414
6415 /* Write length only when we know the actual value */
6416 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6417 p + 2, end - ( p + 2 ), &len,
6418 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6419 {
6420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6421 return( ret );
6422 }
6423 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6424 p += 2 + len;
6425
6426 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6427 }
6428 else
6429#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006430#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006431 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6432 {
6433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6434 size_t zlen;
6435
6436 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6437 p + 2, end - ( p + 2 ),
6438 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6439 {
6440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6441 return( ret );
6442 }
6443
6444 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6445 p += 2 + zlen;
6446
6447 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6448 MBEDTLS_DEBUG_ECDH_Z );
6449 }
6450 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006451#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006452 {
6453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6455 }
6456
6457 /* opaque psk<0..2^16-1>; */
6458 if( end - p < 2 )
6459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6460
6461 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6462 p += 2;
6463
6464 if( end < p || (size_t)( end - p ) < psk_len )
6465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6466
6467 memcpy( p, psk, psk_len );
6468 p += psk_len;
6469
6470 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6471
6472 return( 0 );
6473}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006474#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006475
6476#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006477MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006478static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6479
6480#if defined(MBEDTLS_SSL_PROTO_DTLS)
6481int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6482{
6483 /* If renegotiation is not enforced, retransmit until we would reach max
6484 * timeout if we were using the usual handshake doubling scheme */
6485 if( ssl->conf->renego_max_records < 0 )
6486 {
6487 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6488 unsigned char doublings = 1;
6489
6490 while( ratio != 0 )
6491 {
6492 ++doublings;
6493 ratio >>= 1;
6494 }
6495
6496 if( ++ssl->renego_records_seen > doublings )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6499 return( 0 );
6500 }
6501 }
6502
6503 return( ssl_write_hello_request( ssl ) );
6504}
6505#endif
6506#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006507
Jerry Yud9526692022-02-17 14:23:47 +08006508/*
6509 * Handshake functions
6510 */
6511#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6512/* No certificate support -> dummy functions */
6513int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6514{
6515 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6516 ssl->handshake->ciphersuite_info;
6517
6518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6519
6520 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6521 {
6522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6523 ssl->state++;
6524 return( 0 );
6525 }
6526
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6529}
6530
6531int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6532{
6533 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6534 ssl->handshake->ciphersuite_info;
6535
6536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6537
6538 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6539 {
6540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6541 ssl->state++;
6542 return( 0 );
6543 }
6544
6545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6547}
6548
6549#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6550/* Some certificate support -> implement write and parse */
6551
6552int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6553{
6554 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6555 size_t i, n;
6556 const mbedtls_x509_crt *crt;
6557 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6558 ssl->handshake->ciphersuite_info;
6559
6560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6561
6562 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6563 {
6564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6565 ssl->state++;
6566 return( 0 );
6567 }
6568
6569#if defined(MBEDTLS_SSL_CLI_C)
6570 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6571 {
6572 if( ssl->handshake->client_auth == 0 )
6573 {
6574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6575 ssl->state++;
6576 return( 0 );
6577 }
6578 }
6579#endif /* MBEDTLS_SSL_CLI_C */
6580#if defined(MBEDTLS_SSL_SRV_C)
6581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6582 {
6583 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6584 {
6585 /* Should never happen because we shouldn't have picked the
6586 * ciphersuite if we don't have a certificate. */
6587 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6588 }
6589 }
6590#endif
6591
6592 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6593
6594 /*
6595 * 0 . 0 handshake type
6596 * 1 . 3 handshake length
6597 * 4 . 6 length of all certs
6598 * 7 . 9 length of cert. 1
6599 * 10 . n-1 peer certificate
6600 * n . n+2 length of cert. 2
6601 * n+3 . ... upper level cert, etc.
6602 */
6603 i = 7;
6604 crt = mbedtls_ssl_own_cert( ssl );
6605
6606 while( crt != NULL )
6607 {
6608 n = crt->raw.len;
6609 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6610 {
6611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6612 " > %" MBEDTLS_PRINTF_SIZET,
6613 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6614 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6615 }
6616
6617 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6618 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6619 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6620
6621 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6622 i += n; crt = crt->next;
6623 }
6624
6625 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6626 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6627 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6628
6629 ssl->out_msglen = i;
6630 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6631 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6632
6633 ssl->state++;
6634
6635 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6636 {
6637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6638 return( ret );
6639 }
6640
6641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6642
6643 return( ret );
6644}
6645
6646#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6647
6648#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006649MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006650static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6651 unsigned char *crt_buf,
6652 size_t crt_buf_len )
6653{
6654 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6655
6656 if( peer_crt == NULL )
6657 return( -1 );
6658
6659 if( peer_crt->raw.len != crt_buf_len )
6660 return( -1 );
6661
6662 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6663}
6664#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006665MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006666static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6667 unsigned char *crt_buf,
6668 size_t crt_buf_len )
6669{
6670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6671 unsigned char const * const peer_cert_digest =
6672 ssl->session->peer_cert_digest;
6673 mbedtls_md_type_t const peer_cert_digest_type =
6674 ssl->session->peer_cert_digest_type;
6675 mbedtls_md_info_t const * const digest_info =
6676 mbedtls_md_info_from_type( peer_cert_digest_type );
6677 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6678 size_t digest_len;
6679
6680 if( peer_cert_digest == NULL || digest_info == NULL )
6681 return( -1 );
6682
6683 digest_len = mbedtls_md_get_size( digest_info );
6684 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6685 return( -1 );
6686
6687 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6688 if( ret != 0 )
6689 return( -1 );
6690
6691 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6692}
6693#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6694#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6695
6696/*
6697 * Once the certificate message is read, parse it into a cert chain and
6698 * perform basic checks, but leave actual verification to the caller
6699 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006700MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006701static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6702 mbedtls_x509_crt *chain )
6703{
6704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6705#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6706 int crt_cnt=0;
6707#endif
6708 size_t i, n;
6709 uint8_t alert;
6710
6711 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6712 {
6713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6715 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6716 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6717 }
6718
6719 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6720 {
6721 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6722 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6723 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6724 }
6725
6726 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6727 {
6728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6729 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6730 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6731 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6732 }
6733
6734 i = mbedtls_ssl_hs_hdr_len( ssl );
6735
6736 /*
6737 * Same message structure as in mbedtls_ssl_write_certificate()
6738 */
6739 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6740
6741 if( ssl->in_msg[i] != 0 ||
6742 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6746 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6747 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6748 }
6749
6750 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6751 i += 3;
6752
6753 /* Iterate through and parse the CRTs in the provided chain. */
6754 while( i < ssl->in_hslen )
6755 {
6756 /* Check that there's room for the next CRT's length fields. */
6757 if ( i + 3 > ssl->in_hslen ) {
6758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6759 mbedtls_ssl_send_alert_message( ssl,
6760 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6761 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6762 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6763 }
6764 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6765 * anything beyond 2**16 ~ 64K. */
6766 if( ssl->in_msg[i] != 0 )
6767 {
6768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6769 mbedtls_ssl_send_alert_message( ssl,
6770 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6771 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6772 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6773 }
6774
6775 /* Read length of the next CRT in the chain. */
6776 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6777 | (unsigned int) ssl->in_msg[i + 2];
6778 i += 3;
6779
6780 if( n < 128 || i + n > ssl->in_hslen )
6781 {
6782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6783 mbedtls_ssl_send_alert_message( ssl,
6784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6785 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6786 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6787 }
6788
6789 /* Check if we're handling the first CRT in the chain. */
6790#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6791 if( crt_cnt++ == 0 &&
6792 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6793 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6794 {
6795 /* During client-side renegotiation, check that the server's
6796 * end-CRTs hasn't changed compared to the initial handshake,
6797 * mitigating the triple handshake attack. On success, reuse
6798 * the original end-CRT instead of parsing it again. */
6799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6800 if( ssl_check_peer_crt_unchanged( ssl,
6801 &ssl->in_msg[i],
6802 n ) != 0 )
6803 {
6804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6805 mbedtls_ssl_send_alert_message( ssl,
6806 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6807 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6808 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6809 }
6810
6811 /* Now we can safely free the original chain. */
6812 ssl_clear_peer_cert( ssl->session );
6813 }
6814#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6815
6816 /* Parse the next certificate in the chain. */
6817#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6818 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6819#else
6820 /* If we don't need to store the CRT chain permanently, parse
6821 * it in-place from the input buffer instead of making a copy. */
6822 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6823#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6824 switch( ret )
6825 {
6826 case 0: /*ok*/
6827 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6828 /* Ignore certificate with an unknown algorithm: maybe a
6829 prior certificate was already trusted. */
6830 break;
6831
6832 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6833 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6834 goto crt_parse_der_failed;
6835
6836 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 goto crt_parse_der_failed;
6839
6840 default:
6841 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6842 crt_parse_der_failed:
6843 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6844 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6845 return( ret );
6846 }
6847
6848 i += n;
6849 }
6850
6851 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6852 return( 0 );
6853}
6854
6855#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006856MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006857static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6858{
6859 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6860 return( -1 );
6861
6862 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6863 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6864 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6865 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6866 {
Ronald Cron19385882022-06-15 16:26:13 +02006867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006868 return( 0 );
6869 }
6870 return( -1 );
6871}
6872#endif /* MBEDTLS_SSL_SRV_C */
6873
6874/* Check if a certificate message is expected.
6875 * Return either
6876 * - SSL_CERTIFICATE_EXPECTED, or
6877 * - SSL_CERTIFICATE_SKIP
6878 * indicating whether a Certificate message is expected or not.
6879 */
6880#define SSL_CERTIFICATE_EXPECTED 0
6881#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006882MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006883static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6884 int authmode )
6885{
6886 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6887 ssl->handshake->ciphersuite_info;
6888
6889 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6890 return( SSL_CERTIFICATE_SKIP );
6891
6892#if defined(MBEDTLS_SSL_SRV_C)
6893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6894 {
6895 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6896 return( SSL_CERTIFICATE_SKIP );
6897
6898 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6899 {
6900 ssl->session_negotiate->verify_result =
6901 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6902 return( SSL_CERTIFICATE_SKIP );
6903 }
6904 }
6905#else
6906 ((void) authmode);
6907#endif /* MBEDTLS_SSL_SRV_C */
6908
6909 return( SSL_CERTIFICATE_EXPECTED );
6910}
6911
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006912MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006913static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6914 int authmode,
6915 mbedtls_x509_crt *chain,
6916 void *rs_ctx )
6917{
6918 int ret = 0;
6919 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6920 ssl->handshake->ciphersuite_info;
6921 int have_ca_chain = 0;
6922
6923 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6924 void *p_vrfy;
6925
6926 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6927 return( 0 );
6928
6929 if( ssl->f_vrfy != NULL )
6930 {
6931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6932 f_vrfy = ssl->f_vrfy;
6933 p_vrfy = ssl->p_vrfy;
6934 }
6935 else
6936 {
6937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6938 f_vrfy = ssl->conf->f_vrfy;
6939 p_vrfy = ssl->conf->p_vrfy;
6940 }
6941
6942 /*
6943 * Main check: verify certificate
6944 */
6945#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6946 if( ssl->conf->f_ca_cb != NULL )
6947 {
6948 ((void) rs_ctx);
6949 have_ca_chain = 1;
6950
6951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6952 ret = mbedtls_x509_crt_verify_with_ca_cb(
6953 chain,
6954 ssl->conf->f_ca_cb,
6955 ssl->conf->p_ca_cb,
6956 ssl->conf->cert_profile,
6957 ssl->hostname,
6958 &ssl->session_negotiate->verify_result,
6959 f_vrfy, p_vrfy );
6960 }
6961 else
6962#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6963 {
6964 mbedtls_x509_crt *ca_chain;
6965 mbedtls_x509_crl *ca_crl;
6966
6967#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6968 if( ssl->handshake->sni_ca_chain != NULL )
6969 {
6970 ca_chain = ssl->handshake->sni_ca_chain;
6971 ca_crl = ssl->handshake->sni_ca_crl;
6972 }
6973 else
6974#endif
6975 {
6976 ca_chain = ssl->conf->ca_chain;
6977 ca_crl = ssl->conf->ca_crl;
6978 }
6979
6980 if( ca_chain != NULL )
6981 have_ca_chain = 1;
6982
6983 ret = mbedtls_x509_crt_verify_restartable(
6984 chain,
6985 ca_chain, ca_crl,
6986 ssl->conf->cert_profile,
6987 ssl->hostname,
6988 &ssl->session_negotiate->verify_result,
6989 f_vrfy, p_vrfy, rs_ctx );
6990 }
6991
6992 if( ret != 0 )
6993 {
6994 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6995 }
6996
6997#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6998 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6999 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7000#endif
7001
7002 /*
7003 * Secondary checks: always done, but change 'ret' only if it was 0
7004 */
7005
7006#if defined(MBEDTLS_ECP_C)
7007 {
7008 const mbedtls_pk_context *pk = &chain->pk;
7009
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007010 /* If certificate uses an EC key, make sure the curve is OK.
7011 * This is a public key, so it can't be opaque, so can_do() is a good
7012 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007013 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007014 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007015 /* and in the unlikely case the above assumption no longer holds
7016 * we are making sure that pk_ec() here does not return a NULL
7017 */
7018 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7019 if( ec == NULL )
7020 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7023 }
Jerry Yud9526692022-02-17 14:23:47 +08007024
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007025 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7026 {
7027 ssl->session_negotiate->verify_result |=
7028 MBEDTLS_X509_BADCERT_BAD_KEY;
7029
7030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7031 if( ret == 0 )
7032 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7033 }
Jerry Yud9526692022-02-17 14:23:47 +08007034 }
7035 }
7036#endif /* MBEDTLS_ECP_C */
7037
7038 if( mbedtls_ssl_check_cert_usage( chain,
7039 ciphersuite_info,
7040 ! ssl->conf->endpoint,
7041 &ssl->session_negotiate->verify_result ) != 0 )
7042 {
7043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7044 if( ret == 0 )
7045 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7046 }
7047
7048 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7049 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7050 * with details encoded in the verification flags. All other kinds
7051 * of error codes, including those from the user provided f_vrfy
7052 * functions, are treated as fatal and lead to a failure of
7053 * ssl_parse_certificate even if verification was optional. */
7054 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7055 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7056 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7057 {
7058 ret = 0;
7059 }
7060
7061 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7062 {
7063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7064 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7065 }
7066
7067 if( ret != 0 )
7068 {
7069 uint8_t alert;
7070
7071 /* The certificate may have been rejected for several reasons.
7072 Pick one and send the corresponding alert. Which alert to send
7073 may be a subject of debate in some cases. */
7074 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7075 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7076 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7077 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7078 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7079 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7080 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7081 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7082 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7083 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7084 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7085 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7086 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7087 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7088 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7089 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7090 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7091 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7092 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7093 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7094 else
7095 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7096 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7097 alert );
7098 }
7099
7100#if defined(MBEDTLS_DEBUG_C)
7101 if( ssl->session_negotiate->verify_result != 0 )
7102 {
7103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7104 (unsigned int) ssl->session_negotiate->verify_result ) );
7105 }
7106 else
7107 {
7108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7109 }
7110#endif /* MBEDTLS_DEBUG_C */
7111
7112 return( ret );
7113}
7114
7115#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007116MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007117static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7118 unsigned char *start, size_t len )
7119{
7120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7121 /* Remember digest of the peer's end-CRT. */
7122 ssl->session_negotiate->peer_cert_digest =
7123 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7124 if( ssl->session_negotiate->peer_cert_digest == NULL )
7125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7127 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7128 mbedtls_ssl_send_alert_message( ssl,
7129 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7130 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7131
7132 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7133 }
7134
7135 ret = mbedtls_md( mbedtls_md_info_from_type(
7136 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7137 start, len,
7138 ssl->session_negotiate->peer_cert_digest );
7139
7140 ssl->session_negotiate->peer_cert_digest_type =
7141 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7142 ssl->session_negotiate->peer_cert_digest_len =
7143 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7144
7145 return( ret );
7146}
7147
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007148MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007149static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7150 unsigned char *start, size_t len )
7151{
7152 unsigned char *end = start + len;
7153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7154
7155 /* Make a copy of the peer's raw public key. */
7156 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7157 ret = mbedtls_pk_parse_subpubkey( &start, end,
7158 &ssl->handshake->peer_pubkey );
7159 if( ret != 0 )
7160 {
7161 /* We should have parsed the public key before. */
7162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7163 }
7164
7165 return( 0 );
7166}
7167#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7168
7169int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7170{
7171 int ret = 0;
7172 int crt_expected;
7173#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7174 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7175 ? ssl->handshake->sni_authmode
7176 : ssl->conf->authmode;
7177#else
7178 const int authmode = ssl->conf->authmode;
7179#endif
7180 void *rs_ctx = NULL;
7181 mbedtls_x509_crt *chain = NULL;
7182
7183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7184
7185 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7186 if( crt_expected == SSL_CERTIFICATE_SKIP )
7187 {
7188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7189 goto exit;
7190 }
7191
7192#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7193 if( ssl->handshake->ecrs_enabled &&
7194 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7195 {
7196 chain = ssl->handshake->ecrs_peer_cert;
7197 ssl->handshake->ecrs_peer_cert = NULL;
7198 goto crt_verify;
7199 }
7200#endif
7201
7202 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7203 {
7204 /* mbedtls_ssl_read_record may have sent an alert already. We
7205 let it decide whether to alert. */
7206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7207 goto exit;
7208 }
7209
7210#if defined(MBEDTLS_SSL_SRV_C)
7211 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7212 {
7213 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7214
7215 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7216 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7217
7218 goto exit;
7219 }
7220#endif /* MBEDTLS_SSL_SRV_C */
7221
7222 /* Clear existing peer CRT structure in case we tried to
7223 * reuse a session but it failed, and allocate a new one. */
7224 ssl_clear_peer_cert( ssl->session_negotiate );
7225
7226 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7227 if( chain == NULL )
7228 {
7229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7230 sizeof( mbedtls_x509_crt ) ) );
7231 mbedtls_ssl_send_alert_message( ssl,
7232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7233 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7234
7235 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7236 goto exit;
7237 }
7238 mbedtls_x509_crt_init( chain );
7239
7240 ret = ssl_parse_certificate_chain( ssl, chain );
7241 if( ret != 0 )
7242 goto exit;
7243
7244#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7245 if( ssl->handshake->ecrs_enabled)
7246 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7247
7248crt_verify:
7249 if( ssl->handshake->ecrs_enabled)
7250 rs_ctx = &ssl->handshake->ecrs_ctx;
7251#endif
7252
7253 ret = ssl_parse_certificate_verify( ssl, authmode,
7254 chain, rs_ctx );
7255 if( ret != 0 )
7256 goto exit;
7257
7258#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7259 {
7260 unsigned char *crt_start, *pk_start;
7261 size_t crt_len, pk_len;
7262
7263 /* We parse the CRT chain without copying, so
7264 * these pointers point into the input buffer,
7265 * and are hence still valid after freeing the
7266 * CRT chain. */
7267
7268 crt_start = chain->raw.p;
7269 crt_len = chain->raw.len;
7270
7271 pk_start = chain->pk_raw.p;
7272 pk_len = chain->pk_raw.len;
7273
7274 /* Free the CRT structures before computing
7275 * digest and copying the peer's public key. */
7276 mbedtls_x509_crt_free( chain );
7277 mbedtls_free( chain );
7278 chain = NULL;
7279
7280 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7281 if( ret != 0 )
7282 goto exit;
7283
7284 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7285 if( ret != 0 )
7286 goto exit;
7287 }
7288#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7289 /* Pass ownership to session structure. */
7290 ssl->session_negotiate->peer_cert = chain;
7291 chain = NULL;
7292#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7293
7294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7295
7296exit:
7297
7298 if( ret == 0 )
7299 ssl->state++;
7300
7301#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7302 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7303 {
7304 ssl->handshake->ecrs_peer_cert = chain;
7305 chain = NULL;
7306 }
7307#endif
7308
7309 if( chain != NULL )
7310 {
7311 mbedtls_x509_crt_free( chain );
7312 mbedtls_free( chain );
7313 }
7314
7315 return( ret );
7316}
7317#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7318
Andrzej Kurek25f27152022-08-17 16:09:31 -04007319#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007320static void ssl_calc_finished_tls_sha256(
7321 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7322{
7323 int len = 12;
7324 const char *sender;
7325 unsigned char padbuf[32];
7326#if defined(MBEDTLS_USE_PSA_CRYPTO)
7327 size_t hash_size;
7328 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7329 psa_status_t status;
7330#else
7331 mbedtls_sha256_context sha256;
7332#endif
7333
7334 mbedtls_ssl_session *session = ssl->session_negotiate;
7335 if( !session )
7336 session = ssl->session;
7337
7338 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7339 ? "client finished"
7340 : "server finished";
7341
7342#if defined(MBEDTLS_USE_PSA_CRYPTO)
7343 sha256_psa = psa_hash_operation_init();
7344
7345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7346
7347 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7348 if( status != PSA_SUCCESS )
7349 {
7350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7351 return;
7352 }
7353
7354 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7355 if( status != PSA_SUCCESS )
7356 {
7357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7358 return;
7359 }
7360 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7361#else
7362
7363 mbedtls_sha256_init( &sha256 );
7364
7365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7366
7367 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7368
7369 /*
7370 * TLSv1.2:
7371 * hash = PRF( master, finished_label,
7372 * Hash( handshake ) )[0.11]
7373 */
7374
7375#if !defined(MBEDTLS_SHA256_ALT)
7376 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7377 sha256.state, sizeof( sha256.state ) );
7378#endif
7379
7380 mbedtls_sha256_finish( &sha256, padbuf );
7381 mbedtls_sha256_free( &sha256 );
7382#endif /* MBEDTLS_USE_PSA_CRYPTO */
7383
7384 ssl->handshake->tls_prf( session->master, 48, sender,
7385 padbuf, 32, buf, len );
7386
7387 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7388
7389 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7390
7391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7392}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007393#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007394
Jerry Yub7ba49e2022-02-17 14:25:53 +08007395
Andrzej Kurek25f27152022-08-17 16:09:31 -04007396#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007397static void ssl_calc_finished_tls_sha384(
7398 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7399{
7400 int len = 12;
7401 const char *sender;
7402 unsigned char padbuf[48];
7403#if defined(MBEDTLS_USE_PSA_CRYPTO)
7404 size_t hash_size;
7405 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7406 psa_status_t status;
7407#else
7408 mbedtls_sha512_context sha512;
7409#endif
7410
7411 mbedtls_ssl_session *session = ssl->session_negotiate;
7412 if( !session )
7413 session = ssl->session;
7414
7415 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7416 ? "client finished"
7417 : "server finished";
7418
7419#if defined(MBEDTLS_USE_PSA_CRYPTO)
7420 sha384_psa = psa_hash_operation_init();
7421
7422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7423
7424 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7425 if( status != PSA_SUCCESS )
7426 {
7427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7428 return;
7429 }
7430
7431 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7432 if( status != PSA_SUCCESS )
7433 {
7434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7435 return;
7436 }
7437 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7438#else
7439 mbedtls_sha512_init( &sha512 );
7440
7441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7442
Andrzej Kureka242e832022-08-11 10:03:14 -04007443 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007444
7445 /*
7446 * TLSv1.2:
7447 * hash = PRF( master, finished_label,
7448 * Hash( handshake ) )[0.11]
7449 */
7450
7451#if !defined(MBEDTLS_SHA512_ALT)
7452 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7453 sha512.state, sizeof( sha512.state ) );
7454#endif
7455 mbedtls_sha512_finish( &sha512, padbuf );
7456
7457 mbedtls_sha512_free( &sha512 );
7458#endif
7459
7460 ssl->handshake->tls_prf( session->master, 48, sender,
7461 padbuf, 48, buf, len );
7462
7463 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7464
7465 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7466
7467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7468}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007469#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007470
Jerry Yuaef00152022-02-17 14:27:31 +08007471void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7472{
7473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7474
7475 /*
7476 * Free our handshake params
7477 */
7478 mbedtls_ssl_handshake_free( ssl );
7479 mbedtls_free( ssl->handshake );
7480 ssl->handshake = NULL;
7481
7482 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007483 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007484 */
7485 if( ssl->transform )
7486 {
7487 mbedtls_ssl_transform_free( ssl->transform );
7488 mbedtls_free( ssl->transform );
7489 }
7490 ssl->transform = ssl->transform_negotiate;
7491 ssl->transform_negotiate = NULL;
7492
7493 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7494}
7495
Jerry Yu2a9fff52022-02-17 14:28:51 +08007496void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7497{
7498 int resume = ssl->handshake->resume;
7499
7500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7501
7502#if defined(MBEDTLS_SSL_RENEGOTIATION)
7503 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7504 {
7505 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7506 ssl->renego_records_seen = 0;
7507 }
7508#endif
7509
7510 /*
7511 * Free the previous session and switch in the current one
7512 */
7513 if( ssl->session )
7514 {
7515#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7516 /* RFC 7366 3.1: keep the EtM state */
7517 ssl->session_negotiate->encrypt_then_mac =
7518 ssl->session->encrypt_then_mac;
7519#endif
7520
7521 mbedtls_ssl_session_free( ssl->session );
7522 mbedtls_free( ssl->session );
7523 }
7524 ssl->session = ssl->session_negotiate;
7525 ssl->session_negotiate = NULL;
7526
7527 /*
7528 * Add cache entry
7529 */
7530 if( ssl->conf->f_set_cache != NULL &&
7531 ssl->session->id_len != 0 &&
7532 resume == 0 )
7533 {
7534 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7535 ssl->session->id,
7536 ssl->session->id_len,
7537 ssl->session ) != 0 )
7538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7539 }
7540
7541#if defined(MBEDTLS_SSL_PROTO_DTLS)
7542 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7543 ssl->handshake->flight != NULL )
7544 {
7545 /* Cancel handshake timer */
7546 mbedtls_ssl_set_timer( ssl, 0 );
7547
7548 /* Keep last flight around in case we need to resend it:
7549 * we need the handshake and transform structures for that */
7550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7551 }
7552 else
7553#endif
7554 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7555
7556 ssl->state++;
7557
7558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7559}
7560
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007561int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7562{
7563 int ret, hash_len;
7564
7565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7566
7567 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7568
7569 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7570
7571 /*
7572 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7573 * may define some other value. Currently (early 2016), no defined
7574 * ciphersuite does this (and this is unlikely to change as activity has
7575 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7576 */
7577 hash_len = 12;
7578
7579#if defined(MBEDTLS_SSL_RENEGOTIATION)
7580 ssl->verify_data_len = hash_len;
7581 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7582#endif
7583
7584 ssl->out_msglen = 4 + hash_len;
7585 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7586 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7587
7588 /*
7589 * In case of session resuming, invert the client and server
7590 * ChangeCipherSpec messages order.
7591 */
7592 if( ssl->handshake->resume != 0 )
7593 {
7594#if defined(MBEDTLS_SSL_CLI_C)
7595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7596 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7597#endif
7598#if defined(MBEDTLS_SSL_SRV_C)
7599 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7600 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7601#endif
7602 }
7603 else
7604 ssl->state++;
7605
7606 /*
7607 * Switch to our negotiated transform and session parameters for outbound
7608 * data.
7609 */
7610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7611
7612#if defined(MBEDTLS_SSL_PROTO_DTLS)
7613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7614 {
7615 unsigned char i;
7616
7617 /* Remember current epoch settings for resending */
7618 ssl->handshake->alt_transform_out = ssl->transform_out;
7619 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7620 sizeof( ssl->handshake->alt_out_ctr ) );
7621
7622 /* Set sequence_number to zero */
7623 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7624
7625
7626 /* Increment epoch */
7627 for( i = 2; i > 0; i-- )
7628 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7629 break;
7630
7631 /* The loop goes to its end iff the counter is wrapping */
7632 if( i == 0 )
7633 {
7634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7635 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7636 }
7637 }
7638 else
7639#endif /* MBEDTLS_SSL_PROTO_DTLS */
7640 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7641
7642 ssl->transform_out = ssl->transform_negotiate;
7643 ssl->session_out = ssl->session_negotiate;
7644
7645#if defined(MBEDTLS_SSL_PROTO_DTLS)
7646 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7647 mbedtls_ssl_send_flight_completed( ssl );
7648#endif
7649
7650 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7651 {
7652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7653 return( ret );
7654 }
7655
7656#if defined(MBEDTLS_SSL_PROTO_DTLS)
7657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7658 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7659 {
7660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7661 return( ret );
7662 }
7663#endif
7664
7665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7666
7667 return( 0 );
7668}
7669
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007670#define SSL_MAX_HASH_LEN 12
7671
7672int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7673{
7674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7675 unsigned int hash_len = 12;
7676 unsigned char buf[SSL_MAX_HASH_LEN];
7677
7678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7679
7680 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7681
7682 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7683 {
7684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7685 goto exit;
7686 }
7687
7688 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7689 {
7690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7692 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7693 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7694 goto exit;
7695 }
7696
7697 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7698 {
7699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7700 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7701 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7702 goto exit;
7703 }
7704
7705 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7706 {
7707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7709 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7710 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7711 goto exit;
7712 }
7713
7714 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7715 buf, hash_len ) != 0 )
7716 {
7717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7718 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7719 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7720 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7721 goto exit;
7722 }
7723
7724#if defined(MBEDTLS_SSL_RENEGOTIATION)
7725 ssl->verify_data_len = hash_len;
7726 memcpy( ssl->peer_verify_data, buf, hash_len );
7727#endif
7728
7729 if( ssl->handshake->resume != 0 )
7730 {
7731#if defined(MBEDTLS_SSL_CLI_C)
7732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7733 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7734#endif
7735#if defined(MBEDTLS_SSL_SRV_C)
7736 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7737 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7738#endif
7739 }
7740 else
7741 ssl->state++;
7742
7743#if defined(MBEDTLS_SSL_PROTO_DTLS)
7744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7745 mbedtls_ssl_recv_flight_completed( ssl );
7746#endif
7747
7748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7749
7750exit:
7751 mbedtls_platform_zeroize( buf, hash_len );
7752 return( ret );
7753}
7754
Jerry Yu392112c2022-02-17 14:34:10 +08007755#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7756/*
7757 * Helper to get TLS 1.2 PRF from ciphersuite
7758 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7759 */
7760static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7761{
Jerry Yu392112c2022-02-17 14:34:10 +08007762 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007763 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7764#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007765 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007766 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007767 else
Jerry Yu392112c2022-02-17 14:34:10 +08007768#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007769#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7770 {
7771 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7772 return( tls_prf_sha256 );
7773 }
7774#endif
7775#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7776 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7777 (void) ciphersuite_info;
7778#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007779
Andrzej Kurek894edde2022-09-29 06:31:14 -04007780 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007781}
7782#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007783
7784static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7785{
7786 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007787#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007788 if( tls_prf == tls_prf_sha384 )
7789 {
7790 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7791 }
7792 else
7793#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007794#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007795 if( tls_prf == tls_prf_sha256 )
7796 {
7797 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7798 }
7799 else
7800#endif
7801 return( MBEDTLS_SSL_TLS_PRF_NONE );
7802}
7803
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007804/*
7805 * Populate a transform structure with session keys and all the other
7806 * necessary information.
7807 *
7808 * Parameters:
7809 * - [in/out]: transform: structure to populate
7810 * [in] must be just initialised with mbedtls_ssl_transform_init()
7811 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7812 * - [in] ciphersuite
7813 * - [in] master
7814 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007815 * - [in] tls_prf: pointer to PRF to use for key derivation
7816 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007817 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007818 * - [in] endpoint: client or server
7819 * - [in] ssl: used for:
7820 * - ssl->conf->{f,p}_export_keys
7821 * [in] optionally used for:
7822 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7823 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007824MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007825static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7826 int ciphersuite,
7827 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007828#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007829 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007830#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007831 ssl_tls_prf_t tls_prf,
7832 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007833 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007834 unsigned endpoint,
7835 const mbedtls_ssl_context *ssl )
7836{
7837 int ret = 0;
7838 unsigned char keyblk[256];
7839 unsigned char *key1;
7840 unsigned char *key2;
7841 unsigned char *mac_enc;
7842 unsigned char *mac_dec;
7843 size_t mac_key_len = 0;
7844 size_t iv_copy_len;
7845 size_t keylen;
7846 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007847 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007848#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007849 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007850 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007851#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007852
7853#if defined(MBEDTLS_USE_PSA_CRYPTO)
7854 psa_key_type_t key_type;
7855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7856 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007857 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007858 size_t key_bits;
7859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7860#endif
7861
7862#if !defined(MBEDTLS_DEBUG_C) && \
7863 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7864 if( ssl->f_export_keys == NULL )
7865 {
7866 ssl = NULL; /* make sure we don't use it except for these cases */
7867 (void) ssl;
7868 }
7869#endif
7870
7871 /*
7872 * Some data just needs copying into the structure
7873 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007874#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007875 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007876#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007877 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007878
7879#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7880 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7881#endif
7882
7883#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007884 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007885 {
7886 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7887 * generation separate. This should never happen. */
7888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7889 }
7890#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7891
7892 /*
7893 * Get various info structures
7894 */
7895 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7896 if( ciphersuite_info == NULL )
7897 {
7898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7899 ciphersuite ) );
7900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7901 }
7902
Neil Armstrongab555e02022-04-04 11:07:59 +02007903 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007904#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007905 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007906#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007907 ciphersuite_info );
7908
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007909 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7910 transform->taglen =
7911 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7912
7913#if defined(MBEDTLS_USE_PSA_CRYPTO)
7914 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7915 transform->taglen,
7916 &alg,
7917 &key_type,
7918 &key_bits ) ) != PSA_SUCCESS )
7919 {
7920 ret = psa_ssl_status_to_mbedtls( status );
7921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7922 goto end;
7923 }
7924#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007925 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7926 if( cipher_info == NULL )
7927 {
7928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7929 ciphersuite_info->cipher ) );
7930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7931 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007933
Neil Armstronge4512952022-03-08 09:08:22 +01007934#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007935 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007936 if( mac_alg == 0 )
7937 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007939 (unsigned) ciphersuite_info->mac ) );
7940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7941 }
7942#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007943 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7944 if( md_info == NULL )
7945 {
7946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7947 (unsigned) ciphersuite_info->mac ) );
7948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7949 }
Neil Armstronge4512952022-03-08 09:08:22 +01007950#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007951
7952#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7953 /* Copy own and peer's CID if the use of the CID
7954 * extension has been negotiated. */
7955 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7956 {
7957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7958
7959 transform->in_cid_len = ssl->own_cid_len;
7960 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7961 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7962 transform->in_cid_len );
7963
7964 transform->out_cid_len = ssl->handshake->peer_cid_len;
7965 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7966 ssl->handshake->peer_cid_len );
7967 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7968 transform->out_cid_len );
7969 }
7970#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7971
7972 /*
7973 * Compute key block using the PRF
7974 */
7975 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7976 if( ret != 0 )
7977 {
7978 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7979 return( ret );
7980 }
7981
7982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7983 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7984 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7985 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7986 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7987
7988 /*
7989 * Determine the appropriate key, IV and MAC length.
7990 */
7991
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007992#if defined(MBEDTLS_USE_PSA_CRYPTO)
7993 keylen = PSA_BITS_TO_BYTES(key_bits);
7994#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007995 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007996#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007997
7998#if defined(MBEDTLS_GCM_C) || \
7999 defined(MBEDTLS_CCM_C) || \
8000 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008001 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008002 {
8003 size_t explicit_ivlen;
8004
8005 transform->maclen = 0;
8006 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008007
8008 /* All modes haves 96-bit IVs, but the length of the static parts vary
8009 * with mode and version:
8010 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8011 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8012 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8013 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8014 * sequence number).
8015 */
8016 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008017
8018 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008019#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008020 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008021#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008022 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8023 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008024#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008025
8026 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008027 transform->fixed_ivlen = 12;
8028 else
8029 transform->fixed_ivlen = 4;
8030
8031 /* Minimum length of encrypted record */
8032 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8033 transform->minlen = explicit_ivlen + transform->taglen;
8034 }
8035 else
8036#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8037#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008038 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8039 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8040 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008041 {
Neil Armstronge4512952022-03-08 09:08:22 +01008042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008043 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008044#else
8045 size_t block_size = cipher_info->block_size;
8046#endif /* MBEDTLS_USE_PSA_CRYPTO */
8047
8048#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008049 /* Get MAC length */
8050 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8051#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008052 /* Initialize HMAC contexts */
8053 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8054 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8055 {
8056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8057 goto end;
8058 }
8059
8060 /* Get MAC length */
8061 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008062#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008063 transform->maclen = mac_key_len;
8064
8065 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008067 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008068#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008069 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008070#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008071
8072 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008073 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008074 transform->minlen = transform->maclen;
8075 else
8076 {
8077 /*
8078 * GenericBlockCipher:
8079 * 1. if EtM is in use: one block plus MAC
8080 * otherwise: * first multiple of blocklen greater than maclen
8081 * 2. IV
8082 */
8083#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008084 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008085 {
8086 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008087 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008088 }
8089 else
8090#endif
8091 {
8092 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008093 + block_size
8094 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008095 }
8096
Glenn Strauss07c64162022-03-14 12:34:51 -04008097 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008098 {
8099 transform->minlen += transform->ivlen;
8100 }
8101 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008102 {
8103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8104 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8105 goto end;
8106 }
8107 }
8108 }
8109 else
8110#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8111 {
8112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8114 }
8115
8116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8117 (unsigned) keylen,
8118 (unsigned) transform->minlen,
8119 (unsigned) transform->ivlen,
8120 (unsigned) transform->maclen ) );
8121
8122 /*
8123 * Finally setup the cipher contexts, IVs and MAC secrets.
8124 */
8125#if defined(MBEDTLS_SSL_CLI_C)
8126 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8127 {
8128 key1 = keyblk + mac_key_len * 2;
8129 key2 = keyblk + mac_key_len * 2 + keylen;
8130
8131 mac_enc = keyblk;
8132 mac_dec = keyblk + mac_key_len;
8133
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008134 iv_copy_len = ( transform->fixed_ivlen ) ?
8135 transform->fixed_ivlen : transform->ivlen;
8136 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8137 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8138 iv_copy_len );
8139 }
8140 else
8141#endif /* MBEDTLS_SSL_CLI_C */
8142#if defined(MBEDTLS_SSL_SRV_C)
8143 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8144 {
8145 key1 = keyblk + mac_key_len * 2 + keylen;
8146 key2 = keyblk + mac_key_len * 2;
8147
8148 mac_enc = keyblk + mac_key_len;
8149 mac_dec = keyblk;
8150
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008151 iv_copy_len = ( transform->fixed_ivlen ) ?
8152 transform->fixed_ivlen : transform->ivlen;
8153 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8154 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8155 iv_copy_len );
8156 }
8157 else
8158#endif /* MBEDTLS_SSL_SRV_C */
8159 {
8160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8161 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8162 goto end;
8163 }
8164
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008165 if( ssl != NULL && ssl->f_export_keys != NULL )
8166 {
8167 ssl->f_export_keys( ssl->p_export_keys,
8168 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8169 master, 48,
8170 randbytes + 32,
8171 randbytes,
8172 tls_prf_get_type( tls_prf ) );
8173 }
8174
8175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176 transform->psa_alg = alg;
8177
8178 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8179 {
8180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8181 psa_set_key_algorithm( &attributes, alg );
8182 psa_set_key_type( &attributes, key_type );
8183
8184 if( ( status = psa_import_key( &attributes,
8185 key1,
8186 PSA_BITS_TO_BYTES( key_bits ),
8187 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8188 {
8189 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8190 ret = psa_ssl_status_to_mbedtls( status );
8191 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8192 goto end;
8193 }
8194
8195 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8196
8197 if( ( status = psa_import_key( &attributes,
8198 key2,
8199 PSA_BITS_TO_BYTES( key_bits ),
8200 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8201 {
8202 ret = psa_ssl_status_to_mbedtls( status );
8203 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8204 goto end;
8205 }
8206 }
8207#else
8208 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8209 cipher_info ) ) != 0 )
8210 {
8211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8212 goto end;
8213 }
8214
8215 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8216 cipher_info ) ) != 0 )
8217 {
8218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8219 goto end;
8220 }
8221
8222 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8223 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8224 MBEDTLS_ENCRYPT ) ) != 0 )
8225 {
8226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8227 goto end;
8228 }
8229
8230 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8231 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8232 MBEDTLS_DECRYPT ) ) != 0 )
8233 {
8234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8235 goto end;
8236 }
8237
8238#if defined(MBEDTLS_CIPHER_MODE_CBC)
8239 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8240 {
8241 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8242 MBEDTLS_PADDING_NONE ) ) != 0 )
8243 {
8244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8245 goto end;
8246 }
8247
8248 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8249 MBEDTLS_PADDING_NONE ) ) != 0 )
8250 {
8251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8252 goto end;
8253 }
8254 }
8255#endif /* MBEDTLS_CIPHER_MODE_CBC */
8256#endif /* MBEDTLS_USE_PSA_CRYPTO */
8257
Neil Armstrong29c0c042022-03-17 17:47:28 +01008258#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8259 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8260 For AEAD-based ciphersuites, there is nothing to do here. */
8261 if( mac_key_len != 0 )
8262 {
8263#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008264 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008265
8266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008267 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008268 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8269
8270 if( ( status = psa_import_key( &attributes,
8271 mac_enc, mac_key_len,
8272 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8273 {
8274 ret = psa_ssl_status_to_mbedtls( status );
8275 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8276 goto end;
8277 }
8278
Ronald Cronfb39f152022-03-25 14:36:28 +01008279 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008280 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8281#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8282 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8283#endif
8284 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008285 /* mbedtls_ct_hmac() requires the key to be exportable */
8286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8287 PSA_KEY_USAGE_VERIFY_HASH );
8288 else
8289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8290
8291 if( ( status = psa_import_key( &attributes,
8292 mac_dec, mac_key_len,
8293 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8294 {
8295 ret = psa_ssl_status_to_mbedtls( status );
8296 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8297 goto end;
8298 }
8299#else
8300 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8301 if( ret != 0 )
8302 goto end;
8303 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8304 if( ret != 0 )
8305 goto end;
8306#endif /* MBEDTLS_USE_PSA_CRYPTO */
8307 }
8308#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8309
8310 ((void) mac_dec);
8311 ((void) mac_enc);
8312
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313end:
8314 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8315 return( ret );
8316}
8317
Jerry Yuee40f9d2022-02-17 14:55:16 +08008318#if defined(MBEDTLS_USE_PSA_CRYPTO)
8319int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8320 unsigned char *hash, size_t *hashlen,
8321 unsigned char *data, size_t data_len,
8322 mbedtls_md_type_t md_alg )
8323{
8324 psa_status_t status;
8325 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008326 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008327
8328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8329
8330 if( ( status = psa_hash_setup( &hash_operation,
8331 hash_alg ) ) != PSA_SUCCESS )
8332 {
8333 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8334 goto exit;
8335 }
8336
8337 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8338 64 ) ) != PSA_SUCCESS )
8339 {
8340 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8341 goto exit;
8342 }
8343
8344 if( ( status = psa_hash_update( &hash_operation,
8345 data, data_len ) ) != PSA_SUCCESS )
8346 {
8347 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8348 goto exit;
8349 }
8350
8351 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8352 hashlen ) ) != PSA_SUCCESS )
8353 {
8354 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8355 goto exit;
8356 }
8357
8358exit:
8359 if( status != PSA_SUCCESS )
8360 {
8361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8362 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8363 switch( status )
8364 {
8365 case PSA_ERROR_NOT_SUPPORTED:
8366 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8367 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8368 case PSA_ERROR_BUFFER_TOO_SMALL:
8369 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8370 case PSA_ERROR_INSUFFICIENT_MEMORY:
8371 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8372 default:
8373 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8374 }
8375 }
8376 return( 0 );
8377}
8378
8379#else
8380
8381int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8382 unsigned char *hash, size_t *hashlen,
8383 unsigned char *data, size_t data_len,
8384 mbedtls_md_type_t md_alg )
8385{
8386 int ret = 0;
8387 mbedtls_md_context_t ctx;
8388 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8389 *hashlen = mbedtls_md_get_size( md_info );
8390
8391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8392
8393 mbedtls_md_init( &ctx );
8394
8395 /*
8396 * digitally-signed struct {
8397 * opaque client_random[32];
8398 * opaque server_random[32];
8399 * ServerDHParams params;
8400 * };
8401 */
8402 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8403 {
8404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8405 goto exit;
8406 }
8407 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8408 {
8409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8410 goto exit;
8411 }
8412 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8413 {
8414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8415 goto exit;
8416 }
8417 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8418 {
8419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8420 goto exit;
8421 }
8422 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8423 {
8424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8425 goto exit;
8426 }
8427
8428exit:
8429 mbedtls_md_free( &ctx );
8430
8431 if( ret != 0 )
8432 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8433 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8434
8435 return( ret );
8436}
8437#endif /* MBEDTLS_USE_PSA_CRYPTO */
8438
Jerry Yud9d91da2022-02-17 14:57:06 +08008439#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8440
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008441/* Find the preferred hash for a given signature algorithm. */
8442unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8443 mbedtls_ssl_context *ssl,
8444 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008445{
Gabor Mezei078e8032022-04-27 21:17:56 +02008446 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008447 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008448
8449 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008450 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008451
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008452 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008453 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008454 unsigned int hash_alg_received =
8455 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8456 received_sig_algs[i] );
8457 unsigned int sig_alg_received =
8458 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8459 received_sig_algs[i] );
8460
8461 if( sig_alg == sig_alg_received )
8462 {
8463#if defined(MBEDTLS_USE_PSA_CRYPTO)
8464 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8465 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008466 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008467 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008468
Neil Armstrong96eceb82022-06-30 18:05:05 +02008469 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8470 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8471 PSA_ALG_ECDSA( psa_hash_alg ),
8472 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008473 continue;
8474
Neil Armstrong96eceb82022-06-30 18:05:05 +02008475 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008476 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8477 PSA_ALG_RSA_PKCS1V15_SIGN(
8478 psa_hash_alg ),
8479 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008480 continue;
8481 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008482#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008483
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008484 return( hash_alg_received );
8485 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008486 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008487
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008488 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008489}
8490
8491#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8492
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008493/* Serialization of TLS 1.2 sessions:
8494 *
8495 * struct {
8496 * uint64 start_time;
8497 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008498 * uint8 session_id_len; // at most 32
8499 * opaque session_id[32];
8500 * opaque master[48]; // fixed length in the standard
8501 * uint32 verify_result;
8502 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8503 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8504 * uint32 ticket_lifetime;
8505 * uint8 mfl_code; // up to 255 according to standard
8506 * uint8 encrypt_then_mac; // 0 or 1
8507 * } serialized_session_tls12;
8508 *
8509 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008510static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008511 unsigned char *buf,
8512 size_t buf_len )
8513{
8514 unsigned char *p = buf;
8515 size_t used = 0;
8516
8517#if defined(MBEDTLS_HAVE_TIME)
8518 uint64_t start;
8519#endif
8520#if defined(MBEDTLS_X509_CRT_PARSE_C)
8521#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8522 size_t cert_len;
8523#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8524#endif /* MBEDTLS_X509_CRT_PARSE_C */
8525
8526 /*
8527 * Time
8528 */
8529#if defined(MBEDTLS_HAVE_TIME)
8530 used += 8;
8531
8532 if( used <= buf_len )
8533 {
8534 start = (uint64_t) session->start;
8535
8536 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8537 p += 8;
8538 }
8539#endif /* MBEDTLS_HAVE_TIME */
8540
8541 /*
8542 * Basic mandatory fields
8543 */
8544 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008545 + 1 /* id_len */
8546 + sizeof( session->id )
8547 + sizeof( session->master )
8548 + 4; /* verify_result */
8549
8550 if( used <= buf_len )
8551 {
8552 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8553 p += 2;
8554
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008555 *p++ = MBEDTLS_BYTE_0( session->id_len );
8556 memcpy( p, session->id, 32 );
8557 p += 32;
8558
8559 memcpy( p, session->master, 48 );
8560 p += 48;
8561
8562 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8563 p += 4;
8564 }
8565
8566 /*
8567 * Peer's end-entity certificate
8568 */
8569#if defined(MBEDTLS_X509_CRT_PARSE_C)
8570#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8571 if( session->peer_cert == NULL )
8572 cert_len = 0;
8573 else
8574 cert_len = session->peer_cert->raw.len;
8575
8576 used += 3 + cert_len;
8577
8578 if( used <= buf_len )
8579 {
8580 *p++ = MBEDTLS_BYTE_2( cert_len );
8581 *p++ = MBEDTLS_BYTE_1( cert_len );
8582 *p++ = MBEDTLS_BYTE_0( cert_len );
8583
8584 if( session->peer_cert != NULL )
8585 {
8586 memcpy( p, session->peer_cert->raw.p, cert_len );
8587 p += cert_len;
8588 }
8589 }
8590#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8591 if( session->peer_cert_digest != NULL )
8592 {
8593 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8594 if( used <= buf_len )
8595 {
8596 *p++ = (unsigned char) session->peer_cert_digest_type;
8597 *p++ = (unsigned char) session->peer_cert_digest_len;
8598 memcpy( p, session->peer_cert_digest,
8599 session->peer_cert_digest_len );
8600 p += session->peer_cert_digest_len;
8601 }
8602 }
8603 else
8604 {
8605 used += 2;
8606 if( used <= buf_len )
8607 {
8608 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8609 *p++ = 0;
8610 }
8611 }
8612#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8613#endif /* MBEDTLS_X509_CRT_PARSE_C */
8614
8615 /*
8616 * Session ticket if any, plus associated data
8617 */
8618#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8619 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8620
8621 if( used <= buf_len )
8622 {
8623 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8624 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8625 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8626
8627 if( session->ticket != NULL )
8628 {
8629 memcpy( p, session->ticket, session->ticket_len );
8630 p += session->ticket_len;
8631 }
8632
8633 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8634 p += 4;
8635 }
8636#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8637
8638 /*
8639 * Misc extension-related info
8640 */
8641#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8642 used += 1;
8643
8644 if( used <= buf_len )
8645 *p++ = session->mfl_code;
8646#endif
8647
8648#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8649 used += 1;
8650
8651 if( used <= buf_len )
8652 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8653#endif
8654
8655 return( used );
8656}
8657
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008658MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008659static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008660 const unsigned char *buf,
8661 size_t len )
8662{
8663#if defined(MBEDTLS_HAVE_TIME)
8664 uint64_t start;
8665#endif
8666#if defined(MBEDTLS_X509_CRT_PARSE_C)
8667#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8668 size_t cert_len;
8669#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8670#endif /* MBEDTLS_X509_CRT_PARSE_C */
8671
8672 const unsigned char *p = buf;
8673 const unsigned char * const end = buf + len;
8674
8675 /*
8676 * Time
8677 */
8678#if defined(MBEDTLS_HAVE_TIME)
8679 if( 8 > (size_t)( end - p ) )
8680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8681
8682 start = ( (uint64_t) p[0] << 56 ) |
8683 ( (uint64_t) p[1] << 48 ) |
8684 ( (uint64_t) p[2] << 40 ) |
8685 ( (uint64_t) p[3] << 32 ) |
8686 ( (uint64_t) p[4] << 24 ) |
8687 ( (uint64_t) p[5] << 16 ) |
8688 ( (uint64_t) p[6] << 8 ) |
8689 ( (uint64_t) p[7] );
8690 p += 8;
8691
8692 session->start = (time_t) start;
8693#endif /* MBEDTLS_HAVE_TIME */
8694
8695 /*
8696 * Basic mandatory fields
8697 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008698 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8700
8701 session->ciphersuite = ( p[0] << 8 ) | p[1];
8702 p += 2;
8703
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008704 session->id_len = *p++;
8705 memcpy( session->id, p, 32 );
8706 p += 32;
8707
8708 memcpy( session->master, p, 48 );
8709 p += 48;
8710
8711 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8712 ( (uint32_t) p[1] << 16 ) |
8713 ( (uint32_t) p[2] << 8 ) |
8714 ( (uint32_t) p[3] );
8715 p += 4;
8716
8717 /* Immediately clear invalid pointer values that have been read, in case
8718 * we exit early before we replaced them with valid ones. */
8719#if defined(MBEDTLS_X509_CRT_PARSE_C)
8720#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8721 session->peer_cert = NULL;
8722#else
8723 session->peer_cert_digest = NULL;
8724#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8725#endif /* MBEDTLS_X509_CRT_PARSE_C */
8726#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8727 session->ticket = NULL;
8728#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8729
8730 /*
8731 * Peer certificate
8732 */
8733#if defined(MBEDTLS_X509_CRT_PARSE_C)
8734#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8735 /* Deserialize CRT from the end of the ticket. */
8736 if( 3 > (size_t)( end - p ) )
8737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8738
8739 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8740 p += 3;
8741
8742 if( cert_len != 0 )
8743 {
8744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8745
8746 if( cert_len > (size_t)( end - p ) )
8747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8748
8749 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8750
8751 if( session->peer_cert == NULL )
8752 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8753
8754 mbedtls_x509_crt_init( session->peer_cert );
8755
8756 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8757 p, cert_len ) ) != 0 )
8758 {
8759 mbedtls_x509_crt_free( session->peer_cert );
8760 mbedtls_free( session->peer_cert );
8761 session->peer_cert = NULL;
8762 return( ret );
8763 }
8764
8765 p += cert_len;
8766 }
8767#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8768 /* Deserialize CRT digest from the end of the ticket. */
8769 if( 2 > (size_t)( end - p ) )
8770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8771
8772 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8773 session->peer_cert_digest_len = (size_t) *p++;
8774
8775 if( session->peer_cert_digest_len != 0 )
8776 {
8777 const mbedtls_md_info_t *md_info =
8778 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8779 if( md_info == NULL )
8780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8781 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8783
8784 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8786
8787 session->peer_cert_digest =
8788 mbedtls_calloc( 1, session->peer_cert_digest_len );
8789 if( session->peer_cert_digest == NULL )
8790 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8791
8792 memcpy( session->peer_cert_digest, p,
8793 session->peer_cert_digest_len );
8794 p += session->peer_cert_digest_len;
8795 }
8796#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8797#endif /* MBEDTLS_X509_CRT_PARSE_C */
8798
8799 /*
8800 * Session ticket and associated data
8801 */
8802#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8803 if( 3 > (size_t)( end - p ) )
8804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8805
8806 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8807 p += 3;
8808
8809 if( session->ticket_len != 0 )
8810 {
8811 if( session->ticket_len > (size_t)( end - p ) )
8812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8813
8814 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8815 if( session->ticket == NULL )
8816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8817
8818 memcpy( session->ticket, p, session->ticket_len );
8819 p += session->ticket_len;
8820 }
8821
8822 if( 4 > (size_t)( end - p ) )
8823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8824
8825 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8826 ( (uint32_t) p[1] << 16 ) |
8827 ( (uint32_t) p[2] << 8 ) |
8828 ( (uint32_t) p[3] );
8829 p += 4;
8830#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8831
8832 /*
8833 * Misc extension-related info
8834 */
8835#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8836 if( 1 > (size_t)( end - p ) )
8837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8838
8839 session->mfl_code = *p++;
8840#endif
8841
8842#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8843 if( 1 > (size_t)( end - p ) )
8844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8845
8846 session->encrypt_then_mac = *p++;
8847#endif
8848
8849 /* Done, should have consumed entire buffer */
8850 if( p != end )
8851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8852
8853 return( 0 );
8854}
Jerry Yudc7bd172022-02-17 13:44:15 +08008855#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8856
XiaokangQian75d40ef2022-04-20 11:05:24 +00008857int mbedtls_ssl_validate_ciphersuite(
8858 const mbedtls_ssl_context *ssl,
8859 const mbedtls_ssl_ciphersuite_t *suite_info,
8860 mbedtls_ssl_protocol_version min_tls_version,
8861 mbedtls_ssl_protocol_version max_tls_version )
8862{
8863 (void) ssl;
8864
8865 if( suite_info == NULL )
8866 return( -1 );
8867
8868 if( ( suite_info->min_tls_version > max_tls_version ) ||
8869 ( suite_info->max_tls_version < min_tls_version ) )
8870 {
8871 return( -1 );
8872 }
8873
XiaokangQian060d8672022-04-21 09:24:56 +00008874#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008875#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8876 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8877 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8878 {
8879 return( -1 );
8880 }
8881#endif
8882
8883 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8884#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8885 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8886 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8887 {
8888 return( -1 );
8889 }
8890#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8892
8893 return( 0 );
8894}
8895
Ronald Crone68ab4f2022-10-05 12:46:29 +02008896#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008897/*
8898 * Function for writing a signature algorithm extension.
8899 *
8900 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8901 * value (TLS 1.3 RFC8446):
8902 * enum {
8903 * ....
8904 * ecdsa_secp256r1_sha256( 0x0403 ),
8905 * ecdsa_secp384r1_sha384( 0x0503 ),
8906 * ecdsa_secp521r1_sha512( 0x0603 ),
8907 * ....
8908 * } SignatureScheme;
8909 *
8910 * struct {
8911 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8912 * } SignatureSchemeList;
8913 *
8914 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8915 * value (TLS 1.2 RFC5246):
8916 * enum {
8917 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8918 * sha512(6), (255)
8919 * } HashAlgorithm;
8920 *
8921 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8922 * SignatureAlgorithm;
8923 *
8924 * struct {
8925 * HashAlgorithm hash;
8926 * SignatureAlgorithm signature;
8927 * } SignatureAndHashAlgorithm;
8928 *
8929 * SignatureAndHashAlgorithm
8930 * supported_signature_algorithms<2..2^16-2>;
8931 *
8932 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8933 * generalization of the TLS 1.2 signature algorithm extension.
8934 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8935 * `SignatureScheme` field of TLS 1.3
8936 *
8937 */
8938int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8939 const unsigned char *end, size_t *out_len )
8940{
8941 unsigned char *p = buf;
8942 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8943 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8944
8945 *out_len = 0;
8946
8947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8948
8949 /* Check if we have space for header and length field:
8950 * - extension_type (2 bytes)
8951 * - extension_data_length (2 bytes)
8952 * - supported_signature_algorithms_length (2 bytes)
8953 */
8954 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8955 p += 6;
8956
8957 /*
8958 * Write supported_signature_algorithms
8959 */
8960 supported_sig_alg = p;
8961 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8962 if( sig_alg == NULL )
8963 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8964
8965 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8966 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8968 *sig_alg,
8969 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008970 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8971 continue;
8972 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8973 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8974 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008976 *sig_alg,
8977 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008978 }
8979
8980 /* Length of supported_signature_algorithms */
8981 supported_sig_alg_len = p - supported_sig_alg;
8982 if( supported_sig_alg_len == 0 )
8983 {
8984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8985 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8986 }
8987
XiaokangQianeaf36512022-04-24 09:07:44 +00008988 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008989 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008990 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8991
XiaokangQianeaf36512022-04-24 09:07:44 +00008992 *out_len = p - buf;
8993
8994#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08008995 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00008996#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008997
XiaokangQianeaf36512022-04-24 09:07:44 +00008998 return( 0 );
8999}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009000#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009001
XiaokangQian40a35232022-05-07 09:02:40 +00009002#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009003/*
9004 * mbedtls_ssl_parse_server_name_ext
9005 *
9006 * Structure of server_name extension:
9007 *
9008 * enum {
9009 * host_name(0), (255)
9010 * } NameType;
9011 * opaque HostName<1..2^16-1>;
9012 *
9013 * struct {
9014 * NameType name_type;
9015 * select (name_type) {
9016 * case host_name: HostName;
9017 * } name;
9018 * } ServerName;
9019 * struct {
9020 * ServerName server_name_list<1..2^16-1>
9021 * } ServerNameList;
9022 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009023MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009024int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9025 const unsigned char *buf,
9026 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009027{
9028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9029 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009030 size_t server_name_list_len, hostname_len;
9031 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009032
XiaokangQianf2a94202022-05-20 06:44:24 +00009033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009034
9035 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009036 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009037 p += 2;
9038
XiaokangQian9b2b7712022-05-17 02:57:00 +00009039 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9040 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009041 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009042 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009043 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009044 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009045 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9046 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009047
9048 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9049 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009050 /* sni_name is intended to be used only during the parsing of the
9051 * ClientHello message (it is reset to NULL before the end of
9052 * the message parsing). Thus it is ok to just point to the
9053 * reception buffer and not make a copy of it.
9054 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009055 ssl->handshake->sni_name = p + 3;
9056 ssl->handshake->sni_name_len = hostname_len;
9057 if( ssl->conf->f_sni == NULL )
9058 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009059 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009060 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009061 if( ret != 0 )
9062 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009064 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9065 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009066 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9067 }
9068 return( 0 );
9069 }
9070
9071 p += hostname_len + 3;
9072 }
9073
9074 return( 0 );
9075}
9076#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9077
XiaokangQianacb39922022-06-17 10:18:48 +00009078#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009079MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009080int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9081 const unsigned char *buf,
9082 const unsigned char *end )
9083{
9084 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009085 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009086 const unsigned char *protocol_name_list;
9087 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009088 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009089
9090 /* If ALPN not configured, just ignore the extension */
9091 if( ssl->conf->alpn_list == NULL )
9092 return( 0 );
9093
9094 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009095 * RFC7301, section 3.1
9096 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009097 *
XiaokangQianc7403452022-06-23 03:24:12 +00009098 * struct {
9099 * ProtocolName protocol_name_list<2..2^16-1>
9100 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009101 */
9102
XiaokangQianc7403452022-06-23 03:24:12 +00009103 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009104 * protocol_name_list_len 2 bytes
9105 * protocol_name_len 1 bytes
9106 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009107 */
XiaokangQianacb39922022-06-17 10:18:48 +00009108 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9109
XiaokangQianc7403452022-06-23 03:24:12 +00009110 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009111 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009112 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009113 protocol_name_list = p;
9114 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009115
9116 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009117 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009118 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009119 protocol_name_len = *p++;
9120 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9121 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009122 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009123 {
9124 MBEDTLS_SSL_PEND_FATAL_ALERT(
9125 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9126 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009127 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009128 }
9129
9130 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009131 }
9132
9133 /* Use our order of preference */
9134 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9135 {
9136 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009137 p = protocol_name_list;
9138 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009139 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009140 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009141 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009142 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009143 {
9144 ssl->alpn_chosen = *alpn;
9145 return( 0 );
9146 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009147
9148 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009149 }
9150 }
9151
XiaokangQian95d5f542022-06-24 02:29:26 +00009152 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009153 MBEDTLS_SSL_PEND_FATAL_ALERT(
9154 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9155 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9156 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9157}
9158
9159int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9160 unsigned char *buf,
9161 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009162 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009163{
9164 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009165 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009166 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009167
9168 if( ssl->alpn_chosen == NULL )
9169 {
9170 return( 0 );
9171 }
9172
XiaokangQian95d5f542022-06-24 02:29:26 +00009173 protocol_name_len = strlen( ssl->alpn_chosen );
9174 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009175
9176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9177 /*
9178 * 0 . 1 ext identifier
9179 * 2 . 3 ext length
9180 * 4 . 5 protocol list length
9181 * 6 . 6 protocol name length
9182 * 7 . 7+n protocol name
9183 */
9184 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9185
XiaokangQian95d5f542022-06-24 02:29:26 +00009186 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009187
XiaokangQian95d5f542022-06-24 02:29:26 +00009188 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9189 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009190 /* Note: the length of the chosen protocol has been checked to be less
9191 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9192 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009193 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009194
XiaokangQian95d5f542022-06-24 02:29:26 +00009195 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009196
9197#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9198 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9199#endif
9200
XiaokangQianacb39922022-06-17 10:18:48 +00009201 return ( 0 );
9202}
9203#endif /* MBEDTLS_SSL_ALPN */
9204
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009205#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009206 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009207 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009208 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009209int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9210 const char *hostname )
9211{
9212 /* Initialize to suppress unnecessary compiler warning */
9213 size_t hostname_len = 0;
9214
9215 /* Check if new hostname is valid before
9216 * making any change to current one */
9217 if( hostname != NULL )
9218 {
9219 hostname_len = strlen( hostname );
9220
9221 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9223 }
9224
9225 /* Now it's clear that we will overwrite the old hostname,
9226 * so we can free it safely */
9227 if( session->hostname != NULL )
9228 {
9229 mbedtls_platform_zeroize( session->hostname,
9230 strlen( session->hostname ) );
9231 mbedtls_free( session->hostname );
9232 }
9233
9234 /* Passing NULL as hostname shall clear the old one */
9235 if( hostname == NULL )
9236 {
9237 session->hostname = NULL;
9238 }
9239 else
9240 {
9241 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9242 if( session->hostname == NULL )
9243 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9244
9245 memcpy( session->hostname, hostname, hostname_len );
9246 }
9247
9248 return( 0 );
9249}
Xiaokang Qian03409292022-10-12 02:49:52 +00009250#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9251 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009252 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009253 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255#endif /* MBEDTLS_SSL_TLS_C */