blob: da90b2350fcc14efaf7a546cddac3bc0a6555279 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
59 const uint8_t *cur, const uint8_t *end, size_t need )
60{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
66void mbedtls_ssl_reset_chk_buf_ptr_fail_args( void )
67{
68 memset( &chk_buf_ptr_fail_args, 0, sizeof( chk_buf_ptr_fail_args ) );
69}
70
71int mbedtls_ssl_cmp_chk_buf_ptr_fail_args( mbedtls_ssl_chk_buf_ptr_args *args )
72{
73 return( ( chk_buf_ptr_fail_args.cur != args->cur ) ||
74 ( chk_buf_ptr_fail_args.end != args->end ) ||
75 ( chk_buf_ptr_fail_args.need != args->need ) );
76}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Hanno Becker8367ccc2019-05-14 11:30:10 +010084int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
88 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
89 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
90
Hanno Becker611ac772019-05-14 11:45:26 +010091 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
93 {
94 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
95 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
99 return( 0 );
100}
101
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100102int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len )
106{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100107 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
111 if( enable == MBEDTLS_SSL_CID_DISABLED )
112 {
113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
114 return( 0 );
115 }
116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100117 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Hanno Beckerad4a1372019-05-03 13:06:44 +0100119 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100120 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
122 (unsigned) own_cid_len,
123 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125 }
126
127 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100128 /* Truncation is not an issue here because
129 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
130 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132 return( 0 );
133}
134
Paul Elliott0113cf12022-03-11 20:26:47 +0000135int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl,
136 int *enabled,
137 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
138 size_t *own_cid_len )
139{
140 *enabled = MBEDTLS_SSL_CID_DISABLED;
141
142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
148 if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
149 return( 0 );
150
151 if( own_cid_len != NULL )
152 {
153 *own_cid_len = ssl->own_cid_len;
154 if( own_cid != NULL )
155 memcpy( own_cid, ssl->own_cid, ssl->own_cid_len );
156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
160 return( 0 );
161}
162
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
166 size_t *peer_cid_len )
167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Becker76a79ab2019-05-03 14:38:32 +0100170 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Jerry Yu6848a612022-10-27 13:03:26 +0800171 mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 if( ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0 )
182 {
183 return( 0 );
184 }
185
Hanno Becker615ef172019-05-22 16:50:35 +0100186 if( peer_cid_len != NULL )
187 {
188 *peer_cid_len = ssl->transform_in->out_cid_len;
189 if( peer_cid != NULL )
190 {
191 memcpy( peer_cid, ssl->transform_in->out_cid,
192 ssl->transform_in->out_cid_len );
193 }
194 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100195
196 *enabled = MBEDTLS_SSL_CID_ENABLED;
197
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198 return( 0 );
199}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205/*
206 * Convert max_fragment_length codes to length.
207 * RFC 6066 says:
208 * enum{
209 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
210 * } MaxFragmentLength;
211 * and we add 0 -> extension unused
212 */
Angus Grattond8213d02016-05-25 20:56:48 +1000213static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200214{
Angus Grattond8213d02016-05-25 20:56:48 +1000215 switch( mfl )
216 {
217 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
218 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
219 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
220 return 512;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
222 return 1024;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
224 return 2048;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
226 return 4096;
227 default:
228 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
229 }
230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200232
Hanno Becker52055ae2019-02-06 14:30:46 +0000233int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
234 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_session_free( dst );
237 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
吴敬辉0b716112021-11-29 10:46:35 +0800238#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
239 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
241 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000242 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800243#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000244#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249 if( src->peer_cert != NULL )
250 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200253 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200254 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200255 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200260 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
264 return( ret );
265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 if( src->peer_cert_digest != NULL )
269 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000271 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 if( dst->peer_cert_digest == NULL )
273 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
274
275 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len );
277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200284#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->ticket != NULL )
286 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200287 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200288 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
291 memcpy( dst->ticket, src->ticket, src->ticket_len );
292 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000293
294#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
295 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
296 if( src->endpoint == MBEDTLS_SSL_IS_CLIENT )
297 {
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 ret = mbedtls_ssl_session_set_hostname( dst, src->hostname );
300 if( ret != 0 )
301 return ( ret );
302 }
303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
304 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 return( 0 );
308}
309
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500310#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200311MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
313{
314 unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
315 if( resized_buffer == NULL )
316 return -1;
317
318 /* We want to copy len_new bytes when downsizing the buffer, and
319 * len_old bytes when upsizing, so we choose the smaller of two sizes,
320 * to fit one buffer into another. Size checks, ensuring that no data is
321 * lost, are done outside of this function. */
322 memcpy( resized_buffer, *buffer,
323 ( len_new < *len_old ) ? len_new : *len_old );
324 mbedtls_platform_zeroize( *buffer, *len_old );
325 mbedtls_free( *buffer );
326
327 *buffer = resized_buffer;
328 *len_old = len_new;
329
330 return 0;
331}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332
333static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
Andrzej Kurek069fa962021-01-07 08:02:15 -0500334 size_t in_buf_new_len,
335 size_t out_buf_new_len )
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336{
337 int modified = 0;
338 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
339 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
340 if( ssl->in_buf != NULL )
341 {
342 written_in = ssl->in_msg - ssl->in_buf;
343 iv_offset_in = ssl->in_iv - ssl->in_buf;
344 len_offset_in = ssl->in_len - ssl->in_buf;
345 if( downsizing ?
346 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
347 ssl->in_buf_len < in_buf_new_len )
348 {
349 if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
350 {
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
352 }
353 else
354 {
Paul Elliottb7449902021-03-10 18:14:58 +0000355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
362 if( ssl->out_buf != NULL )
363 {
364 written_out = ssl->out_msg - ssl->out_buf;
365 iv_offset_out = ssl->out_iv - ssl->out_buf;
366 len_offset_out = ssl->out_len - ssl->out_buf;
367 if( downsizing ?
368 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
369 ssl->out_buf_len < out_buf_new_len )
370 {
371 if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
374 }
375 else
376 {
Paul Elliottb7449902021-03-10 18:14:58 +0000377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len ) );
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
383 if( modified )
384 {
385 /* Update pointers here to avoid doing it twice. */
386 mbedtls_ssl_reset_in_out_pointers( ssl );
387 /* Fields below might not be properly updated with record
388 * splitting or with CID, so they are manually updated here. */
389 ssl->out_msg = ssl->out_buf + written_out;
390 ssl->out_len = ssl->out_buf + len_offset_out;
391 ssl->out_iv = ssl->out_buf + iv_offset_out;
392
393 ssl->in_msg = ssl->in_buf + written_in;
394 ssl->in_len = ssl->in_buf + len_offset_in;
395 ssl->in_iv = ssl->in_buf + iv_offset_in;
396 }
397}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500398#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
399
Jerry Yudb8c48a2022-01-27 14:54:54 +0800400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800401
402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
403typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
406 unsigned char *dstbuf, size_t dlen );
407
408static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id );
409
410#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
411
412/* Type for the TLS PRF */
413typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
414 const unsigned char *, size_t,
415 unsigned char *, size_t);
416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200417MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800418static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
419 int ciphersuite,
420 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yued14c932022-02-17 13:40:45 +0800422 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yued14c932022-02-17 13:40:45 +0800424 ssl_tls_prf_t tls_prf,
425 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -0400426 mbedtls_ssl_protocol_version tls_version,
Jerry Yued14c932022-02-17 13:40:45 +0800427 unsigned endpoint,
428 const mbedtls_ssl_context *ssl );
429
Andrzej Kurek25f27152022-08-17 16:09:31 -0400430#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200431MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen );
436static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
437static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
438
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400439#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800440
Andrzej Kurek25f27152022-08-17 16:09:31 -0400441#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yued14c932022-02-17 13:40:45 +0800443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
446 unsigned char *dstbuf, size_t dlen );
447
448static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
449static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400450#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yued14c932022-02-17 13:40:45 +0800451
Jerry Yu438ddd82022-07-07 06:55:50 +0000452static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800453 unsigned char *buf,
454 size_t buf_len );
Jerry Yu251a12e2022-07-13 15:15:48 +0800455
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200456MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +0000457static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yued14c932022-02-17 13:40:45 +0800458 const unsigned char *buf,
459 size_t len );
460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
461
Jerry Yu53d23e22022-02-09 16:25:09 +0800462static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
463
Andrzej Kurek25f27152022-08-17 16:09:31 -0400464#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800465static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400466#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudb8c48a2022-01-27 14:54:54 +0800467
Andrzej Kurek25f27152022-08-17 16:09:31 -0400468#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yudb8c48a2022-01-27 14:54:54 +0800469static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400470#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Paul Bakker1ef83d62012-04-11 12:09:53 +0000471
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
473 const unsigned char *secret, size_t slen,
474 const char *label,
475 const unsigned char *random, size_t rlen,
476 unsigned char *dstbuf, size_t dlen )
477{
478 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
479
480 switch( prf )
481 {
Ron Eldord2f25f72019-05-15 14:54:22 +0300482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 case MBEDTLS_SSL_TLS_PRF_SHA384:
485 tls_prf = tls_prf_sha384;
486 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400487#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400488#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 case MBEDTLS_SSL_TLS_PRF_SHA256:
490 tls_prf = tls_prf_sha256;
491 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400492#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 default:
495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
496 }
497
498 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
499}
500
Jerry Yuc73c6182022-02-08 20:29:25 +0800501#if defined(MBEDTLS_X509_CRT_PARSE_C)
502static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
503{
504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
505 if( session->peer_cert != NULL )
506 {
507 mbedtls_x509_crt_free( session->peer_cert );
508 mbedtls_free( session->peer_cert );
509 session->peer_cert = NULL;
510 }
511#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512 if( session->peer_cert_digest != NULL )
513 {
514 /* Zeroization is not necessary. */
515 mbedtls_free( session->peer_cert_digest );
516 session->peer_cert_digest = NULL;
517 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
518 session->peer_cert_digest_len = 0;
519 }
520#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
521}
522#endif /* MBEDTLS_X509_CRT_PARSE_C */
523
Jerry Yu7a485c12022-10-31 13:08:18 +0800524uint32_t mbedtls_ssl_get_extension_id( unsigned int extension_type )
525{
526 switch( extension_type )
527 {
528 case MBEDTLS_TLS_EXT_SERVERNAME:
529 return( MBEDTLS_SSL_EXT_ID_SERVERNAME );
530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
532 return( MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH );
533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
535 return( MBEDTLS_SSL_EXT_ID_STATUS_REQUEST );
536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
538 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS );
539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
541 return( MBEDTLS_SSL_EXT_ID_SIG_ALG );
542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
544 return( MBEDTLS_SSL_EXT_ID_USE_SRTP );
545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
547 return( MBEDTLS_SSL_EXT_ID_HEARTBEAT );
548
549 case MBEDTLS_TLS_EXT_ALPN:
550 return( MBEDTLS_SSL_EXT_ID_ALPN );
551
552 case MBEDTLS_TLS_EXT_SCT:
553 return( MBEDTLS_SSL_EXT_ID_SCT );
554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
556 return( MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE );
557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
559 return( MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE );
560
561 case MBEDTLS_TLS_EXT_PADDING:
562 return( MBEDTLS_SSL_EXT_ID_PADDING );
563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
565 return( MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY );
566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
568 return( MBEDTLS_SSL_EXT_ID_EARLY_DATA );
569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
571 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS );
572
573 case MBEDTLS_TLS_EXT_COOKIE:
574 return( MBEDTLS_SSL_EXT_ID_COOKIE );
575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
577 return( MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES );
578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
580 return( MBEDTLS_SSL_EXT_ID_CERT_AUTH );
581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
583 return( MBEDTLS_SSL_EXT_ID_OID_FILTERS );
584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
586 return( MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH );
587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
589 return( MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT );
590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
592 return( MBEDTLS_SSL_EXT_ID_KEY_SHARE );
593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
595 return( MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC );
596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
598 return( MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS );
599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
601 return( MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC );
602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
604 return( MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET );
605
606 case MBEDTLS_TLS_EXT_SESSION_TICKET:
607 return( MBEDTLS_SSL_EXT_ID_SESSION_TICKET );
608
609 }
610
611 return( MBEDTLS_SSL_EXT_ID_UNRECOGNIZED );
612}
613
614uint32_t mbedtls_ssl_get_extension_mask( unsigned int extension_type )
615{
616 return( 1 << mbedtls_ssl_get_extension_id( extension_type ) );
617}
618
Jerry Yud25cab02022-10-31 12:48:30 +0800619#if defined(MBEDTLS_DEBUG_C)
620static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800621 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800622 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
623 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
624 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
625 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
626 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
627 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
628 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
629 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
630 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
631 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
632 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
633 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
634 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
635 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
636 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
637 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
638 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
639 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
640 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
641 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
642 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
643 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
644 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
646 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
647 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
648 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
649};
650
Jerry Yuea52ed92022-11-08 21:01:17 +0800651static unsigned int extension_type_table[]={
Jerry Yud25cab02022-10-31 12:48:30 +0800652 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
653 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
654 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
655 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
657 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
658 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
659 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
660 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
661 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
662 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
664 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
665 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
666 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
668 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
669 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
670 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
671 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
672 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
673 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
674 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
675 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
676 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
677 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
678 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
679 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
680};
681
682const char *mbedtls_ssl_get_extension_name( unsigned int extension_type )
683{
684 return( extension_name_table[
685 mbedtls_ssl_get_extension_id( extension_type ) ] );
686}
687
688static const char *ssl_tls13_get_hs_msg_name( int hs_msg_type )
689{
690 switch( hs_msg_type )
691 {
692 case MBEDTLS_SSL_HS_CLIENT_HELLO:
693 return( "ClientHello" );
694 case MBEDTLS_SSL_HS_SERVER_HELLO:
695 return( "ServerHello" );
696 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
697 return( "HelloRetryRequest" );
698 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
699 return( "NewSessionTicket" );
700 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
701 return( "EncryptedExtensions" );
702 case MBEDTLS_SSL_HS_CERTIFICATE:
703 return( "Certificate" );
704 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
705 return( "CertificateRequest" );
706 }
Jerry Yu79aa7212022-11-08 21:30:21 +0800707 return( "Unknown" );
Jerry Yud25cab02022-10-31 12:48:30 +0800708}
709
Jerry Yu79aa7212022-11-08 21:30:21 +0800710void mbedtls_ssl_print_extension( const mbedtls_ssl_context *ssl,
711 int level, const char *file, int line,
712 int hs_msg_type, unsigned int extension_type,
713 const char *extra_msg0, const char *extra_msg1 )
Jerry Yud25cab02022-10-31 12:48:30 +0800714{
715 const char *extra_msg;
716 if( extra_msg0 && extra_msg1 )
717 {
718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
720 "%s: %s(%u) extension %s %s.",
721 ssl_tls13_get_hs_msg_name( hs_msg_type ),
722 mbedtls_ssl_get_extension_name( extension_type ),
723 extension_type,
724 extra_msg0, extra_msg1 );
725 return;
726 }
727
728 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
729 if( extra_msg )
730 {
731 mbedtls_debug_print_msg(
732 ssl, level, file, line,
733 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
734 mbedtls_ssl_get_extension_name( extension_type ), extension_type,
735 extra_msg );
736 return;
737 }
738
739 mbedtls_debug_print_msg(
740 ssl, level, file, line,
741 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name( hs_msg_type ),
742 mbedtls_ssl_get_extension_name( extension_type ), extension_type );
743}
744
745void mbedtls_ssl_print_extensions( const mbedtls_ssl_context *ssl,
746 int level, const char *file, int line,
747 int hs_msg_type, uint32_t extensions_mask,
748 const char *extra )
749{
750
751 for( unsigned i = 0;
752 i < sizeof( extension_name_table ) / sizeof( extension_name_table[0] );
753 i++ )
754 {
Jerry Yu79aa7212022-11-08 21:30:21 +0800755 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800756 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Jerry Yu97be6a92022-11-09 22:43:31 +0800757 extensions_mask & ( 1 << i ) ? "exists" : "does not exist", extra );
Jerry Yud25cab02022-10-31 12:48:30 +0800758 }
759}
760
761#endif /* MBEDTLS_DEBUG_C */
762
Jerry Yuc73c6182022-02-08 20:29:25 +0800763void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
764 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
765{
766 ((void) ciphersuite_info);
767
Andrzej Kurek25f27152022-08-17 16:09:31 -0400768#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800769 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
770 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
771 else
772#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400773#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800774 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
775 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
776 else
777#endif
778 {
779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
780 return;
781 }
782}
783
XiaokangQianadab9a62022-07-18 07:41:26 +0000784void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
785 unsigned hs_type,
786 size_t total_hs_len )
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100787{
788 unsigned char hs_hdr[4];
789
790 /* Build HS header for checksum update. */
791 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
792 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
793 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
794 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
795
796 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
797}
798
799void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
800 unsigned hs_type,
801 unsigned char const *msg,
802 size_t msg_len )
803{
804 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
805 ssl->handshake->update_checksum( ssl, msg, msg_len );
806}
807
Jerry Yuc73c6182022-02-08 20:29:25 +0800808void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
809{
810 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400811#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#if defined(MBEDTLS_USE_PSA_CRYPTO)
813 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
814 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
815#else
816 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
817#endif
818#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400819#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820#if defined(MBEDTLS_USE_PSA_CRYPTO)
821 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
822 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
823#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400824 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#endif
826#endif
827}
828
829static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
830 const unsigned char *buf, size_t len )
831{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400832#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
834 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
835#else
836 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
837#endif
838#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
842#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400843 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400846#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
847 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
848 (void) ssl;
849 (void) buf;
850 (void) len;
851#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800852}
853
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
856 const unsigned char *buf, size_t len )
857{
858#if defined(MBEDTLS_USE_PSA_CRYPTO)
859 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
860#else
861 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
862#endif
863}
864#endif
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800867static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
868 const unsigned char *buf, size_t len )
869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
871 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
872#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400873 mbedtls_sha512_update( &ssl->handshake->fin_sha384, buf, len );
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
875}
876#endif
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200881
Andrzej Kurek25f27152022-08-17 16:09:31 -0400882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884 handshake->fin_sha256_psa = psa_hash_operation_init();
885 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_sha256_init( &handshake->fin_sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200888 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400891#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500893 handshake->fin_sha384_psa = psa_hash_operation_init();
894 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -0500895#else
Andrzej Kureka242e832022-08-11 10:03:14 -0400896 mbedtls_sha512_init( &handshake->fin_sha384 );
897 mbedtls_sha512_starts( &handshake->fin_sha384, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200900
901 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_DHM_C)
904 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200906#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200909#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
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
Ronald Cron73fe8df2022-10-05 14:31:43 +02001875#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Ronald Crond29e13e2022-10-19 10:33:48 +02001876int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001877{
Ronald Crond29e13e2022-10-19 10:33:48 +02001878 if( conf->psk_identity == NULL ||
1879 conf->psk_identity_len == 0 )
1880 {
1881 return( 0 );
1882 }
1883
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001884#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Crond29e13e2022-10-19 10:33:48 +02001885 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001886 return( 1 );
1887#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001888
1889 if( conf->psk != NULL && conf->psk_len != 0 )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001890 return( 1 );
1891
1892 return( 0 );
1893}
1894
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001895static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
1896{
1897 /* Remove reference to existing PSK, if any. */
1898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001899 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001900 {
1901 /* The maintenance of the PSK key slot is the
1902 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001903 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001904 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001905#endif /* MBEDTLS_USE_PSA_CRYPTO */
1906 if( conf->psk != NULL )
1907 {
1908 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
1909
1910 mbedtls_free( conf->psk );
1911 conf->psk = NULL;
1912 conf->psk_len = 0;
1913 }
1914
1915 /* Remove reference to PSK identity, if any. */
1916 if( conf->psk_identity != NULL )
1917 {
1918 mbedtls_free( conf->psk_identity );
1919 conf->psk_identity = NULL;
1920 conf->psk_identity_len = 0;
1921 }
1922}
1923
Hanno Becker7390c712018-11-15 13:33:04 +00001924/* This function assumes that PSK identity in the SSL config is unset.
1925 * It checks that the provided identity is well-formed and attempts
1926 * to make a copy of it in the SSL config.
1927 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001928MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker7390c712018-11-15 13:33:04 +00001929static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
1930 unsigned char const *psk_identity,
1931 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001932{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001933 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00001934 if( psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001935 psk_identity_len == 0 ||
Hanno Becker7390c712018-11-15 13:33:04 +00001936 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10001937 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001938 {
1939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1940 }
1941
Hanno Becker7390c712018-11-15 13:33:04 +00001942 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
1943 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001944 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02001945
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001946 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01001947 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02001948
1949 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02001950}
1951
Hanno Becker7390c712018-11-15 13:33:04 +00001952int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
1953 const unsigned char *psk, size_t psk_len,
1954 const unsigned char *psk_identity, size_t psk_identity_len )
1955{
Janos Follath865b3eb2019-12-16 11:46:15 +00001956 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001957
1958 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02001959 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001960 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Becker7390c712018-11-15 13:33:04 +00001961
1962 /* Check and set raw PSK */
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001963 if( psk == NULL )
Hanno Becker7390c712018-11-15 13:33:04 +00001964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01001965 if( psk_len == 0 )
1966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1967 if( psk_len > MBEDTLS_PSK_MAX_LEN )
1968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1969
Hanno Becker7390c712018-11-15 13:33:04 +00001970 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
1971 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1972 conf->psk_len = psk_len;
1973 memcpy( conf->psk, psk, conf->psk_len );
1974
1975 /* Check and set PSK Identity */
1976 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
1977 if( ret != 0 )
1978 ssl_conf_remove_psk( conf );
1979
1980 return( ret );
1981}
1982
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001983static void ssl_remove_psk( mbedtls_ssl_context *ssl )
1984{
1985#if defined(MBEDTLS_USE_PSA_CRYPTO)
Ronald Croncf56a0a2020-08-04 09:51:30 +02001986 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001987 {
Neil Armstrong501c9322022-05-03 09:35:09 +02001988 /* The maintenance of the external PSK key slot is the
1989 * user's responsibility. */
1990 if( ssl->handshake->psk_opaque_is_internal )
1991 {
1992 psa_destroy_key( ssl->handshake->psk_opaque );
1993 ssl->handshake->psk_opaque_is_internal = 0;
1994 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02001995 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001996 }
Neil Armstronge952a302022-05-03 10:22:14 +02001997#else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001998 if( ssl->handshake->psk != NULL )
1999 {
2000 mbedtls_platform_zeroize( ssl->handshake->psk,
2001 ssl->handshake->psk_len );
2002 mbedtls_free( ssl->handshake->psk );
2003 ssl->handshake->psk_len = 0;
2004 }
Neil Armstronge952a302022-05-03 10:22:14 +02002005#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002006}
2007
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002008int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
2009 const unsigned char *psk, size_t psk_len )
2010{
Neil Armstrong501c9322022-05-03 09:35:09 +02002011#if defined(MBEDTLS_USE_PSA_CRYPTO)
2012 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002014 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002016#endif /* MBEDTLS_USE_PSA_CRYPTO */
2017
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002018 if( psk == NULL || ssl->handshake == NULL )
2019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2020
2021 if( psk_len > MBEDTLS_PSK_MAX_LEN )
2022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2023
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002024 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002025
Neil Armstrong501c9322022-05-03 09:35:09 +02002026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002027#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2028 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
2029 {
Jerry Yu6cf6b472022-08-16 14:50:28 +08002030 if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yuccc68a42022-07-26 16:39:20 +08002031 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_384 );
2032 else
2033 alg = PSA_ALG_TLS12_PSK_TO_MS( PSA_ALG_SHA_256 );
2034 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
2035 }
2036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002037
Jerry Yu568ec252022-07-22 21:27:34 +08002038#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuccc68a42022-07-26 16:39:20 +08002039 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
2040 {
2041 alg = PSA_ALG_HKDF_EXTRACT( PSA_ALG_ANY_HASH );
2042 psa_set_key_usage_flags( &key_attributes,
2043 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT );
2044 }
2045#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2046
Neil Armstrong501c9322022-05-03 09:35:09 +02002047 psa_set_key_algorithm( &key_attributes, alg );
2048 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
2049
2050 status = psa_import_key( &key_attributes, psk, psk_len, &key );
2051 if( status != PSA_SUCCESS )
2052 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2053
2054 /* Allow calling psa_destroy_key() on psk remove */
2055 ssl->handshake->psk_opaque_is_internal = 1;
2056 return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
2057#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002058 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002059 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002060
2061 ssl->handshake->psk_len = psk_len;
2062 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
2063
2064 return( 0 );
Neil Armstrong501c9322022-05-03 09:35:09 +02002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002066}
2067
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002068#if defined(MBEDTLS_USE_PSA_CRYPTO)
2069int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek03e01462022-01-03 12:53:24 +01002070 mbedtls_svc_key_id_t psk,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002071 const unsigned char *psk_identity,
2072 size_t psk_identity_len )
2073{
Janos Follath865b3eb2019-12-16 11:46:15 +00002074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002075
2076 /* We currently only support one PSK, raw or opaque. */
Ronald Crond29e13e2022-10-19 10:33:48 +02002077 if( mbedtls_ssl_conf_has_static_psk( conf ) )
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002078 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002079
Hanno Becker7390c712018-11-15 13:33:04 +00002080 /* Check and set opaque PSK */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002081 if( mbedtls_svc_key_id_is_null( psk ) )
Hanno Becker7390c712018-11-15 13:33:04 +00002082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ronald Croncf56a0a2020-08-04 09:51:30 +02002083 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002084
2085 /* Check and set PSK Identity */
2086 ret = ssl_conf_set_psk_identity( conf, psk_identity,
2087 psk_identity_len );
2088 if( ret != 0 )
2089 ssl_conf_remove_psk( conf );
2090
2091 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002092}
2093
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002094int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
2095 mbedtls_svc_key_id_t psk )
2096{
2097 if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
2098 ( ssl->handshake == NULL ) )
2099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2100
2101 ssl_remove_psk( ssl );
2102 ssl->handshake->psk_opaque = psk;
2103 return( 0 );
2104}
2105#endif /* MBEDTLS_USE_PSA_CRYPTO */
2106
Jerry Yu8897c072022-08-12 13:56:53 +08002107#if defined(MBEDTLS_SSL_SRV_C)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002108void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
2109 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2110 size_t),
2111 void *p_psk )
2112{
2113 conf->f_psk = f_psk;
2114 conf->p_psk = p_psk;
2115}
Jerry Yu8897c072022-08-12 13:56:53 +08002116#endif /* MBEDTLS_SSL_SRV_C */
2117
Ronald Cron73fe8df2022-10-05 14:31:43 +02002118#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002119
2120#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002121static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2122 psa_algorithm_t alg )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002123{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002124#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002125 if( alg == PSA_ALG_CBC_NO_PADDING )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002126 return( MBEDTLS_SSL_MODE_CBC );
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002127#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002128 if( PSA_ALG_IS_AEAD( alg ) )
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002129 return( MBEDTLS_SSL_MODE_AEAD );
Gilles Peskine301711e2022-04-26 16:57:05 +02002130 return( MBEDTLS_SSL_MODE_STREAM );
2131}
2132
2133#else /* MBEDTLS_USE_PSA_CRYPTO */
2134
2135static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
2136 mbedtls_cipher_mode_t mode )
2137{
2138#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
2139 if( mode == MBEDTLS_MODE_CBC )
2140 return( MBEDTLS_SSL_MODE_CBC );
2141#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2142
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002143#if defined(MBEDTLS_GCM_C) || \
2144 defined(MBEDTLS_CCM_C) || \
2145 defined(MBEDTLS_CHACHAPOLY_C)
2146 if( mode == MBEDTLS_MODE_GCM ||
2147 mode == MBEDTLS_MODE_CCM ||
2148 mode == MBEDTLS_MODE_CHACHAPOLY )
2149 return( MBEDTLS_SSL_MODE_AEAD );
2150#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002151
2152 return( MBEDTLS_SSL_MODE_STREAM );
2153}
Gilles Peskine301711e2022-04-26 16:57:05 +02002154#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002155
Gilles Peskinee108d982022-04-26 16:50:40 +02002156static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2157 mbedtls_ssl_mode_t base_mode,
2158 int encrypt_then_mac )
2159{
2160#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2161 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2162 base_mode == MBEDTLS_SSL_MODE_CBC )
2163 {
2164 return( MBEDTLS_SSL_MODE_CBC_ETM );
2165 }
2166#else
2167 (void) encrypt_then_mac;
2168#endif
2169 return( base_mode );
2170}
2171
Neil Armstrongab555e02022-04-04 11:07:59 +02002172mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002173 const mbedtls_ssl_transform *transform )
2174{
Gilles Peskinee108d982022-04-26 16:50:40 +02002175 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002176#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskinee108d982022-04-26 16:50:40 +02002177 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002178#else
Gilles Peskinee108d982022-04-26 16:50:40 +02002179 mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc )
2180#endif
2181 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002182
Gilles Peskinee108d982022-04-26 16:50:40 +02002183 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002184#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002185 encrypt_then_mac = transform->encrypt_then_mac;
2186#endif
2187 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002188}
2189
Neil Armstrongab555e02022-04-04 11:07:59 +02002190mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002191#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002192 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002193#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002194 const mbedtls_ssl_ciphersuite_t *suite )
2195{
Gilles Peskinee108d982022-04-26 16:50:40 +02002196 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2197
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002198#if defined(MBEDTLS_USE_PSA_CRYPTO)
2199 psa_status_t status;
2200 psa_algorithm_t alg;
2201 psa_key_type_t type;
2202 size_t size;
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002203 status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size );
2204 if( status == PSA_SUCCESS )
Gilles Peskinee108d982022-04-26 16:50:40 +02002205 base_mode = mbedtls_ssl_get_base_mode( alg );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002206#else
2207 const mbedtls_cipher_info_t *cipher =
2208 mbedtls_cipher_info_from_type( suite->cipher );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002209 if( cipher != NULL )
Gilles Peskinee108d982022-04-26 16:50:40 +02002210 {
2211 base_mode =
2212 mbedtls_ssl_get_base_mode(
2213 mbedtls_cipher_info_get_mode( cipher ) );
2214 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002215#endif /* MBEDTLS_USE_PSA_CRYPTO */
2216
Gilles Peskinee108d982022-04-26 16:50:40 +02002217#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2218 int encrypt_then_mac = 0;
2219#endif
2220 return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002221}
2222
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002223#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002224
2225#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002226/* Serialization of TLS 1.3 sessions:
2227 *
2228 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002229 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002230 * uint64 ticket_received;
2231 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002232 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002233 * } ClientOnlyData;
2234 *
2235 * struct {
2236 * uint8 endpoint;
2237 * uint8 ciphersuite[2];
2238 * uint32 ticket_age_add;
2239 * uint8 ticket_flags;
2240 * opaque resumption_key<0..255>;
2241 * select ( endpoint ) {
2242 * case client: ClientOnlyData;
2243 * case server: uint64 start_time;
2244 * };
2245 * } serialized_session_tls13;
2246 *
2247 */
2248#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002249MBEDTLS_CHECK_RETURN_CRITICAL
2250static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2251 unsigned char *buf,
2252 size_t buf_len,
2253 size_t *olen )
Jerry Yu438ddd82022-07-07 06:55:50 +00002254{
2255 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002256#if defined(MBEDTLS_SSL_CLI_C) && \
2257 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2258 size_t hostname_len = ( session->hostname == NULL ) ?
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002259 0 : strlen( session->hostname ) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002260#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002261 size_t needed = 1 /* endpoint */
2262 + 2 /* ciphersuite */
2263 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002264 + 1 /* ticket_flags */
2265 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002266 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002267
2268 if( session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN )
2269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2270 needed += session->resumption_key_len; /* resumption_key */
2271
Jerry Yu438ddd82022-07-07 06:55:50 +00002272#if defined(MBEDTLS_HAVE_TIME)
2273 needed += 8; /* start_time or ticket_received */
2274#endif
2275
2276#if defined(MBEDTLS_SSL_CLI_C)
2277 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2278 {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002279#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002280 needed += 2 /* hostname_len */
Xiaokang Qian2f9efd32022-10-10 11:24:08 +00002281 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002282#endif
2283
Jerry Yu438ddd82022-07-07 06:55:50 +00002284 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002285 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002286
2287 /* Check size_t overflow */
Jerry Yue36fdd62022-08-17 21:31:36 +08002288 if( session->ticket_len > SIZE_MAX - needed )
2289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yu34191072022-08-18 10:32:09 +08002290
Jerry Yue28d9742022-08-18 15:44:03 +08002291 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002292 }
2293#endif /* MBEDTLS_SSL_CLI_C */
2294
Jerry Yue36fdd62022-08-17 21:31:36 +08002295 *olen = needed;
Jerry Yu438ddd82022-07-07 06:55:50 +00002296 if( needed > buf_len )
Jerry Yue36fdd62022-08-17 21:31:36 +08002297 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Jerry Yu438ddd82022-07-07 06:55:50 +00002298
2299 p[0] = session->endpoint;
2300 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 1 );
2301 MBEDTLS_PUT_UINT32_BE( session->ticket_age_add, p, 3 );
2302 p[7] = session->ticket_flags;
2303
2304 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002305 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002306 p += 9;
Jerry Yubc7c1a42022-07-21 22:57:37 +08002307 memcpy( p, session->resumption_key, session->resumption_key_len );
2308 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002309
2310#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2311 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2312 {
2313 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->start, p, 0 );
2314 p += 8;
2315 }
2316#endif /* MBEDTLS_HAVE_TIME */
2317
2318#if defined(MBEDTLS_SSL_CLI_C)
2319 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2320 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002321#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2322 MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
2323 p += 2;
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002324 if( hostname_len > 0 )
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002325 {
2326 /* save host name */
2327 memcpy( p, session->hostname, hostname_len );
2328 p += hostname_len;
2329 }
2330#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2331
Jerry Yu438ddd82022-07-07 06:55:50 +00002332#if defined(MBEDTLS_HAVE_TIME)
2333 MBEDTLS_PUT_UINT64_BE( (uint64_t) session->ticket_received, p, 0 );
2334 p += 8;
2335#endif
2336 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
2337 p += 4;
2338
2339 MBEDTLS_PUT_UINT16_BE( session->ticket_len, p, 0 );
2340 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002341
2342 if( session->ticket != NULL && session->ticket_len > 0 )
Jerry Yu438ddd82022-07-07 06:55:50 +00002343 {
2344 memcpy( p, session->ticket, session->ticket_len );
2345 p += session->ticket_len;
2346 }
2347 }
2348#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yue36fdd62022-08-17 21:31:36 +08002349 return( 0 );
Jerry Yu438ddd82022-07-07 06:55:50 +00002350}
2351
2352MBEDTLS_CHECK_RETURN_CRITICAL
2353static int ssl_tls13_session_load( mbedtls_ssl_session *session,
2354 const unsigned char *buf,
2355 size_t len )
2356{
2357 const unsigned char *p = buf;
2358 const unsigned char *end = buf + len;
2359
2360 if( end - p < 9 )
2361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2362 session->endpoint = p[0];
2363 session->ciphersuite = MBEDTLS_GET_UINT16_BE( p, 1 );
2364 session->ticket_age_add = MBEDTLS_GET_UINT32_BE( p, 3 );
2365 session->ticket_flags = p[7];
2366
2367 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002368 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002369 p += 9;
2370
Jerry Yubc7c1a42022-07-21 22:57:37 +08002371 if( end - p < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2373
Jerry Yubc7c1a42022-07-21 22:57:37 +08002374 if( sizeof( session->resumption_key ) < session->resumption_key_len )
Jerry Yu438ddd82022-07-07 06:55:50 +00002375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yubc7c1a42022-07-21 22:57:37 +08002376 memcpy( session->resumption_key, p, session->resumption_key_len );
2377 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002378
2379#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
2380 if( session->endpoint == MBEDTLS_SSL_IS_SERVER )
2381 {
2382 if( end - p < 8 )
2383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2384 session->start = MBEDTLS_GET_UINT64_BE( p, 0 );
2385 p += 8;
2386 }
2387#endif /* MBEDTLS_HAVE_TIME */
2388
2389#if defined(MBEDTLS_SSL_CLI_C)
2390 if( session->endpoint == MBEDTLS_SSL_IS_CLIENT )
2391 {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002392#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
2393 defined(MBEDTLS_SSL_SESSION_TICKETS)
2394 size_t hostname_len;
2395 /* load host name */
2396 if( end - p < 2 )
2397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2398 hostname_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2399 p += 2;
2400
2401 if( end - p < ( long int )hostname_len )
2402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2403 if( hostname_len > 0 )
2404 {
2405 session->hostname = mbedtls_calloc( 1, hostname_len );
2406 if( session->hostname == NULL )
2407 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2408 memcpy( session->hostname, p, hostname_len );
2409 p += hostname_len;
2410 }
2411#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2412 MBEDTLS_SSL_SESSION_TICKETS */
2413
Jerry Yu438ddd82022-07-07 06:55:50 +00002414#if defined(MBEDTLS_HAVE_TIME)
2415 if( end - p < 8 )
2416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2417 session->ticket_received = MBEDTLS_GET_UINT64_BE( p, 0 );
2418 p += 8;
2419#endif
2420 if( end - p < 4 )
2421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2422 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE( p, 0 );
2423 p += 4;
2424
2425 if( end - p < 2 )
2426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2427 session->ticket_len = MBEDTLS_GET_UINT16_BE( p, 0 );
2428 p += 2;
2429
2430 if( end - p < ( long int )session->ticket_len )
2431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2432 if( session->ticket_len > 0 )
2433 {
2434 session->ticket = mbedtls_calloc( 1, session->ticket_len );
2435 if( session->ticket == NULL )
2436 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2437 memcpy( session->ticket, p, session->ticket_len );
2438 p += session->ticket_len;
2439 }
2440 }
2441#endif /* MBEDTLS_SSL_CLI_C */
2442
2443 return( 0 );
2444
2445}
2446#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002447MBEDTLS_CHECK_RETURN_CRITICAL
2448static int ssl_tls13_session_save( const mbedtls_ssl_session *session,
2449 unsigned char *buf,
2450 size_t buf_len,
2451 size_t *olen )
Jerry Yu251a12e2022-07-13 15:15:48 +08002452{
2453 ((void) session);
2454 ((void) buf);
2455 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002456 *olen = 0;
2457 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Jerry Yu251a12e2022-07-13 15:15:48 +08002458}
Jerry Yu438ddd82022-07-07 06:55:50 +00002459
2460static int ssl_tls13_session_load( const mbedtls_ssl_session *session,
2461 unsigned char *buf,
2462 size_t buf_len )
2463{
2464 ((void) session);
2465 ((void) buf);
2466 ((void) buf_len);
2467 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
2468}
2469#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002470#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2471
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01002472psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type,
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002473 size_t taglen,
2474 psa_algorithm_t *alg,
2475 psa_key_type_t *key_type,
2476 size_t *key_size )
2477{
2478 switch ( mbedtls_cipher_type )
2479 {
2480 case MBEDTLS_CIPHER_AES_128_CBC:
2481 *alg = PSA_ALG_CBC_NO_PADDING;
2482 *key_type = PSA_KEY_TYPE_AES;
2483 *key_size = 128;
2484 break;
2485 case MBEDTLS_CIPHER_AES_128_CCM:
2486 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2487 *key_type = PSA_KEY_TYPE_AES;
2488 *key_size = 128;
2489 break;
2490 case MBEDTLS_CIPHER_AES_128_GCM:
2491 *alg = PSA_ALG_GCM;
2492 *key_type = PSA_KEY_TYPE_AES;
2493 *key_size = 128;
2494 break;
2495 case MBEDTLS_CIPHER_AES_192_CCM:
2496 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2497 *key_type = PSA_KEY_TYPE_AES;
2498 *key_size = 192;
2499 break;
2500 case MBEDTLS_CIPHER_AES_192_GCM:
2501 *alg = PSA_ALG_GCM;
2502 *key_type = PSA_KEY_TYPE_AES;
2503 *key_size = 192;
2504 break;
2505 case MBEDTLS_CIPHER_AES_256_CBC:
2506 *alg = PSA_ALG_CBC_NO_PADDING;
2507 *key_type = PSA_KEY_TYPE_AES;
2508 *key_size = 256;
2509 break;
2510 case MBEDTLS_CIPHER_AES_256_CCM:
2511 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2512 *key_type = PSA_KEY_TYPE_AES;
2513 *key_size = 256;
2514 break;
2515 case MBEDTLS_CIPHER_AES_256_GCM:
2516 *alg = PSA_ALG_GCM;
2517 *key_type = PSA_KEY_TYPE_AES;
2518 *key_size = 256;
2519 break;
2520 case MBEDTLS_CIPHER_ARIA_128_CBC:
2521 *alg = PSA_ALG_CBC_NO_PADDING;
2522 *key_type = PSA_KEY_TYPE_ARIA;
2523 *key_size = 128;
2524 break;
2525 case MBEDTLS_CIPHER_ARIA_128_CCM:
2526 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2527 *key_type = PSA_KEY_TYPE_ARIA;
2528 *key_size = 128;
2529 break;
2530 case MBEDTLS_CIPHER_ARIA_128_GCM:
2531 *alg = PSA_ALG_GCM;
2532 *key_type = PSA_KEY_TYPE_ARIA;
2533 *key_size = 128;
2534 break;
2535 case MBEDTLS_CIPHER_ARIA_192_CCM:
2536 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2537 *key_type = PSA_KEY_TYPE_ARIA;
2538 *key_size = 192;
2539 break;
2540 case MBEDTLS_CIPHER_ARIA_192_GCM:
2541 *alg = PSA_ALG_GCM;
2542 *key_type = PSA_KEY_TYPE_ARIA;
2543 *key_size = 192;
2544 break;
2545 case MBEDTLS_CIPHER_ARIA_256_CBC:
2546 *alg = PSA_ALG_CBC_NO_PADDING;
2547 *key_type = PSA_KEY_TYPE_ARIA;
2548 *key_size = 256;
2549 break;
2550 case MBEDTLS_CIPHER_ARIA_256_CCM:
2551 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2552 *key_type = PSA_KEY_TYPE_ARIA;
2553 *key_size = 256;
2554 break;
2555 case MBEDTLS_CIPHER_ARIA_256_GCM:
2556 *alg = PSA_ALG_GCM;
2557 *key_type = PSA_KEY_TYPE_ARIA;
2558 *key_size = 256;
2559 break;
2560 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2561 *alg = PSA_ALG_CBC_NO_PADDING;
2562 *key_type = PSA_KEY_TYPE_CAMELLIA;
2563 *key_size = 128;
2564 break;
2565 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
2566 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2567 *key_type = PSA_KEY_TYPE_CAMELLIA;
2568 *key_size = 128;
2569 break;
2570 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2571 *alg = PSA_ALG_GCM;
2572 *key_type = PSA_KEY_TYPE_CAMELLIA;
2573 *key_size = 128;
2574 break;
2575 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
2576 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2577 *key_type = PSA_KEY_TYPE_CAMELLIA;
2578 *key_size = 192;
2579 break;
2580 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2581 *alg = PSA_ALG_GCM;
2582 *key_type = PSA_KEY_TYPE_CAMELLIA;
2583 *key_size = 192;
2584 break;
2585 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2586 *alg = PSA_ALG_CBC_NO_PADDING;
2587 *key_type = PSA_KEY_TYPE_CAMELLIA;
2588 *key_size = 256;
2589 break;
2590 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
2591 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM;
2592 *key_type = PSA_KEY_TYPE_CAMELLIA;
2593 *key_size = 256;
2594 break;
2595 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2596 *alg = PSA_ALG_GCM;
2597 *key_type = PSA_KEY_TYPE_CAMELLIA;
2598 *key_size = 256;
2599 break;
2600 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2601 *alg = PSA_ALG_CHACHA20_POLY1305;
2602 *key_type = PSA_KEY_TYPE_CHACHA20;
2603 *key_size = 256;
2604 break;
2605 case MBEDTLS_CIPHER_NULL:
2606 *alg = MBEDTLS_SSL_NULL_CIPHER;
2607 *key_type = 0;
2608 *key_size = 0;
2609 break;
2610 default:
2611 return PSA_ERROR_NOT_SUPPORTED;
2612 }
2613
2614 return PSA_SUCCESS;
2615}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002616#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002617
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002618#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckera90658f2017-10-04 15:29:08 +01002619int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
2620 const unsigned char *dhm_P, size_t P_len,
2621 const unsigned char *dhm_G, size_t G_len )
2622{
Janos Follath865b3eb2019-12-16 11:46:15 +00002623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002624
Glenn Strausscee11292021-12-20 01:43:17 -05002625 mbedtls_mpi_free( &conf->dhm_P );
2626 mbedtls_mpi_free( &conf->dhm_G );
2627
Hanno Beckera90658f2017-10-04 15:29:08 +01002628 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
2629 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
2630 {
2631 mbedtls_mpi_free( &conf->dhm_P );
2632 mbedtls_mpi_free( &conf->dhm_G );
2633 return( ret );
2634 }
2635
2636 return( 0 );
2637}
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002639int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00002640{
Janos Follath865b3eb2019-12-16 11:46:15 +00002641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002642
Glenn Strausscee11292021-12-20 01:43:17 -05002643 mbedtls_mpi_free( &conf->dhm_P );
2644 mbedtls_mpi_free( &conf->dhm_G );
2645
Gilles Peskinee5702482021-06-11 21:59:08 +02002646 if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P,
2647 &conf->dhm_P ) ) != 0 ||
2648 ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G,
2649 &conf->dhm_G ) ) != 0 )
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002650 {
2651 mbedtls_mpi_free( &conf->dhm_P );
2652 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00002653 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002654 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002655
2656 return( 0 );
2657}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002658#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002659
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002660#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2661/*
2662 * Set the minimum length for Diffie-Hellman parameters
2663 */
2664void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
2665 unsigned int bitlen )
2666{
2667 conf->dhm_min_bitlen = bitlen;
2668}
2669#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2670
Ronald Crone68ab4f2022-10-05 12:46:29 +02002671#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002672#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002673/*
2674 * Set allowed/preferred hashes for handshake signatures
2675 */
2676void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
2677 const int *hashes )
2678{
2679 conf->sig_hashes = hashes;
2680}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002681#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002682
Jerry Yuf017ee42022-01-12 15:49:48 +08002683/* Configure allowed signature algorithms for handshake */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002684void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf,
2685 const uint16_t* sig_algs )
2686{
Jerry Yuf017ee42022-01-12 15:49:48 +08002687#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2688 conf->sig_hashes = NULL;
2689#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2690 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002691}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002692#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002693
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002694#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002695#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002696/*
2697 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002698 *
2699 * mbedtls_ssl_setup() takes the provided list
2700 * and translates it to a list of IANA TLS group identifiers,
2701 * stored in ssl->handshake->group_list.
2702 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002703 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002704void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002705 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002707 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002708 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002709}
Brett Warrene0edc842021-08-17 09:53:13 +01002710#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002711#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002712
Brett Warrene0edc842021-08-17 09:53:13 +01002713/*
2714 * Set the allowed groups
2715 */
2716void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf,
2717 const uint16_t *group_list )
2718{
2719#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2720 conf->curve_list = NULL;
2721#endif
2722 conf->group_list = group_list;
2723}
2724
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002725#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00002727{
Hanno Becker947194e2017-04-07 13:25:49 +01002728 /* Initialize to suppress unnecessary compiler warning */
2729 size_t hostname_len = 0;
2730
2731 /* Check if new hostname is valid before
2732 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01002733 if( hostname != NULL )
2734 {
2735 hostname_len = strlen( hostname );
2736
2737 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
2738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2739 }
2740
2741 /* Now it's clear that we will overwrite the old hostname,
2742 * so we can free it safely */
2743
2744 if( ssl->hostname != NULL )
2745 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05002746 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01002747 mbedtls_free( ssl->hostname );
2748 }
2749
2750 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002751
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01002753 {
2754 ssl->hostname = NULL;
2755 }
2756 else
2757 {
2758 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01002759 if( ssl->hostname == NULL )
2760 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002761
Hanno Becker947194e2017-04-07 13:25:49 +01002762 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002763
Hanno Becker947194e2017-04-07 13:25:49 +01002764 ssl->hostname[hostname_len] = '\0';
2765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 return( 0 );
2768}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002769#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002771#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002772void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00002774 const unsigned char *, size_t),
2775 void *p_sni )
2776{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002777 conf->f_sni = f_sni;
2778 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002783int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002784{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002785 size_t cur_len, tot_len;
2786 const char **p;
2787
2788 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002789 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2790 * MUST NOT be truncated."
2791 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002792 */
2793 tot_len = 0;
2794 for( p = protos; *p != NULL; p++ )
2795 {
2796 cur_len = strlen( *p );
2797 tot_len += cur_len;
2798
Ronald Cron8216dd32020-04-23 16:41:44 +02002799 if( ( cur_len == 0 ) ||
2800 ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
2801 ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002803 }
2804
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002805 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002806
2807 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002808}
2809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002811{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002812 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002815
Johan Pascalb62bb512015-12-03 21:56:45 +01002816#if defined(MBEDTLS_SSL_DTLS_SRTP)
Ron Eldoref72faf2018-07-12 11:54:20 +03002817void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
2818 int support_mki_value )
Ron Eldor591f1622018-01-22 12:30:04 +02002819{
2820 conf->dtls_srtp_mki_support = support_mki_value;
2821}
2822
Ron Eldoref72faf2018-07-12 11:54:20 +03002823int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
2824 unsigned char *mki_value,
Johan Pascalf6417ec2020-09-22 15:15:19 +02002825 uint16_t mki_len )
Ron Eldor591f1622018-01-22 12:30:04 +02002826{
Johan Pascal5ef72d22020-10-28 17:05:47 +01002827 if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
Ron Eldora9788042018-12-05 11:04:31 +02002828 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Ron Eldora9788042018-12-05 11:04:31 +02002830 }
Ron Eldor591f1622018-01-22 12:30:04 +02002831
2832 if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
Ron Eldora9788042018-12-05 11:04:31 +02002833 {
Johan Pascald576fdb2020-09-22 10:39:53 +02002834 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Ron Eldora9788042018-12-05 11:04:31 +02002835 }
Ron Eldor591f1622018-01-22 12:30:04 +02002836
2837 memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
2838 ssl->dtls_srtp_info.mki_len = mki_len;
Ron Eldora9788042018-12-05 11:04:31 +02002839 return( 0 );
Ron Eldor591f1622018-01-22 12:30:04 +02002840}
2841
Ron Eldoref72faf2018-07-12 11:54:20 +03002842int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
Johan Pascal253d0262020-09-22 13:04:45 +02002843 const mbedtls_ssl_srtp_profile *profiles )
Johan Pascalb62bb512015-12-03 21:56:45 +01002844{
Johan Pascal253d0262020-09-22 13:04:45 +02002845 const mbedtls_ssl_srtp_profile *p;
2846 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002847
Johan Pascal253d0262020-09-22 13:04:45 +02002848 /* check the profiles list: all entry must be valid,
2849 * its size cannot be more than the total number of supported profiles, currently 4 */
Johan Pascald387aa02020-09-23 18:47:56 +02002850 for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2851 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2852 p++ )
Johan Pascald576fdb2020-09-22 10:39:53 +02002853 {
Johan Pascal5ef72d22020-10-28 17:05:47 +01002854 if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
Johan Pascald576fdb2020-09-22 10:39:53 +02002855 {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002856 list_size++;
2857 }
2858 else
2859 {
2860 /* unsupported value, stop parsing and set the size to an error value */
2861 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002862 }
2863 }
2864
Johan Pascal5ef72d22020-10-28 17:05:47 +01002865 if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
Johan Pascald387aa02020-09-23 18:47:56 +02002866 {
Johan Pascal253d0262020-09-22 13:04:45 +02002867 conf->dtls_srtp_profile_list = NULL;
2868 conf->dtls_srtp_profile_list_len = 0;
2869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2870 }
2871
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002872 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002873 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002874
2875 return( 0 );
2876}
2877
Johan Pascal5ef72d22020-10-28 17:05:47 +01002878void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
2879 mbedtls_dtls_srtp_info *dtls_srtp_info )
Johan Pascalb62bb512015-12-03 21:56:45 +01002880{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002881 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2882 /* do not copy the mki value if there is no chosen profile */
Johan Pascal5ef72d22020-10-28 17:05:47 +01002883 if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002884 {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002885 dtls_srtp_info->mki_len = 0;
Johan Pascal0dbcd1d2020-10-28 11:03:07 +01002886 }
Johan Pascal2258a4f2020-10-28 13:53:09 +01002887 else
2888 {
2889 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Johan Pascal5ef72d22020-10-28 17:05:47 +01002890 memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2891 ssl->dtls_srtp_info.mki_len );
Johan Pascal2258a4f2020-10-28 13:53:09 +01002892 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002893}
Johan Pascalb62bb512015-12-03 21:56:45 +01002894#endif /* MBEDTLS_SSL_DTLS_SRTP */
2895
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302896#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002897void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00002898{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002899 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002900}
2901
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02002902void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00002903{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002904 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00002905}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302906#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002907
Janos Follath088ce432017-04-10 12:42:31 +01002908#if defined(MBEDTLS_SSL_SRV_C)
2909void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
2910 char cert_req_ca_list )
2911{
2912 conf->cert_req_ca_list = cert_req_ca_list;
2913}
2914#endif
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002917void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002918{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002919 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002920}
2921#endif
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002924void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002926 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002927}
2928#endif
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002931int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10002934 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002937 }
2938
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002939 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002940
2941 return( 0 );
2942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002944
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002945void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00002946{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002947 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002948}
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002951void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002952{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002953 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002954}
2955
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002956void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002958 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002959}
2960
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02002961void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002962 const unsigned char period[8] )
2963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002964 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002969#if defined(MBEDTLS_SSL_CLI_C)
2970void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002971{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002972 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002973}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002974#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002975
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002976#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002977
Jerry Yud0766ec2022-09-22 10:46:57 +08002978#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002979void mbedtls_ssl_conf_new_session_tickets( mbedtls_ssl_config *conf,
2980 uint16_t num_tickets )
2981{
Jerry Yud0766ec2022-09-22 10:46:57 +08002982 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002983}
2984#endif
2985
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002986void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
2987 mbedtls_ssl_ticket_write_t *f_ticket_write,
2988 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2989 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02002990{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002991 conf->f_ticket_write = f_ticket_write;
2992 conf->f_ticket_parse = f_ticket_parse;
2993 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002994}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002997
Hanno Becker7e6c1782021-06-08 09:24:55 +01002998void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl,
2999 mbedtls_ssl_export_keys_t *f_export_keys,
3000 void *p_export_keys )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003001{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003002 ssl->f_export_keys = f_export_keys;
3003 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003004}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003005
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003006#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003007void mbedtls_ssl_conf_async_private_cb(
3008 mbedtls_ssl_config *conf,
3009 mbedtls_ssl_async_sign_t *f_async_sign,
3010 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3011 mbedtls_ssl_async_resume_t *f_async_resume,
3012 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003013 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003014{
3015 conf->f_async_sign_start = f_async_sign;
3016 conf->f_async_decrypt_start = f_async_decrypt;
3017 conf->f_async_resume = f_async_resume;
3018 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003019 conf->p_async_config_data = async_config_data;
3020}
3021
Gilles Peskine8f97af72018-04-26 11:46:10 +02003022void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
3023{
3024 return( conf->p_async_config_data );
3025}
3026
Gilles Peskine1febfef2018-04-30 11:54:39 +02003027void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003028{
3029 if( ssl->handshake == NULL )
3030 return( NULL );
3031 else
3032 return( ssl->handshake->user_async_ctx );
3033}
3034
Gilles Peskine1febfef2018-04-30 11:54:39 +02003035void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003036 void *ctx )
3037{
3038 if( ssl->handshake != NULL )
3039 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003040}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003041#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003042
Paul Bakker5121ce52009-01-03 21:22:43 +00003043/*
3044 * SSL get accessors
3045 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02003046uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003048 if( ssl->session != NULL )
3049 return( ssl->session->verify_result );
3050
3051 if( ssl->session_negotiate != NULL )
3052 return( ssl->session_negotiate->verify_result );
3053
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02003054 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055}
3056
Glenn Strauss8f526902022-01-13 00:04:49 -05003057int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl )
3058{
3059 if( ssl == NULL || ssl->session == NULL )
3060 return( 0 );
3061
3062 return( ssl->session->ciphersuite );
3063}
3064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003066{
Paul Bakker926c8e42013-03-06 10:23:34 +01003067 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003068 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003071}
3072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003077 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003078 switch( ssl->tls_version )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003079 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003080 case MBEDTLS_SSL_VERSION_TLS1_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003081 return( "DTLSv1.2" );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003082 default:
3083 return( "unknown (DTLS)" );
3084 }
3085 }
3086#endif
3087
Glenn Strauss60bfe602022-03-14 19:04:24 -04003088 switch( ssl->tls_version )
Paul Bakker43ca69c2011-01-15 17:35:19 +00003089 {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003090 case MBEDTLS_SSL_VERSION_TLS1_2:
Paul Bakker1ef83d62012-04-11 12:09:53 +00003091 return( "TLSv1.2" );
Glenn Strauss60bfe602022-03-14 19:04:24 -04003092 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskinec63a1e02022-01-13 01:10:24 +01003093 return( "TLSv1.3" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003094 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003095 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00003096 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003097}
3098
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003099#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003100size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
3101{
David Horstmann95d516f2021-05-04 18:36:56 +01003102 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003103 size_t read_mfl;
3104
3105 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
3106 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3107 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
3108 {
3109 return ssl_mfl_code_to_length( ssl->conf->mfl_code );
3110 }
3111
3112 /* Check if a smaller max length was negotiated */
3113 if( ssl->session_out != NULL )
3114 {
3115 read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
3116 if( read_mfl < max_len )
3117 {
3118 max_len = read_mfl;
3119 }
3120 }
3121
3122 // During a handshake, use the value being negotiated
3123 if( ssl->session_negotiate != NULL )
3124 {
3125 read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3126 if( read_mfl < max_len )
3127 {
3128 max_len = read_mfl;
3129 }
3130 }
3131
3132 return( max_len );
3133}
3134
3135size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003136{
3137 size_t max_len;
3138
3139 /*
3140 * Assume mfl_code is correct since it was checked when set
3141 */
Angus Grattond8213d02016-05-25 20:56:48 +10003142 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003143
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003144 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003145 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10003146 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003147 {
Angus Grattond8213d02016-05-25 20:56:48 +10003148 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003149 }
3150
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003151 /* During a handshake, use the value being negotiated */
3152 if( ssl->session_negotiate != NULL &&
3153 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
3154 {
3155 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
3156 }
3157
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003158 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003159}
3160#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3161
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003163size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003164{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003165 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
3166 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3167 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3168 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
3169 return ( 0 );
3170
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003171 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
3172 return( ssl->mtu );
3173
3174 if( ssl->mtu == 0 )
3175 return( ssl->handshake->mtu );
3176
3177 return( ssl->mtu < ssl->handshake->mtu ?
3178 ssl->mtu : ssl->handshake->mtu );
3179}
3180#endif /* MBEDTLS_SSL_PROTO_DTLS */
3181
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003182int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
3183{
3184 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3185
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003186#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3187 !defined(MBEDTLS_SSL_PROTO_DTLS)
3188 (void) ssl;
3189#endif
3190
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003191#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003192 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003193
3194 if( max_len > mfl )
3195 max_len = mfl;
3196#endif
3197
3198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker89490712020-02-05 10:50:12 +00003199 if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003200 {
Hanno Becker89490712020-02-05 10:50:12 +00003201 const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003202 const int ret = mbedtls_ssl_get_record_expansion( ssl );
3203 const size_t overhead = (size_t) ret;
3204
3205 if( ret < 0 )
3206 return( ret );
3207
3208 if( mtu <= overhead )
3209 {
3210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
3211 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3212 }
3213
3214 if( max_len > mtu - overhead )
3215 max_len = mtu - overhead;
3216 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003217#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003218
Hanno Becker0defedb2018-08-10 12:35:02 +01003219#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3220 !defined(MBEDTLS_SSL_PROTO_DTLS)
3221 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003222#endif
3223
3224 return( (int) max_len );
3225}
3226
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003227int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl )
3228{
3229 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3230
3231#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3232 (void) ssl;
3233#endif
3234
3235#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3236 const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl );
3237
3238 if( max_len > mfl )
3239 max_len = mfl;
3240#endif
3241
3242 return( (int) max_len );
3243}
3244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_X509_CRT_PARSE_C)
3246const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003247{
3248 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003249 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003250
Hanno Beckere6824572019-02-07 13:18:46 +00003251#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003252 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00003253#else
3254 return( NULL );
3255#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00003260int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
3261 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003262{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003263 int ret;
3264
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003265 if( ssl == NULL ||
3266 dst == NULL ||
3267 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003268 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003271 }
3272
Hanno Beckere810bbc2021-05-14 16:01:05 +01003273 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3274 * idempotent: Each session can only be exported once.
3275 *
3276 * (This is in preparation for TLS 1.3 support where we will
3277 * need the ability to export multiple sessions (aka tickets),
3278 * which will be achieved by calling mbedtls_ssl_get_session()
3279 * multiple times until it fails.)
3280 *
3281 * Check whether we have already exported the current session,
3282 * and fail if so.
3283 */
3284 if( ssl->session->exported == 1 )
3285 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3286
3287 ret = mbedtls_ssl_session_copy( dst, ssl->session );
3288 if( ret != 0 )
3289 return( ret );
3290
3291 /* Remember that we've exported the session. */
3292 ssl->session->exported = 1;
3293 return( 0 );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003296
Paul Bakker5121ce52009-01-03 21:22:43 +00003297/*
Hanno Beckera835da52019-05-16 12:39:07 +01003298 * Define ticket header determining Mbed TLS version
3299 * and structure of the ticket.
3300 */
3301
Hanno Becker94ef3b32019-05-16 12:50:45 +01003302/*
Hanno Becker50b59662019-05-28 14:30:45 +01003303 * Define bitflag determining compile-time settings influencing
3304 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003305 */
3306
Hanno Becker50b59662019-05-28 14:30:45 +01003307#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003308#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003309#else
Hanno Becker3e088662019-05-29 11:10:18 +01003310#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003311#endif /* MBEDTLS_HAVE_TIME */
3312
3313#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003314#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003315#else
Hanno Becker3e088662019-05-29 11:10:18 +01003316#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003317#endif /* MBEDTLS_X509_CRT_PARSE_C */
3318
3319#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003320#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003321#else
Hanno Becker3e088662019-05-29 11:10:18 +01003322#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003323#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3324
3325#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003326#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003327#else
Hanno Becker3e088662019-05-29 11:10:18 +01003328#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003329#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3330
Hanno Becker94ef3b32019-05-16 12:50:45 +01003331#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003332#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003333#else
Hanno Becker3e088662019-05-29 11:10:18 +01003334#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003335#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3336
Hanno Becker94ef3b32019-05-16 12:50:45 +01003337#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3338#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3339#else
3340#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3341#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3342
Hanno Becker3e088662019-05-29 11:10:18 +01003343#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3344#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3345#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3346#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003347#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3348#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003349
Hanno Becker50b59662019-05-28 14:30:45 +01003350#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Becker3e088662019-05-29 11:10:18 +01003351 ( (uint16_t) ( \
3352 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
3353 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
3354 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
3355 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
Hanno Becker3e088662019-05-29 11:10:18 +01003356 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01003357 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker94ef3b32019-05-16 12:50:45 +01003358
Hanno Beckerf8787072019-05-16 12:41:07 +01003359static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003360 MBEDTLS_VERSION_MAJOR,
3361 MBEDTLS_VERSION_MINOR,
3362 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01003363 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
3364 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
Hanno Beckerf8787072019-05-16 12:41:07 +01003365};
Hanno Beckera835da52019-05-16 12:39:07 +01003366
3367/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003368 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003369 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003370 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003371 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003372 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003373 * opaque mbedtls_version[3]; // library version: major, minor, patch
3374 * opaque session_format[2]; // library-version specific 16-bit field
3375 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003376 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003377 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003378 * Note: When updating the format, remember to keep
3379 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003380 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003381 * // In this version, `session_format` determines
3382 * // the setting of those compile-time
3383 * // configuration options which influence
3384 * // the structure of mbedtls_ssl_session.
3385 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003386 * uint8_t minor_ver; // Protocol minor version. Possible values:
3387 * // - TLS 1.2 (3)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003388 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003389 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003390 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003391 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003392 * serialized_session_tls12 data;
3393 *
3394 * };
3395 *
3396 * } serialized_session;
3397 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003398 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003399
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003400MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003401static int ssl_session_save( const mbedtls_ssl_session *session,
3402 unsigned char omit_header,
3403 unsigned char *buf,
3404 size_t buf_len,
3405 size_t *olen )
3406{
3407 unsigned char *p = buf;
3408 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003409 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003410#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3411 size_t out_len;
3412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3413#endif
Jerry Yu438ddd82022-07-07 06:55:50 +00003414 if( session == NULL )
3415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3416
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003417 if( !omit_header )
3418 {
3419 /*
3420 * Add Mbed TLS version identifier
3421 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003422 used += sizeof( ssl_serialized_session_header );
3423
3424 if( used <= buf_len )
3425 {
3426 memcpy( p, ssl_serialized_session_header,
3427 sizeof( ssl_serialized_session_header ) );
3428 p += sizeof( ssl_serialized_session_header );
3429 }
3430 }
3431
3432 /*
3433 * TLS version identifier
3434 */
3435 used += 1;
3436 if( used <= buf_len )
3437 {
Glenn Straussda7851c2022-03-14 13:29:48 -04003438 *p++ = MBEDTLS_BYTE_0( session->tls_version );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003439 }
3440
3441 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003442 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Glenn Straussda7851c2022-03-14 13:29:48 -04003443 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003444 {
3445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003446 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003447 used += ssl_tls12_session_save( session, p, remaining_len );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003448 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3450
Jerry Yu251a12e2022-07-13 15:15:48 +08003451#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3452 case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yue36fdd62022-08-17 21:31:36 +08003453 ret = ssl_tls13_session_save( session, p, remaining_len, &out_len );
3454 if( ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
Jerry Yue36fdd62022-08-17 21:31:36 +08003455 return( ret );
Jerry Yue36fdd62022-08-17 21:31:36 +08003456 used += out_len;
Jerry Yu251a12e2022-07-13 15:15:48 +08003457 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003458#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3459
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003460 default:
3461 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3462 }
3463
3464 *olen = used;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02003465 if( used > buf_len )
3466 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003467
3468 return( 0 );
3469}
3470
3471/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003472 * Public wrapper for ssl_session_save()
3473 */
3474int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
3475 unsigned char *buf,
3476 size_t buf_len,
3477 size_t *olen )
3478{
3479 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
3480}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003481
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003482/*
3483 * Deserialize session, see mbedtls_ssl_session_save() for format.
3484 *
3485 * This internal version is wrapped by a public function that cleans up in
3486 * case of error, and has an extra option omit_header.
3487 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003488MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003489static int ssl_session_load( mbedtls_ssl_session *session,
3490 unsigned char omit_header,
3491 const unsigned char *buf,
3492 size_t len )
3493{
3494 const unsigned char *p = buf;
3495 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003496 size_t remaining_len;
3497
3498
3499 if( session == NULL )
3500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501
3502 if( !omit_header )
3503 {
3504 /*
3505 * Check Mbed TLS version identifier
3506 */
3507
3508 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
3509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3510
3511 if( memcmp( p, ssl_serialized_session_header,
3512 sizeof( ssl_serialized_session_header ) ) != 0 )
3513 {
3514 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
3515 }
3516 p += sizeof( ssl_serialized_session_header );
3517 }
3518
3519 /*
3520 * TLS version identifier
3521 */
3522 if( 1 > (size_t)( end - p ) )
3523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Glenn Straussda7851c2022-03-14 13:29:48 -04003524 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003525
3526 /* Dispatch according to TLS version. */
Jerry Yu438ddd82022-07-07 06:55:50 +00003527 remaining_len = ( end - p );
Glenn Straussda7851c2022-03-14 13:29:48 -04003528 switch( session->tls_version )
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003529 {
3530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Straussda7851c2022-03-14 13:29:48 -04003531 case MBEDTLS_SSL_VERSION_TLS1_2:
Jerry Yu438ddd82022-07-07 06:55:50 +00003532 return( ssl_tls12_session_load( session, p, remaining_len ) );
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003533#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3534
Jerry Yu438ddd82022-07-07 06:55:50 +00003535#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3536 case MBEDTLS_SSL_VERSION_TLS1_3:
3537 return( ssl_tls13_session_load( session, p, remaining_len ) );
3538#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3539
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003540 default:
3541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3542 }
3543}
3544
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003545/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003546 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003547 */
3548int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
3549 const unsigned char *buf,
3550 size_t len )
3551{
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003552 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003553
3554 if( ret != 0 )
3555 mbedtls_ssl_session_free( session );
3556
3557 return( ret );
3558}
3559
3560/*
Paul Bakker1961b702013-01-25 14:49:24 +01003561 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003563MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker41934dd2021-08-07 19:13:43 +01003564static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl )
3565{
3566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3567
Ronald Cron66dbf912022-02-02 15:33:46 +01003568 /*
3569 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003570 * that were written into the output buffer by the previous handshake step,
3571 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003572 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3573 * We proceed to the next handshake step only when all data from the
3574 * previous one have been sent to the peer, thus we make sure that this is
3575 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3576 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3577 * we have to wait before to go ahead.
3578 * In the case of TLS 1.3, handshake step handlers do not send data to the
3579 * peer. Data are only sent here and through
3580 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003581 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003582 */
Hanno Becker41934dd2021-08-07 19:13:43 +01003583 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3584 return( ret );
3585
3586#if defined(MBEDTLS_SSL_PROTO_DTLS)
3587 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3588 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
3589 {
3590 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
3591 return( ret );
3592 }
3593#endif /* MBEDTLS_SSL_PROTO_DTLS */
3594
3595 return( ret );
3596}
3597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003599{
Hanno Becker41934dd2021-08-07 19:13:43 +01003600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Hanno Becker41934dd2021-08-07 19:13:43 +01003602 if( ssl == NULL ||
3603 ssl->conf == NULL ||
3604 ssl->handshake == NULL ||
Jerry Yu6848a612022-10-27 13:03:26 +08003605 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker41934dd2021-08-07 19:13:43 +01003606 {
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker41934dd2021-08-07 19:13:43 +01003608 }
3609
3610 ret = ssl_prepare_handshake_step( ssl );
3611 if( ret != 0 )
3612 return( ret );
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003613
Jerry Yue7047812021-09-13 19:26:39 +08003614 ret = mbedtls_ssl_handle_pending_alert( ssl );
3615 if( ret != 0 )
3616 goto cleanup;
3617
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003618 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3619 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3620 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Jerry Yub9930e72021-08-06 17:11:51 +08003624 {
Ronald Cron27c85e72022-03-08 11:37:55 +01003625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s",
3626 mbedtls_ssl_states_str( ssl->state ) ) );
Jerry Yub9930e72021-08-06 17:11:51 +08003627
Ronald Cron9f0fba32022-02-10 16:45:15 +01003628 switch( ssl->state )
3629 {
3630 case MBEDTLS_SSL_HELLO_REQUEST:
3631 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003632 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003633 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003634
Ronald Cron9f0fba32022-02-10 16:45:15 +01003635 case MBEDTLS_SSL_CLIENT_HELLO:
3636 ret = mbedtls_ssl_write_client_hello( ssl );
3637 break;
3638
3639 default:
3640#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003641 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron9f0fba32022-02-10 16:45:15 +01003642 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3643 else
3644 ret = mbedtls_ssl_handshake_client_step( ssl );
3645#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3646 ret = mbedtls_ssl_handshake_client_step( ssl );
3647#else
3648 ret = mbedtls_ssl_tls13_handshake_client_step( ssl );
3649#endif
3650 }
Jerry Yub9930e72021-08-06 17:11:51 +08003651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003652#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003654 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Jerry Yub9930e72021-08-06 17:11:51 +08003655 {
Ronald Cron6f135e12021-12-08 16:57:54 +01003656#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yub9930e72021-08-06 17:11:51 +08003657 if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
Jerry Yu27561932021-08-27 17:07:38 +08003658 ret = mbedtls_ssl_tls13_handshake_server_step( ssl );
Ronald Cron6f135e12021-12-08 16:57:54 +01003659#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003660
3661#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3662 if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3663 ret = mbedtls_ssl_handshake_server_step( ssl );
3664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003666#endif
3667
Jerry Yue7047812021-09-13 19:26:39 +08003668 if( ret != 0 )
3669 {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003670 /* handshake_step return error. And it is same
3671 * with alert_reason.
3672 */
Jerry Yu3bf1f972021-09-22 21:37:18 +08003673 if( ssl->send_alert )
Jerry Yue7047812021-09-13 19:26:39 +08003674 {
Jerry Yu3bf1f972021-09-22 21:37:18 +08003675 ret = mbedtls_ssl_handle_pending_alert( ssl );
Jerry Yue7047812021-09-13 19:26:39 +08003676 goto cleanup;
3677 }
3678 }
3679
3680cleanup:
Paul Bakker1961b702013-01-25 14:49:24 +01003681 return( ret );
3682}
3683
3684/*
3685 * Perform the SSL handshake
3686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01003688{
3689 int ret = 0;
3690
Hanno Beckera817ea42020-10-20 15:20:23 +01003691 /* Sanity checks */
3692
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003693 if( ssl == NULL || ssl->conf == NULL )
3694 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3695
Hanno Beckera817ea42020-10-20 15:20:23 +01003696#if defined(MBEDTLS_SSL_PROTO_DTLS)
3697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3698 ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
3699 {
3700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3701 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3702 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3703 }
3704#endif /* MBEDTLS_SSL_PROTO_DTLS */
3705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01003707
Hanno Beckera817ea42020-10-20 15:20:23 +01003708 /* Main handshake loop */
Jerry Yu6848a612022-10-27 13:03:26 +08003709 while( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker1961b702013-01-25 14:49:24 +01003710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003712
3713 if( ret != 0 )
3714 break;
3715 }
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
3719 return( ret );
3720}
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#if defined(MBEDTLS_SSL_RENEGOTIATION)
3723#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003724/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003725 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003726 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003727MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003729{
Janos Follath865b3eb2019-12-16 11:46:15 +00003730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003733
3734 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3736 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003737
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003738 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003739 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003740 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003741 return( ret );
3742 }
3743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003745
3746 return( 0 );
3747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003749
3750/*
3751 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752 * - any side: calling mbedtls_ssl_renegotiate(),
3753 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3754 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003755 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003756 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003758 */
Hanno Becker40cdaa12020-02-05 10:48:27 +00003759int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003760{
Janos Follath865b3eb2019-12-16 11:46:15 +00003761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003764
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003765 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3766 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003767
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003768 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3769 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003771 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003773 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003774 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003775 ssl->handshake->out_msg_seq = 1;
3776 else
3777 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003778 }
3779#endif
3780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3782 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00003787 return( ret );
3788 }
3789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003791
3792 return( 0 );
3793}
3794
3795/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003796 * Renegotiate current connection on client,
3797 * or request renegotiation on server
3798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003802
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003803 if( ssl == NULL || ssl->conf == NULL )
3804 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003807 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003808 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003809 {
Jerry Yu6848a612022-10-27 13:03:26 +08003810 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003814
3815 /* Did we already try/start sending HelloRequest? */
3816 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003818
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003819 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003824 /*
3825 * On client, either start the renegotiation process or,
3826 * if already in progress, continue the handshake
3827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003829 {
Jerry Yu6848a612022-10-27 13:03:26 +08003830 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003832
Hanno Becker40cdaa12020-02-05 10:48:27 +00003833 if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003834 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00003835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003836 return( ret );
3837 }
3838 }
3839 else
3840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003844 return( ret );
3845 }
3846 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003848
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003849 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003852
Gilles Peskine9b562d52018-04-25 20:32:43 +02003853void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003854{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003855 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3856
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003857 if( handshake == NULL )
3858 return;
3859
Brett Warrene0edc842021-08-17 09:53:13 +01003860#if defined(MBEDTLS_ECP_C)
3861#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3862 if ( ssl->handshake->group_list_heap_allocated )
3863 mbedtls_free( (void*) handshake->group_list );
3864 handshake->group_list = NULL;
3865#endif /* MBEDTLS_DEPRECATED_REMOVED */
3866#endif /* MBEDTLS_ECP_C */
3867
Ronald Crone68ab4f2022-10-05 12:46:29 +02003868#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003869#if !defined(MBEDTLS_DEPRECATED_REMOVED)
3870 if ( ssl->handshake->sig_algs_heap_allocated )
3871 mbedtls_free( (void*) handshake->sig_algs );
3872 handshake->sig_algs = NULL;
3873#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003874#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3875 if( ssl->handshake->certificate_request_context )
3876 {
3877 mbedtls_free( (void*) handshake->certificate_request_context );
3878 }
3879#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003880#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003881
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003882#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3883 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
3884 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02003885 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003886 handshake->async_in_progress = 0;
3887 }
3888#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3889
Andrzej Kurek25f27152022-08-17 16:09:31 -04003890#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003891#if defined(MBEDTLS_USE_PSA_CRYPTO)
3892 psa_hash_abort( &handshake->fin_sha256_psa );
3893#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003894 mbedtls_sha256_free( &handshake->fin_sha256 );
3895#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003896#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003897#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003899 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05003900#else
Andrzej Kureka242e832022-08-11 10:03:14 -04003901 mbedtls_sha512_free( &handshake->fin_sha384 );
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003902#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003903#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905#if defined(MBEDTLS_DHM_C)
3906 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00003907#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02003908#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02003910#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003911#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003912 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003913#if defined(MBEDTLS_SSL_CLI_C)
3914 mbedtls_free( handshake->ecjpake_cache );
3915 handshake->ecjpake_cache = NULL;
3916 handshake->ecjpake_cache_len = 0;
3917#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003918#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02003919
Janos Follath4ae5c292016-02-10 11:27:43 +00003920#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
3921 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02003922 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02003924#endif
3925
Ronald Cron73fe8df2022-10-05 14:31:43 +02003926#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02003927#if defined(MBEDTLS_USE_PSA_CRYPTO)
3928 if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
3929 {
3930 /* The maintenance of the external PSK key slot is the
3931 * user's responsibility. */
3932 if( ssl->handshake->psk_opaque_is_internal )
3933 {
3934 psa_destroy_key( ssl->handshake->psk_opaque );
3935 ssl->handshake->psk_opaque_is_internal = 0;
3936 }
3937 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
3938 }
Neil Armstronge952a302022-05-03 10:22:14 +02003939#else
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003940 if( handshake->psk != NULL )
3941 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003942 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003943 mbedtls_free( handshake->psk );
3944 }
Neil Armstronge952a302022-05-03 10:22:14 +02003945#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02003946#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01003947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3949 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02003950 /*
3951 * Free only the linked list wrapper, not the keys themselves
3952 * since the belong to the SNI callback
3953 */
Glenn Strauss36872db2022-01-22 05:06:31 -05003954 ssl_key_cert_free( handshake->sni_key_cert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02003956
Gilles Peskineeccd8882020-03-10 12:19:08 +01003957#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02003958 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00003959 if( handshake->ecrs_peer_cert != NULL )
3960 {
3961 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
3962 mbedtls_free( handshake->ecrs_peer_cert );
3963 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003964#endif
3965
Hanno Becker75173122019-02-06 16:18:31 +00003966#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3967 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3968 mbedtls_pk_free( &handshake->peer_pubkey );
3969#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3970
XiaokangQian9b93c0d2022-02-09 06:02:25 +00003971#if defined(MBEDTLS_SSL_CLI_C) && \
3972 ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
3973 mbedtls_free( handshake->cookie );
3974#endif /* MBEDTLS_SSL_CLI_C &&
3975 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00003976
3977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker533ab5f2020-02-05 10:49:13 +00003978 mbedtls_ssl_flight_free( handshake->flight );
3979 mbedtls_ssl_buffering_free( ssl );
XiaokangQian8499b6c2022-01-27 09:00:11 +00003980#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02003981
Ronald Cronf12b81d2022-03-15 10:42:41 +01003982#if defined(MBEDTLS_ECDH_C) && \
3983 ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) )
Neil Armstrongf716a702022-04-04 11:23:46 +02003984 if( handshake->ecdh_psa_privkey_is_external == 0 )
Neil Armstrong8113d252022-03-23 10:57:04 +01003985 psa_destroy_key( handshake->ecdh_psa_privkey );
Hanno Becker4a63ed42019-01-08 11:39:35 +00003986#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
3987
Ronald Cron6f135e12021-12-08 16:57:54 +01003988#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yua1a568c2021-11-09 10:17:21 +08003989 mbedtls_ssl_transform_free( handshake->transform_handshake );
3990 mbedtls_ssl_transform_free( handshake->transform_earlydata );
Jerry Yuba9c7272021-10-30 11:54:10 +08003991 mbedtls_free( handshake->transform_earlydata );
3992 mbedtls_free( handshake->transform_handshake );
Ronald Cron6f135e12021-12-08 16:57:54 +01003993#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08003994
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003995
3996#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3997 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
3998 * processes datagrams and the fact that a datagram is allowed to have
3999 * several records in it, it is possible that the I/O buffers are not
4000 * empty at this stage */
Andrzej Kurek4a063792020-10-21 15:08:44 +02004001 handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
4002 mbedtls_ssl_get_output_buflen( ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004003#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004004
Jerry Yuba9c7272021-10-30 11:54:10 +08004005 /* mbedtls_platform_zeroize MUST be last one in this function */
4006 mbedtls_platform_zeroize( handshake,
4007 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004008}
4009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00004011{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004012 if( session == NULL )
4013 return;
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00004016 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02004017#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004018
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004019#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004020#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4021 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004022 mbedtls_free( session->hostname );
4023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004024 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004025#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004026
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004027 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004028}
4029
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004030#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004031
4032#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4033#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4034#else
4035#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4036#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4037
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004038#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004039
4040#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4041#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4042#else
4043#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4044#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4045
4046#if defined(MBEDTLS_SSL_ALPN)
4047#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4048#else
4049#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4050#endif /* MBEDTLS_SSL_ALPN */
4051
4052#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4053#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4054#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4055#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4056
4057#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
4058 ( (uint32_t) ( \
4059 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
4060 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
4061 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
4062 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
4063 0u ) )
4064
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004065static unsigned char ssl_serialized_context_header[] = {
4066 MBEDTLS_VERSION_MAJOR,
4067 MBEDTLS_VERSION_MINOR,
4068 MBEDTLS_VERSION_PATCH,
Joe Subbiani2194dc42021-07-14 12:31:31 +01004069 MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4070 MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
4071 MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4072 MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
4073 MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004074};
4075
Paul Bakker5121ce52009-01-03 21:22:43 +00004076/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004077 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004078 *
4079 * The format of the serialized data is:
4080 * (in the presentation language of TLS, RFC 8446 section 3)
4081 *
4082 * // header
4083 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004084 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004085 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004086 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004087 * Note: When updating the format, remember to keep these
4088 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004089 *
4090 * // session sub-structure
4091 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4092 * // transform sub-structure
4093 * uint8 random[64]; // ServerHello.random+ClientHello.random
4094 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4095 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4096 * // fields from ssl_context
4097 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4098 * uint64 in_window_top; // DTLS: last validated record seq_num
4099 * uint64 in_window; // DTLS: bitmask for replay protection
4100 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4101 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4102 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4103 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4104 *
4105 * Note that many fields of the ssl_context or sub-structures are not
4106 * serialized, as they fall in one of the following categories:
4107 *
4108 * 1. forced value (eg in_left must be 0)
4109 * 2. pointer to dynamically-allocated memory (eg session, transform)
4110 * 3. value can be re-derived from other data (eg session keys from MS)
4111 * 4. value was temporary (eg content of input buffer)
4112 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004113 */
4114int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
4115 unsigned char *buf,
4116 size_t buf_len,
4117 size_t *olen )
4118{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004119 unsigned char *p = buf;
4120 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004121 size_t session_len;
4122 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004123
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004124 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004125 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4126 * this function's documentation.
4127 *
4128 * These are due to assumptions/limitations in the implementation. Some of
4129 * them are likely to stay (no handshake in progress) some might go away
4130 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004131 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004132 /* The initial handshake must be over */
Jerry Yu6848a612022-10-27 13:03:26 +08004133 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004134 {
4135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004137 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004138 if( ssl->handshake != NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004139 {
4140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004142 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004143 /* Double-check that sub-structures are indeed ready */
4144 if( ssl->transform == NULL || ssl->session == NULL )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004148 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004149 /* There must be no pending incoming or outgoing data */
4150 if( mbedtls_ssl_check_pending( ssl ) != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004151 {
4152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004154 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004155 if( ssl->out_left != 0 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004159 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004160 /* Protocol must be DLTS, not TLS */
4161 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004162 {
4163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004165 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004166 /* Version must be 1.2 */
Glenn Strauss60bfe602022-03-14 19:04:24 -04004167 if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004171 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004172 /* We must be using an AEAD ciphersuite */
4173 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004177 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004178 /* Renegotiation must not be enabled */
4179#if defined(MBEDTLS_SSL_RENEGOTIATION)
4180 if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004184 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004185#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004186
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004187 /*
4188 * Version and format identifier
4189 */
4190 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004191
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004192 if( used <= buf_len )
4193 {
4194 memcpy( p, ssl_serialized_context_header,
4195 sizeof( ssl_serialized_context_header ) );
4196 p += sizeof( ssl_serialized_context_header );
4197 }
4198
4199 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004200 * Session (length + data)
4201 */
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004202 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004203 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
4204 return( ret );
4205
4206 used += 4 + session_len;
4207 if( used <= buf_len )
4208 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004209 MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
4210 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004211
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004212 ret = ssl_session_save( ssl->session, 1,
4213 p, session_len, &session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004214 if( ret != 0 )
4215 return( ret );
4216
4217 p += session_len;
4218 }
4219
4220 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004221 * Transform
4222 */
4223 used += sizeof( ssl->transform->randbytes );
4224 if( used <= buf_len )
4225 {
4226 memcpy( p, ssl->transform->randbytes,
4227 sizeof( ssl->transform->randbytes ) );
4228 p += sizeof( ssl->transform->randbytes );
4229 }
4230
4231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4232 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
4233 if( used <= buf_len )
4234 {
4235 *p++ = ssl->transform->in_cid_len;
4236 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
4237 p += ssl->transform->in_cid_len;
4238
4239 *p++ = ssl->transform->out_cid_len;
4240 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
4241 p += ssl->transform->out_cid_len;
4242 }
4243#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4244
4245 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004246 * Saved fields from top-level ssl_context structure
4247 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004248 used += 4;
4249 if( used <= buf_len )
4250 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004251 MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
4252 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004253 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004254
4255#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4256 used += 16;
4257 if( used <= buf_len )
4258 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004259 MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
4260 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004261
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004262 MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
4263 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004264 }
4265#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4266
4267#if defined(MBEDTLS_SSL_PROTO_DTLS)
4268 used += 1;
4269 if( used <= buf_len )
4270 {
4271 *p++ = ssl->disable_datagram_packing;
4272 }
4273#endif /* MBEDTLS_SSL_PROTO_DTLS */
4274
Jerry Yuae0b2e22021-10-08 15:21:19 +08004275 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004276 if( used <= buf_len )
4277 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004278 memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
4279 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004280 }
4281
4282#if defined(MBEDTLS_SSL_PROTO_DTLS)
4283 used += 2;
4284 if( used <= buf_len )
4285 {
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004286 MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
4287 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004288 }
4289#endif /* MBEDTLS_SSL_PROTO_DTLS */
4290
4291#if defined(MBEDTLS_SSL_ALPN)
4292 {
4293 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnardf041f4e2019-07-24 00:58:27 +02004294 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004295 : 0;
4296
4297 used += 1 + alpn_len;
4298 if( used <= buf_len )
4299 {
4300 *p++ = alpn_len;
4301
4302 if( ssl->alpn_chosen != NULL )
4303 {
4304 memcpy( p, ssl->alpn_chosen, alpn_len );
4305 p += alpn_len;
4306 }
4307 }
4308 }
4309#endif /* MBEDTLS_SSL_ALPN */
4310
4311 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004312 * Done
4313 */
4314 *olen = used;
4315
4316 if( used > buf_len )
4317 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004318
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004319 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
4320
Hanno Becker43aefe22020-02-05 10:44:56 +00004321 return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004322}
4323
4324/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004325 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004326 *
4327 * This internal version is wrapped by a public function that cleans up in
4328 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004329 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004330MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004331static int ssl_context_load( mbedtls_ssl_context *ssl,
4332 const unsigned char *buf,
4333 size_t len )
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004334{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004335 const unsigned char *p = buf;
4336 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004337 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004339#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004340 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004341#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004342
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004343 /*
4344 * The context should have been freshly setup or reset.
4345 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004346 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004347 * renegotiating, or if the user mistakenly loaded a session first.)
4348 */
4349 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4350 ssl->session != NULL )
4351 {
4352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4353 }
4354
4355 /*
4356 * We can't check that the config matches the initial one, but we can at
4357 * least check it matches the requirements for serializing.
4358 */
4359 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004360 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4361 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004362#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004363 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004364#endif
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004365 0 )
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004366 {
4367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4368 }
4369
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004370 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
4371
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004372 /*
4373 * Check version identifier
4374 */
4375 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
4376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4377
4378 if( memcmp( p, ssl_serialized_context_header,
4379 sizeof( ssl_serialized_context_header ) ) != 0 )
4380 {
4381 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
4382 }
4383 p += sizeof( ssl_serialized_context_header );
4384
4385 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004386 * Session
4387 */
4388 if( (size_t)( end - p ) < 4 )
4389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4390
4391 session_len = ( (size_t) p[0] << 24 ) |
4392 ( (size_t) p[1] << 16 ) |
4393 ( (size_t) p[2] << 8 ) |
4394 ( (size_t) p[3] );
4395 p += 4;
4396
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004397 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004398 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004399 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004400 ssl->session_in = ssl->session;
4401 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004402 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004403
4404 if( (size_t)( end - p ) < session_len )
4405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4406
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004407 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004408 if( ret != 0 )
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004409 {
4410 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004411 return( ret );
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004412 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004413
4414 p += session_len;
4415
4416 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004417 * Transform
4418 */
4419
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004420 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004421 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004422 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004423 ssl->transform_in = ssl->transform;
4424 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004425 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004426
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004427#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004428 prf_func = ssl_tls12prf_from_cs( ssl->session->ciphersuite );
4429 if( prf_func == NULL )
4430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4431
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004432 /* Read random bytes and populate structure */
4433 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
4434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004435
Hanno Beckerbd257552021-03-22 06:59:27 +00004436 ret = ssl_tls12_populate_transform( ssl->transform,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004437 ssl->session->ciphersuite,
4438 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004439#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004440 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004441#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Andrzej Kurek894edde2022-09-29 06:31:14 -04004442 prf_func,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004443 p, /* currently pointing to randbytes */
Glenn Strauss07c64162022-03-14 12:34:51 -04004444 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004445 ssl->conf->endpoint,
4446 ssl );
4447 if( ret != 0 )
4448 return( ret );
Jerry Yu840fbb22022-02-17 14:59:29 +08004449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004450 p += sizeof( ssl->transform->randbytes );
4451
4452#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4453 /* Read connection IDs and store them */
4454 if( (size_t)( end - p ) < 1 )
4455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4456
4457 ssl->transform->in_cid_len = *p++;
4458
Manuel Pégourié-Gonnard5ea13b82019-07-23 15:02:54 +02004459 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4461
4462 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
4463 p += ssl->transform->in_cid_len;
4464
4465 ssl->transform->out_cid_len = *p++;
4466
4467 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
4468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4469
4470 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
4471 p += ssl->transform->out_cid_len;
4472#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4473
4474 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004475 * Saved fields from top-level ssl_context structure
4476 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004477 if( (size_t)( end - p ) < 4 )
4478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4479
4480 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
4481 ( (uint32_t) p[1] << 16 ) |
4482 ( (uint32_t) p[2] << 8 ) |
4483 ( (uint32_t) p[3] );
4484 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004485
4486#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4487 if( (size_t)( end - p ) < 16 )
4488 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4489
4490 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
4491 ( (uint64_t) p[1] << 48 ) |
4492 ( (uint64_t) p[2] << 40 ) |
4493 ( (uint64_t) p[3] << 32 ) |
4494 ( (uint64_t) p[4] << 24 ) |
4495 ( (uint64_t) p[5] << 16 ) |
4496 ( (uint64_t) p[6] << 8 ) |
4497 ( (uint64_t) p[7] );
4498 p += 8;
4499
4500 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
4501 ( (uint64_t) p[1] << 48 ) |
4502 ( (uint64_t) p[2] << 40 ) |
4503 ( (uint64_t) p[3] << 32 ) |
4504 ( (uint64_t) p[4] << 24 ) |
4505 ( (uint64_t) p[5] << 16 ) |
4506 ( (uint64_t) p[6] << 8 ) |
4507 ( (uint64_t) p[7] );
4508 p += 8;
4509#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4510
4511#if defined(MBEDTLS_SSL_PROTO_DTLS)
4512 if( (size_t)( end - p ) < 1 )
4513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4514
4515 ssl->disable_datagram_packing = *p++;
4516#endif /* MBEDTLS_SSL_PROTO_DTLS */
4517
Jerry Yud9a94fe2021-09-28 18:58:59 +08004518 if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) )
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004520 memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) );
4521 p += sizeof( ssl->cur_out_ctr );
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004522
4523#if defined(MBEDTLS_SSL_PROTO_DTLS)
4524 if( (size_t)( end - p ) < 2 )
4525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4526
4527 ssl->mtu = ( p[0] << 8 ) | p[1];
4528 p += 2;
4529#endif /* MBEDTLS_SSL_PROTO_DTLS */
4530
4531#if defined(MBEDTLS_SSL_ALPN)
4532 {
4533 uint8_t alpn_len;
4534 const char **cur;
4535
4536 if( (size_t)( end - p ) < 1 )
4537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4538
4539 alpn_len = *p++;
4540
4541 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
4542 {
4543 /* alpn_chosen should point to an item in the configured list */
4544 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
4545 {
4546 if( strlen( *cur ) == alpn_len &&
4547 memcmp( p, cur, alpn_len ) == 0 )
4548 {
4549 ssl->alpn_chosen = *cur;
4550 break;
4551 }
4552 }
4553 }
4554
4555 /* can only happen on conf mismatch */
4556 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
4557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4558
4559 p += alpn_len;
4560 }
4561#endif /* MBEDTLS_SSL_ALPN */
4562
4563 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004564 * Forced fields from top-level ssl_context structure
4565 *
4566 * Most of them already set to the correct value by mbedtls_ssl_init() and
4567 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4568 */
4569 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004570 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004571
Hanno Becker361b10d2019-08-30 10:42:49 +01004572 /* Adjust pointers for header fields of outgoing records to
4573 * the given transform, accounting for explicit IV and CID. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004574 mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
Hanno Becker361b10d2019-08-30 10:42:49 +01004575
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004576#if defined(MBEDTLS_SSL_PROTO_DTLS)
4577 ssl->in_epoch = 1;
4578#endif
4579
4580 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4581 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004582 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4583 * inappropriately. */
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004584 if( ssl->handshake != NULL )
4585 {
4586 mbedtls_ssl_handshake_free( ssl );
4587 mbedtls_free( ssl->handshake );
4588 ssl->handshake = NULL;
4589 }
4590
4591 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004592 * Done - should have consumed entire buffer
4593 */
4594 if( p != end )
4595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004596
4597 return( 0 );
4598}
4599
4600/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004601 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004602 */
4603int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
4604 const unsigned char *buf,
4605 size_t len )
4606{
4607 int ret = ssl_context_load( context, buf, len );
4608
4609 if( ret != 0 )
4610 mbedtls_ssl_free( context );
4611
4612 return( ret );
4613}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004614#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004615
4616/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 * Free an SSL context
4618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004620{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004621 if( ssl == NULL )
4622 return;
4623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004626 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 {
sander-visserb8aa2072020-05-06 22:05:13 +02004628#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4629 size_t out_buf_len = ssl->out_buf_len;
4630#else
4631 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4632#endif
4633
Darryl Greenb33cc762019-11-28 14:29:44 +00004634 mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 mbedtls_free( ssl->out_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004636 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 }
4638
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004639 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
sander-visserb8aa2072020-05-06 22:05:13 +02004641#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4642 size_t in_buf_len = ssl->in_buf_len;
4643#else
4644 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4645#endif
4646
Darryl Greenb33cc762019-11-28 14:29:44 +00004647 mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 mbedtls_free( ssl->in_buf );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004649 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004650 }
4651
Paul Bakker48916f92012-09-16 19:57:18 +00004652 if( ssl->transform )
4653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 mbedtls_ssl_transform_free( ssl->transform );
4655 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004656 }
4657
4658 if( ssl->handshake )
4659 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02004660 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661 mbedtls_ssl_transform_free( ssl->transform_negotiate );
4662 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 mbedtls_free( ssl->handshake );
4665 mbedtls_free( ssl->transform_negotiate );
4666 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004667 }
4668
Ronald Cron6f135e12021-12-08 16:57:54 +01004669#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker3aa186f2021-08-10 09:24:19 +01004670 mbedtls_ssl_transform_free( ssl->transform_application );
4671 mbedtls_free( ssl->transform_application );
Ronald Cron6f135e12021-12-08 16:57:54 +01004672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004673
Paul Bakkerc0463502013-02-14 11:19:38 +01004674 if( ssl->session )
4675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 mbedtls_ssl_session_free( ssl->session );
4677 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004678 }
4679
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004680#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02004681 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004683 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004686#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004687
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004688#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004690#endif
4691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004693
Paul Bakker86f04f42013-02-14 11:20:09 +01004694 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05004695 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004696}
4697
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004698/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004699 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004700 */
4701void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
4702{
4703 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
4704}
4705
Gilles Peskineae270bf2021-06-02 00:05:29 +02004706/* The selection should be the same as mbedtls_x509_crt_profile_default in
4707 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004708 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004709 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004710 * about this list.
4711 */
Brett Warrene0edc842021-08-17 09:53:13 +01004712static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004713#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004714 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004715#endif
4716#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004717 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004718#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004719#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004720 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004721#endif
4722#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004723 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004724#endif
4725#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004726 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004727#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004728#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004729 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004730#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004731#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004732 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004733#endif
4734#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004735 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004736#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004737 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004738};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004739
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004740static int ssl_preset_suiteb_ciphersuites[] = {
4741 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4742 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4743 0
4744};
4745
Ronald Crone68ab4f2022-10-05 12:46:29 +02004746#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004747
Jerry Yu909df7b2022-01-22 11:56:27 +08004748/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004749 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004750 * rules SHOULD be upheld.
4751 * - No duplicate entries.
4752 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004753 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004754 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004755 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004756static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004757
Andrzej Kurek25f27152022-08-17 16:09:31 -04004758#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 +08004759 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4760 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004761#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004762 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004763
Andrzej Kurek25f27152022-08-17 16:09:31 -04004764#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 +08004765 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4766 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004767#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004768 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4769
Andrzej Kurek25f27152022-08-17 16:09:31 -04004770#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 +08004771 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4772 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004773#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004774 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004775
Andrzej Kurek25f27152022-08-17 16:09:31 -04004776#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 +08004777 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004778#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 +08004779
Andrzej Kurek25f27152022-08-17 16:09:31 -04004780#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 +08004781 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004782#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 +08004783
Andrzej Kurek25f27152022-08-17 16:09:31 -04004784#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 +08004785 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurek25f27152022-08-17 16:09:31 -04004786#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 +08004787
Andrzej Kurek25f27152022-08-17 16:09:31 -04004788#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 +08004789 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4790#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4791
Andrzej Kurek25f27152022-08-17 16:09:31 -04004792#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 +08004793 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4794#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4795
Andrzej Kurek25f27152022-08-17 16:09:31 -04004796#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 +08004797 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4798#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4799
Gabor Mezei15b95a62022-05-09 16:37:58 +02004800 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004801};
4802
4803/* NOTICE: see above */
4804#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4805static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004806#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004807#if defined(MBEDTLS_ECDSA_C)
4808 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004809#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004810#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4811 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4812#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004813#if defined(MBEDTLS_RSA_C)
4814 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ),
4815#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004816#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004817#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004818#if defined(MBEDTLS_ECDSA_C)
4819 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004820#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004821#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4822 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4823#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004824#if defined(MBEDTLS_RSA_C)
4825 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4826#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004827#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004828#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004829#if defined(MBEDTLS_ECDSA_C)
4830 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004831#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004832#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4833 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4834#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004835#if defined(MBEDTLS_RSA_C)
4836 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4837#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004838#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004839 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004840};
4841#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4842/* NOTICE: see above */
4843static uint16_t ssl_preset_suiteb_sig_algs[] = {
4844
Andrzej Kurek25f27152022-08-17 16:09:31 -04004845#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 +08004846 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4847 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004848#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004849 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4850
Andrzej Kurek25f27152022-08-17 16:09:31 -04004851#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 +08004852 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4853 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004854#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004855 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004856
Andrzej Kurek25f27152022-08-17 16:09:31 -04004857#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 +08004858 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004859#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 +08004860
Andrzej Kurek25f27152022-08-17 16:09:31 -04004861#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 +08004862 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004863#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004864
Gabor Mezei15b95a62022-05-09 16:37:58 +02004865 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004866};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004867
Jerry Yu909df7b2022-01-22 11:56:27 +08004868/* NOTICE: see above */
4869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4870static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004871#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004872#if defined(MBEDTLS_ECDSA_C)
4873 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ),
Jerry Yu713013f2022-01-17 18:16:35 +08004874#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004875#if defined(MBEDTLS_RSA_C)
4876 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ),
4877#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004878#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004879#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004880#if defined(MBEDTLS_ECDSA_C)
4881 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ),
Jerry Yu18c833e2022-01-25 10:55:47 +08004882#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004883#if defined(MBEDTLS_RSA_C)
4884 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ),
4885#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004886#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004887 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004888};
Jerry Yu909df7b2022-01-22 11:56:27 +08004889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4890
Ronald Crone68ab4f2022-10-05 12:46:29 +02004891#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004892
Brett Warrene0edc842021-08-17 09:53:13 +01004893static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01004894#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004895 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004896#endif
4897#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004898 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01004899#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004900 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004901};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004902
Ronald Crone68ab4f2022-10-05 12:46:29 +02004903#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08004904/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08004905 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004906MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf377d642022-01-25 10:43:59 +08004907static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004908{
4909 size_t i, j;
4910 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08004911
Gabor Mezei15b95a62022-05-09 16:37:58 +02004912 for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004913 {
Jerry Yuf377d642022-01-25 10:43:59 +08004914 for( j = 0; j < i; j++ )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004915 {
Jerry Yuf377d642022-01-25 10:43:59 +08004916 if( sig_algs[i] != sig_algs[j] )
4917 continue;
4918 mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET
4919 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
4920 sig_algs[i], j, i );
4921 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08004922 }
4923 }
4924 return( ret );
4925}
4926
Ronald Crone68ab4f2022-10-05 12:46:29 +02004927#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004928
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004929/*
Tillmann Karras588ad502015-09-25 04:27:22 +02004930 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004931 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004932int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004933 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004934{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004935#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00004936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004937#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004938
Ronald Crone68ab4f2022-10-05 12:46:29 +02004939#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf377d642022-01-25 10:43:59 +08004940 if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004941 {
4942 mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" );
4943 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4944 }
4945
Jerry Yuf377d642022-01-25 10:43:59 +08004946 if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) )
Jerry Yu1a8b4812022-01-20 17:56:50 +08004947 {
4948 mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" );
4949 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4950 }
Jerry Yu909df7b2022-01-22 11:56:27 +08004951
4952#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yuf377d642022-01-25 10:43:59 +08004953 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004954 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004955 mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004956 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4957 }
4958
Jerry Yuf377d642022-01-25 10:43:59 +08004959 if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) )
Jerry Yu909df7b2022-01-22 11:56:27 +08004960 {
Jerry Yu909df7b2022-01-22 11:56:27 +08004961 mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" );
Jerry Yu909df7b2022-01-22 11:56:27 +08004962 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
4963 }
4964#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004965#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08004966
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02004967 /* Use the functions here so that they are covered in tests,
4968 * but otherwise access member directly for efficiency */
4969 mbedtls_ssl_conf_endpoint( conf, endpoint );
4970 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004971
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004972 /*
4973 * Things that are common to all presets
4974 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004975#if defined(MBEDTLS_SSL_CLI_C)
4976 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4977 {
4978 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
4979#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4980 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
4981#endif
4982 }
4983#endif
4984
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004985#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4986 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
4987#endif
4988
4989#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
4990 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
4991#endif
4992
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004993#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004994 conf->f_cookie_write = ssl_cookie_write_dummy;
4995 conf->f_cookie_check = ssl_cookie_check_dummy;
4996#endif
4997
4998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4999 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5000#endif
5001
Janos Follath088ce432017-04-10 12:42:31 +01005002#if defined(MBEDTLS_SSL_SRV_C)
5003 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005004 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005005#endif
5006
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005007#if defined(MBEDTLS_SSL_PROTO_DTLS)
5008 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5009 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5010#endif
5011
5012#if defined(MBEDTLS_SSL_RENEGOTIATION)
5013 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00005014 memset( conf->renego_period, 0x00, 2 );
5015 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005016#endif
5017
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005018#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckere2defad2021-07-24 05:59:17 +01005019 if( endpoint == MBEDTLS_SSL_IS_SERVER )
5020 {
5021 const unsigned char dhm_p[] =
5022 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5023 const unsigned char dhm_g[] =
5024 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005025
Hanno Beckere2defad2021-07-24 05:59:17 +01005026 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
5027 dhm_p, sizeof( dhm_p ),
5028 dhm_g, sizeof( dhm_g ) ) ) != 0 )
5029 {
5030 return( ret );
5031 }
5032 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005033#endif
5034
Ronald Cron6f135e12021-12-08 16:57:54 +01005035#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yud0766ec2022-09-22 10:46:57 +08005036#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005037 mbedtls_ssl_conf_new_session_tickets(
5038 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS );
5039#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005040 /*
5041 * Allow all TLS 1.3 key exchange modes by default.
5042 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005043 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005044#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005045
XiaokangQian4d3a6042022-04-21 13:46:17 +00005046 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5047 {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005048#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005049 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005050 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005051#else
XiaokangQian060d8672022-04-21 09:24:56 +00005052 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
XiaokangQian060d8672022-04-21 09:24:56 +00005053#endif
XiaokangQian4d3a6042022-04-21 13:46:17 +00005054 }
5055 else
5056 {
5057#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
5058 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
5059 {
5060 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5061 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5062 }
5063 else
5064 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
5065 {
5066 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5067 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5068 }
5069#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5070 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5071 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5072#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5073 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5074 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5075#else
5076 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5077#endif
5078 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005079
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005080 /*
5081 * Preset-specific defaults
5082 */
5083 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005084 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005085 /*
5086 * NSA Suite B
5087 */
5088 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005089
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005090 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005091
5092#if defined(MBEDTLS_X509_CRT_PARSE_C)
5093 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005094#endif
5095
Ronald Crone68ab4f2022-10-05 12:46:29 +02005096#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5098 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5099 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
5100 else
5101#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5102 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005103#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005104
Brett Warrene0edc842021-08-17 09:53:13 +01005105#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5106 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005107#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005108 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005109 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005110
5111 /*
5112 * Default
5113 */
5114 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005115
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005116 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005117
5118#if defined(MBEDTLS_X509_CRT_PARSE_C)
5119 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5120#endif
5121
Ronald Crone68ab4f2022-10-05 12:46:29 +02005122#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005123#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5124 if( mbedtls_ssl_conf_is_tls12_only( conf ) )
5125 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
5126 else
5127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5128 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005129#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005130
Brett Warrene0edc842021-08-17 09:53:13 +01005131#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5132 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005133#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005134 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005135
5136#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5137 conf->dhm_min_bitlen = 1024;
5138#endif
5139 }
5140
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005141 return( 0 );
5142}
5143
5144/*
5145 * Free mbedtls_ssl_config
5146 */
5147void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
5148{
5149#if defined(MBEDTLS_DHM_C)
5150 mbedtls_mpi_free( &conf->dhm_P );
5151 mbedtls_mpi_free( &conf->dhm_G );
5152#endif
5153
Ronald Cron73fe8df2022-10-05 14:31:43 +02005154#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005155#if defined(MBEDTLS_USE_PSA_CRYPTO)
5156 if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
5157 {
Neil Armstrong501c9322022-05-03 09:35:09 +02005158 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5159 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005160#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005161 if( conf->psk != NULL )
5162 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005163 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005164 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00005165 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005166 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005167 }
5168
5169 if( conf->psk_identity != NULL )
5170 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005171 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09005172 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00005173 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005174 conf->psk_identity_len = 0;
5175 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005176#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005177
5178#if defined(MBEDTLS_X509_CRT_PARSE_C)
5179 ssl_key_cert_free( conf->key_cert );
5180#endif
5181
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005182 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005183}
5184
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005185#if defined(MBEDTLS_PK_C) && \
5186 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005187/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005191{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192#if defined(MBEDTLS_RSA_C)
5193 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
5194 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196#if defined(MBEDTLS_ECDSA_C)
5197 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
5198 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005201}
5202
Hanno Becker7e5437a2017-04-28 17:15:26 +01005203unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
5204{
5205 switch( type ) {
5206 case MBEDTLS_PK_RSA:
5207 return( MBEDTLS_SSL_SIG_RSA );
5208 case MBEDTLS_PK_ECDSA:
5209 case MBEDTLS_PK_ECKEY:
5210 return( MBEDTLS_SSL_SIG_ECDSA );
5211 default:
5212 return( MBEDTLS_SSL_SIG_ANON );
5213 }
5214}
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005217{
5218 switch( sig )
5219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#if defined(MBEDTLS_RSA_C)
5221 case MBEDTLS_SSL_SIG_RSA:
5222 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#if defined(MBEDTLS_ECDSA_C)
5225 case MBEDTLS_SSL_SIG_ECDSA:
5226 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005227#endif
5228 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005230 }
5231}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005232#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005233
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005234/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005235 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005238{
5239 switch( hash )
5240 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005241#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 case MBEDTLS_SSL_HASH_MD5:
5243 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005244#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005245#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246 case MBEDTLS_SSL_HASH_SHA1:
5247 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005248#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005249#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 case MBEDTLS_SSL_HASH_SHA224:
5251 return( MBEDTLS_MD_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005252#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005253#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 case MBEDTLS_SSL_HASH_SHA256:
5255 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005256#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005257#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 case MBEDTLS_SSL_HASH_SHA384:
5259 return( MBEDTLS_MD_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005260#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005261#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 case MBEDTLS_SSL_HASH_SHA512:
5263 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005264#endif
5265 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005267 }
5268}
5269
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005270/*
5271 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5272 */
5273unsigned char mbedtls_ssl_hash_from_md_alg( int md )
5274{
5275 switch( md )
5276 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005277#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005278 case MBEDTLS_MD_MD5:
5279 return( MBEDTLS_SSL_HASH_MD5 );
5280#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005281#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005282 case MBEDTLS_MD_SHA1:
5283 return( MBEDTLS_SSL_HASH_SHA1 );
5284#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005285#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005286 case MBEDTLS_MD_SHA224:
5287 return( MBEDTLS_SSL_HASH_SHA224 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005288#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005289#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005290 case MBEDTLS_MD_SHA256:
5291 return( MBEDTLS_SSL_HASH_SHA256 );
5292#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005293#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005294 case MBEDTLS_MD_SHA384:
5295 return( MBEDTLS_SSL_HASH_SHA384 );
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005296#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005297#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005298 case MBEDTLS_MD_SHA512:
5299 return( MBEDTLS_SSL_HASH_SHA512 );
5300#endif
5301 default:
5302 return( MBEDTLS_SSL_HASH_NONE );
5303 }
5304}
5305
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005306/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005307 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005308 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005309 */
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005310int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005311{
Brett Warrene0edc842021-08-17 09:53:13 +01005312 const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005313
Brett Warrene0edc842021-08-17 09:53:13 +01005314 if( group_list == NULL )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005315 return( -1 );
5316
Brett Warrene0edc842021-08-17 09:53:13 +01005317 for( ; *group_list != 0; group_list++ )
5318 {
5319 if( *group_list == tls_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005320 return( 0 );
Brett Warrene0edc842021-08-17 09:53:13 +01005321 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005322
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005323 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005324}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005325
5326#if defined(MBEDTLS_ECP_C)
5327/*
5328 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5329 */
5330int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
5331{
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005332 const mbedtls_ecp_curve_info *grp_info =
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005333 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005334
Tom Cosgrovebcc13c92022-08-24 15:08:16 +01005335 if ( grp_info == NULL )
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005336 return -1;
5337
5338 uint16_t tls_id = grp_info->tls_id;
5339
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005340 return mbedtls_ssl_check_curve_tls_id( ssl, tls_id );
5341}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005342#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344#if defined(MBEDTLS_X509_CRT_PARSE_C)
5345int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
5346 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005347 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005348 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005349{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005350 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005351 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005352 const char *ext_oid;
5353 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005356 {
5357 /* Server part of the key exchange */
5358 switch( ciphersuite->key_exchange )
5359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 case MBEDTLS_KEY_EXCHANGE_RSA:
5361 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005362 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005363 break;
5364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5366 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5367 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5368 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005369 break;
5370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5372 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005373 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005374 break;
5375
5376 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 case MBEDTLS_KEY_EXCHANGE_NONE:
5378 case MBEDTLS_KEY_EXCHANGE_PSK:
5379 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5380 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005381 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005382 usage = 0;
5383 }
5384 }
5385 else
5386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5388 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005389 }
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005392 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005393 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005394 ret = -1;
5395 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 ext_oid = MBEDTLS_OID_SERVER_AUTH;
5400 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005401 }
5402 else
5403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
5405 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005406 }
5407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005409 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005410 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005411 ret = -1;
5412 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005413
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005414 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005417
Jerry Yu148165c2021-09-24 23:20:59 +08005418#if defined(MBEDTLS_USE_PSA_CRYPTO)
5419int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5420 const mbedtls_md_type_t md,
5421 unsigned char *dst,
5422 size_t dst_len,
5423 size_t *olen )
5424{
Ronald Cronf6893e12022-01-07 22:09:01 +01005425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5426 psa_hash_operation_t *hash_operation_to_clone;
5427 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5428
Jerry Yu148165c2021-09-24 23:20:59 +08005429 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005430
5431 switch( md )
5432 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005433#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005434 case MBEDTLS_MD_SHA384:
5435 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5436 break;
5437#endif
5438
Andrzej Kurek25f27152022-08-17 16:09:31 -04005439#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cronf6893e12022-01-07 22:09:01 +01005440 case MBEDTLS_MD_SHA256:
5441 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5442 break;
5443#endif
5444
5445 default:
5446 goto exit;
5447 }
5448
5449 status = psa_hash_clone( hash_operation_to_clone, &hash_operation );
5450 if( status != PSA_SUCCESS )
5451 goto exit;
5452
5453 status = psa_hash_finish( &hash_operation, dst, dst_len, olen );
5454 if( status != PSA_SUCCESS )
5455 goto exit;
5456
5457exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005458#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5459 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5460 (void) ssl;
5461#endif
Ronald Cronb788c042022-02-15 09:14:15 +01005462 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu148165c2021-09-24 23:20:59 +08005463}
5464#else /* MBEDTLS_USE_PSA_CRYPTO */
5465
Andrzej Kurek25f27152022-08-17 16:09:31 -04005466#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005467MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005468static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl,
5469 unsigned char *dst,
5470 size_t dst_len,
5471 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005472{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005473 int ret;
5474 mbedtls_sha512_context sha512;
5475
5476 if( dst_len < 48 )
5477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5478
5479 mbedtls_sha512_init( &sha512 );
Andrzej Kureka242e832022-08-11 10:03:14 -04005480 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005481
5482 if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 )
5483 {
5484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret );
5485 goto exit;
5486 }
5487
5488 *olen = 48;
5489
5490exit:
5491
5492 mbedtls_sha512_free( &sha512 );
5493 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005494}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005495#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005496
Andrzej Kurek25f27152022-08-17 16:09:31 -04005497#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005498MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu000f9762021-09-14 11:12:51 +08005499static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl,
5500 unsigned char *dst,
5501 size_t dst_len,
5502 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005503{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005504 int ret;
5505 mbedtls_sha256_context sha256;
5506
5507 if( dst_len < 32 )
5508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5509
5510 mbedtls_sha256_init( &sha256 );
5511 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Jerry Yuc5aef882021-12-23 20:15:02 +08005512
Jerry Yu24c0ec32021-09-09 14:21:07 +08005513 if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 )
5514 {
5515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret );
5516 goto exit;
5517 }
5518
5519 *olen = 32;
5520
5521exit:
5522
5523 mbedtls_sha256_free( &sha256 );
5524 return( ret );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005525}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005526#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005527
Jerry Yu000f9762021-09-14 11:12:51 +08005528int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
5529 const mbedtls_md_type_t md,
5530 unsigned char *dst,
5531 size_t dst_len,
5532 size_t *olen )
Jerry Yu24c0ec32021-09-09 14:21:07 +08005533{
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005534 switch( md )
5535 {
5536
Andrzej Kurek25f27152022-08-17 16:09:31 -04005537#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005538 case MBEDTLS_MD_SHA384:
Jerry Yuc5aef882021-12-23 20:15:02 +08005539 return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005540#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005541
Andrzej Kurek25f27152022-08-17 16:09:31 -04005542#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005543 case MBEDTLS_MD_SHA256:
Jerry Yuc5aef882021-12-23 20:15:02 +08005544 return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) );
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005545#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005546
5547 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005548#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5549 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5550 (void) ssl;
5551 (void) dst;
5552 (void) dst_len;
5553 (void) olen;
5554#endif
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005555 break;
5556 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu24c0ec32021-09-09 14:21:07 +08005558}
XiaokangQian647719a2021-12-07 09:16:29 +00005559
Jerry Yu148165c2021-09-24 23:20:59 +08005560#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005561
Ronald Crone68ab4f2022-10-05 12:46:29 +02005562#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005563/* mbedtls_ssl_parse_sig_alg_ext()
5564 *
5565 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5566 * value (TLS 1.3 RFC8446):
5567 * enum {
5568 * ....
5569 * ecdsa_secp256r1_sha256( 0x0403 ),
5570 * ecdsa_secp384r1_sha384( 0x0503 ),
5571 * ecdsa_secp521r1_sha512( 0x0603 ),
5572 * ....
5573 * } SignatureScheme;
5574 *
5575 * struct {
5576 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5577 * } SignatureSchemeList;
5578 *
5579 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5580 * value (TLS 1.2 RFC5246):
5581 * enum {
5582 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5583 * sha512(6), (255)
5584 * } HashAlgorithm;
5585 *
5586 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5587 * SignatureAlgorithm;
5588 *
5589 * struct {
5590 * HashAlgorithm hash;
5591 * SignatureAlgorithm signature;
5592 * } SignatureAndHashAlgorithm;
5593 *
5594 * SignatureAndHashAlgorithm
5595 * supported_signature_algorithms<2..2^16-2>;
5596 *
5597 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5598 * generalization of the TLS 1.2 signature algorithm extension.
5599 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5600 * `SignatureScheme` field of TLS 1.3
5601 *
5602 */
5603int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
5604 const unsigned char *buf,
5605 const unsigned char *end )
5606{
5607 const unsigned char *p = buf;
5608 size_t supported_sig_algs_len = 0;
5609 const unsigned char *supported_sig_algs_end;
5610 uint16_t sig_alg;
5611 uint32_t common_idx = 0;
5612
5613 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
5614 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
5615 p += 2;
5616
5617 memset( ssl->handshake->received_sig_algs, 0,
5618 sizeof(ssl->handshake->received_sig_algs) );
5619
5620 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
5621 supported_sig_algs_end = p + supported_sig_algs_len;
5622 while( p < supported_sig_algs_end )
5623 {
5624 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
5625 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
5626 p += 2;
Jerry Yuf3b46b52022-06-19 16:52:27 +08005627 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x %s",
5628 sig_alg,
5629 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Jerry Yu2fe6c632022-06-29 10:02:38 +08005630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu52b7d922022-07-01 18:03:31 +08005631 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
Jerry Yu2fe6c632022-06-29 10:02:38 +08005632 ( ! ( mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) &&
5633 mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) ) )
5634 {
Gabor Mezei078e8032022-04-27 21:17:56 +02005635 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005636 }
5637#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005638
Jerry Yuf3b46b52022-06-19 16:52:27 +08005639 MBEDTLS_SSL_DEBUG_MSG( 4, ( "valid signature algorithm: %s",
5640 mbedtls_ssl_sig_alg_to_str( sig_alg ) ) );
Gabor Mezei078e8032022-04-27 21:17:56 +02005641
5642 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
5643 {
5644 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5645 common_idx += 1;
5646 }
5647 }
5648 /* Check that we consumed all the message. */
5649 if( p != end )
5650 {
5651 MBEDTLS_SSL_DEBUG_MSG( 1,
5652 ( "Signature algorithms extension length misaligned" ) );
5653 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5654 MBEDTLS_ERR_SSL_DECODE_ERROR );
5655 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
5656 }
5657
5658 if( common_idx == 0 )
5659 {
5660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
5661 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5662 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5663 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
5664 }
5665
Gabor Mezei15b95a62022-05-09 16:37:58 +02005666 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005667 return( 0 );
5668}
5669
Ronald Crone68ab4f2022-10-05 12:46:29 +02005670#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005671
Jerry Yudc7bd172022-02-17 13:44:15 +08005672#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5673
5674#if defined(MBEDTLS_USE_PSA_CRYPTO)
5675
5676static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
5677 mbedtls_svc_key_id_t key,
5678 psa_algorithm_t alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005679 const unsigned char* raw_psk, size_t raw_psk_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005680 const unsigned char* seed, size_t seed_length,
5681 const unsigned char* label, size_t label_length,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005682 const unsigned char* other_secret,
5683 size_t other_secret_length,
Jerry Yudc7bd172022-02-17 13:44:15 +08005684 size_t capacity )
5685{
5686 psa_status_t status;
5687
5688 status = psa_key_derivation_setup( derivation, alg );
5689 if( status != PSA_SUCCESS )
5690 return( status );
5691
5692 if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5693 {
5694 status = psa_key_derivation_input_bytes( derivation,
5695 PSA_KEY_DERIVATION_INPUT_SEED,
5696 seed, seed_length );
5697 if( status != PSA_SUCCESS )
5698 return( status );
5699
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005700 if ( other_secret != NULL )
Przemek Stekiel1f027032022-04-05 17:12:11 +02005701 {
5702 status = psa_key_derivation_input_bytes( derivation,
Przemek Stekiel51a1f362022-04-13 08:57:06 +02005703 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5704 other_secret, other_secret_length );
Przemek Stekiel1f027032022-04-05 17:12:11 +02005705 if( status != PSA_SUCCESS )
5706 return( status );
5707 }
5708
Jerry Yudc7bd172022-02-17 13:44:15 +08005709 if( mbedtls_svc_key_id_is_null( key ) )
5710 {
5711 status = psa_key_derivation_input_bytes(
5712 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005713 raw_psk, raw_psk_length );
Jerry Yudc7bd172022-02-17 13:44:15 +08005714 }
5715 else
5716 {
5717 status = psa_key_derivation_input_key(
5718 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
5719 }
5720 if( status != PSA_SUCCESS )
5721 return( status );
5722
5723 status = psa_key_derivation_input_bytes( derivation,
5724 PSA_KEY_DERIVATION_INPUT_LABEL,
5725 label, label_length );
5726 if( status != PSA_SUCCESS )
5727 return( status );
5728 }
5729 else
5730 {
5731 return( PSA_ERROR_NOT_SUPPORTED );
5732 }
5733
5734 status = psa_key_derivation_set_capacity( derivation, capacity );
5735 if( status != PSA_SUCCESS )
5736 return( status );
5737
5738 return( PSA_SUCCESS );
5739}
5740
Andrzej Kurek57d10632022-10-24 10:32:01 -04005741#if defined(PSA_WANT_ALG_SHA_384) || \
5742 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005743MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005744static int tls_prf_generic( mbedtls_md_type_t md_type,
5745 const unsigned char *secret, size_t slen,
5746 const char *label,
5747 const unsigned char *random, size_t rlen,
5748 unsigned char *dstbuf, size_t dlen )
5749{
5750 psa_status_t status;
5751 psa_algorithm_t alg;
5752 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5753 psa_key_derivation_operation_t derivation =
5754 PSA_KEY_DERIVATION_OPERATION_INIT;
5755
5756 if( md_type == MBEDTLS_MD_SHA384 )
5757 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
5758 else
5759 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
5760
5761 /* Normally a "secret" should be long enough to be impossible to
5762 * find by brute force, and in particular should not be empty. But
5763 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5764 * and for this use case it makes sense to have a 0-length "secret".
5765 * Since the key API doesn't allow importing a key of length 0,
5766 * keep master_key=0, which setup_psa_key_derivation() understands
5767 * to mean a 0-length "secret" input. */
5768 if( slen != 0 )
5769 {
5770 psa_key_attributes_t key_attributes = psa_key_attributes_init();
5771 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
5772 psa_set_key_algorithm( &key_attributes, alg );
5773 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
5774
5775 status = psa_import_key( &key_attributes, secret, slen, &master_key );
5776 if( status != PSA_SUCCESS )
5777 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5778 }
5779
5780 status = setup_psa_key_derivation( &derivation,
5781 master_key, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005782 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005783 random, rlen,
5784 (unsigned char const *) label,
5785 (size_t) strlen( label ),
Przemek Stekiel1f027032022-04-05 17:12:11 +02005786 NULL, 0,
Jerry Yudc7bd172022-02-17 13:44:15 +08005787 dlen );
5788 if( status != PSA_SUCCESS )
5789 {
5790 psa_key_derivation_abort( &derivation );
5791 psa_destroy_key( master_key );
5792 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5793 }
5794
5795 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
5796 if( status != PSA_SUCCESS )
5797 {
5798 psa_key_derivation_abort( &derivation );
5799 psa_destroy_key( master_key );
5800 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5801 }
5802
5803 status = psa_key_derivation_abort( &derivation );
5804 if( status != PSA_SUCCESS )
5805 {
5806 psa_destroy_key( master_key );
5807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5808 }
5809
5810 if( ! mbedtls_svc_key_id_is_null( master_key ) )
5811 status = psa_destroy_key( master_key );
5812 if( status != PSA_SUCCESS )
5813 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
5814
5815 return( 0 );
5816}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005817#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08005818#else /* MBEDTLS_USE_PSA_CRYPTO */
5819
Andrzej Kurek57d10632022-10-24 10:32:01 -04005820#if defined(MBEDTLS_MD_C) && \
5821 ( defined(MBEDTLS_SHA256_C) || \
5822 defined(MBEDTLS_SHA384_C) )
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005823MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005824static int tls_prf_generic( mbedtls_md_type_t md_type,
5825 const unsigned char *secret, size_t slen,
5826 const char *label,
5827 const unsigned char *random, size_t rlen,
5828 unsigned char *dstbuf, size_t dlen )
5829{
5830 size_t nb;
5831 size_t i, j, k, md_len;
5832 unsigned char *tmp;
5833 size_t tmp_len = 0;
5834 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
5835 const mbedtls_md_info_t *md_info;
5836 mbedtls_md_context_t md_ctx;
5837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
5838
5839 mbedtls_md_init( &md_ctx );
5840
5841 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
5842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5843
5844 md_len = mbedtls_md_get_size( md_info );
5845
5846 tmp_len = md_len + strlen( label ) + rlen;
5847 tmp = mbedtls_calloc( 1, tmp_len );
5848 if( tmp == NULL )
5849 {
5850 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
5851 goto exit;
5852 }
5853
5854 nb = strlen( label );
5855 memcpy( tmp + md_len, label, nb );
5856 memcpy( tmp + md_len + nb, random, rlen );
5857 nb += rlen;
5858
5859 /*
5860 * Compute P_<hash>(secret, label + random)[0..dlen]
5861 */
5862 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
5863 goto exit;
5864
5865 ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
5866 if( ret != 0 )
5867 goto exit;
5868 ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
5869 if( ret != 0 )
5870 goto exit;
5871 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5872 if( ret != 0 )
5873 goto exit;
5874
5875 for( i = 0; i < dlen; i += md_len )
5876 {
5877 ret = mbedtls_md_hmac_reset ( &md_ctx );
5878 if( ret != 0 )
5879 goto exit;
5880 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
5881 if( ret != 0 )
5882 goto exit;
5883 ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
5884 if( ret != 0 )
5885 goto exit;
5886
5887 ret = mbedtls_md_hmac_reset ( &md_ctx );
5888 if( ret != 0 )
5889 goto exit;
5890 ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
5891 if( ret != 0 )
5892 goto exit;
5893 ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
5894 if( ret != 0 )
5895 goto exit;
5896
5897 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
5898
5899 for( j = 0; j < k; j++ )
5900 dstbuf[i + j] = h_i[j];
5901 }
5902
5903exit:
5904 mbedtls_md_free( &md_ctx );
5905
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00005906 if ( tmp != NULL )
5907 mbedtls_platform_zeroize( tmp, tmp_len );
5908
Jerry Yudc7bd172022-02-17 13:44:15 +08005909 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
5910
5911 mbedtls_free( tmp );
5912
5913 return( ret );
5914}
Andrzej Kurek57d10632022-10-24 10:32:01 -04005915#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08005916#endif /* MBEDTLS_USE_PSA_CRYPTO */
5917
Andrzej Kurek25f27152022-08-17 16:09:31 -04005918#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005919MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005920static int tls_prf_sha256( const unsigned char *secret, size_t slen,
5921 const char *label,
5922 const unsigned char *random, size_t rlen,
5923 unsigned char *dstbuf, size_t dlen )
5924{
5925 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
5926 label, random, rlen, dstbuf, dlen ) );
5927}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005928#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005929
Andrzej Kurek25f27152022-08-17 16:09:31 -04005930#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005931MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yudc7bd172022-02-17 13:44:15 +08005932static int tls_prf_sha384( const unsigned char *secret, size_t slen,
5933 const char *label,
5934 const unsigned char *random, size_t rlen,
5935 unsigned char *dstbuf, size_t dlen )
5936{
5937 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
5938 label, random, rlen, dstbuf, dlen ) );
5939}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005940#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08005941
Jerry Yuf009d862022-02-17 14:01:37 +08005942/*
5943 * Set appropriate PRF function and other SSL / TLS1.2 functions
5944 *
5945 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08005946 * - hash associated with the ciphersuite (only used by TLS 1.2)
5947 *
5948 * Outputs:
5949 * - the tls_prf, calc_verify and calc_finished members of handshake structure
5950 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005951MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf009d862022-02-17 14:01:37 +08005952static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
Jerry Yuf009d862022-02-17 14:01:37 +08005953 mbedtls_md_type_t hash )
5954{
Andrzej Kurek25f27152022-08-17 16:09:31 -04005955#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron81591aa2022-03-07 09:05:51 +01005956 if( hash == MBEDTLS_MD_SHA384 )
Jerry Yuf009d862022-02-17 14:01:37 +08005957 {
5958 handshake->tls_prf = tls_prf_sha384;
5959 handshake->calc_verify = ssl_calc_verify_tls_sha384;
5960 handshake->calc_finished = ssl_calc_finished_tls_sha384;
5961 }
5962 else
5963#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005964#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08005965 {
Ronald Cron81591aa2022-03-07 09:05:51 +01005966 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005967 handshake->tls_prf = tls_prf_sha256;
5968 handshake->calc_verify = ssl_calc_verify_tls_sha256;
5969 handshake->calc_finished = ssl_calc_finished_tls_sha256;
5970 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005971#else
Jerry Yuf009d862022-02-17 14:01:37 +08005972 {
Jerry Yuf009d862022-02-17 14:01:37 +08005973 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01005974 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08005975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5976 }
Ronald Cron81591aa2022-03-07 09:05:51 +01005977#endif
Jerry Yuf009d862022-02-17 14:01:37 +08005978
5979 return( 0 );
5980}
Jerry Yud6ab2352022-02-17 14:03:43 +08005981
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005982/*
5983 * Compute master secret if needed
5984 *
5985 * Parameters:
5986 * [in/out] handshake
5987 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
5988 * (PSA-PSK) ciphersuite_info, psk_opaque
5989 * [out] premaster (cleared)
5990 * [out] master
5991 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
5992 * debug: conf->f_dbg, conf->p_dbg
5993 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01005994 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005995 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005996MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08005997static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
5998 unsigned char *master,
5999 const mbedtls_ssl_context *ssl )
6000{
6001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6002
6003 /* cf. RFC 5246, Section 8.1:
6004 * "The master secret is always exactly 48 bytes in length." */
6005 size_t const master_secret_len = 48;
6006
6007#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6008 unsigned char session_hash[48];
6009#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6010
6011 /* The label for the KDF used for key expansion.
6012 * This is either "master secret" or "extended master secret"
6013 * depending on whether the Extended Master Secret extension
6014 * is used. */
6015 char const *lbl = "master secret";
6016
Przemek Stekielae4ed302022-04-05 17:15:55 +02006017 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006018 * - If the Extended Master Secret extension is not used,
6019 * this is ClientHello.Random + ServerHello.Random
6020 * (see Sect. 8.1 in RFC 5246).
6021 * - If the Extended Master Secret extension is used,
6022 * this is the transcript of the handshake so far.
6023 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006024 unsigned char const *seed = handshake->randbytes;
6025 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006026
6027#if !defined(MBEDTLS_DEBUG_C) && \
6028 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6029 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
6030 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
6031 ssl = NULL; /* make sure we don't use it except for those cases */
6032 (void) ssl;
6033#endif
6034
6035 if( handshake->resume != 0 )
6036 {
6037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
6038 return( 0 );
6039 }
6040
6041#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6042 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
6043 {
6044 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006045 seed = session_hash;
6046 handshake->calc_verify( ssl, session_hash, &seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006047
6048 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
Przemek Stekielae4ed302022-04-05 17:15:55 +02006049 session_hash, seed_len );
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006050 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006051#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006052
Przemek Stekiel99114f32022-04-22 11:20:09 +02006053#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006054 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Neil Armstronge952a302022-05-03 10:22:14 +02006055 if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006056 {
6057 /* Perform PSK-to-MS expansion in a single step. */
6058 psa_status_t status;
6059 psa_algorithm_t alg;
6060 mbedtls_svc_key_id_t psk;
6061 psa_key_derivation_operation_t derivation =
6062 PSA_KEY_DERIVATION_OPERATION_INIT;
6063 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6064
6065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
6066
6067 psk = mbedtls_ssl_get_opaque_psk( ssl );
6068
6069 if( hash_alg == MBEDTLS_MD_SHA384 )
6070 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
6071 else
6072 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
6073
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006074 size_t other_secret_len = 0;
6075 unsigned char* other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006076
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006077 switch( handshake->ciphersuite_info->key_exchange )
Przemek Stekielc2033402022-04-05 17:19:41 +02006078 {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006079 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006080 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006081 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006082 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006083 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006084 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006085 other_secret_len = 48;
6086 other_secret = handshake->premaster + 2;
6087 break;
6088 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006089 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006090 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6091 other_secret = handshake->premaster + 2;
6092 break;
6093 default:
6094 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006095 }
6096
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006097 status = setup_psa_key_derivation( &derivation, psk, alg,
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006098 ssl->conf->psk, ssl->conf->psk_len,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006099 seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006100 (unsigned char const *) lbl,
6101 (size_t) strlen( lbl ),
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006102 other_secret, other_secret_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006103 master_secret_len );
6104 if( status != PSA_SUCCESS )
6105 {
6106 psa_key_derivation_abort( &derivation );
6107 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6108 }
6109
6110 status = psa_key_derivation_output_bytes( &derivation,
6111 master,
6112 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_abort( &derivation );
6120 if( status != PSA_SUCCESS )
6121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
6122 }
6123 else
6124#endif
6125 {
6126 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Przemek Stekielae4ed302022-04-05 17:15:55 +02006127 lbl, seed, seed_len,
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006128 master,
6129 master_secret_len );
6130 if( ret != 0 )
6131 {
6132 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
6133 return( ret );
6134 }
6135
6136 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
6137 handshake->premaster,
6138 handshake->pmslen );
6139
6140 mbedtls_platform_zeroize( handshake->premaster,
6141 sizeof(handshake->premaster) );
6142 }
6143
6144 return( 0 );
6145}
6146
Jerry Yud62f87e2022-02-17 14:09:02 +08006147int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
6148{
6149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6150 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6151 ssl->handshake->ciphersuite_info;
6152
6153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
6154
6155 /* Set PRF, calc_verify and calc_finished function pointers */
6156 ret = ssl_set_handshake_prfs( ssl->handshake,
Jerry Yud62f87e2022-02-17 14:09:02 +08006157 ciphersuite_info->mac );
6158 if( ret != 0 )
6159 {
6160 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
6161 return( ret );
6162 }
6163
6164 /* Compute master secret if needed */
6165 ret = ssl_compute_master( ssl->handshake,
6166 ssl->session_negotiate->master,
6167 ssl );
6168 if( ret != 0 )
6169 {
6170 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
6171 return( ret );
6172 }
6173
6174 /* Swap the client and server random values:
6175 * - MS derivation wanted client+server (RFC 5246 8.1)
6176 * - key derivation wants server+client (RFC 5246 6.3) */
6177 {
6178 unsigned char tmp[64];
6179 memcpy( tmp, ssl->handshake->randbytes, 64 );
6180 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
6181 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
6182 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
6183 }
6184
6185 /* Populate transform structure */
6186 ret = ssl_tls12_populate_transform( ssl->transform_negotiate,
6187 ssl->session_negotiate->ciphersuite,
6188 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006189#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yud62f87e2022-02-17 14:09:02 +08006190 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006191#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yud62f87e2022-02-17 14:09:02 +08006192 ssl->handshake->tls_prf,
6193 ssl->handshake->randbytes,
Glenn Strauss60bfe602022-03-14 19:04:24 -04006194 ssl->tls_version,
Jerry Yud62f87e2022-02-17 14:09:02 +08006195 ssl->conf->endpoint,
6196 ssl );
6197 if( ret != 0 )
6198 {
6199 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret );
6200 return( ret );
6201 }
6202
6203 /* We no longer need Server/ClientHello.random values */
6204 mbedtls_platform_zeroize( ssl->handshake->randbytes,
6205 sizeof( ssl->handshake->randbytes ) );
6206
6207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
6208
6209 return( 0 );
6210}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006211
Ronald Cron4dcbca92022-03-07 10:21:40 +01006212int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
6213{
Ronald Cron4dcbca92022-03-07 10:21:40 +01006214 switch( md )
6215 {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006216#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006217 case MBEDTLS_SSL_HASH_SHA384:
6218 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6219 break;
6220#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006221#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006222 case MBEDTLS_SSL_HASH_SHA256:
6223 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6224 break;
6225#endif
6226 default:
6227 return( -1 );
6228 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006229#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6230 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6231 (void) ssl;
6232#endif
Ronald Cronc2f13a02022-03-07 10:25:24 +01006233 return( 0 );
Ronald Cron4dcbca92022-03-07 10:21:40 +01006234}
6235
Andrzej Kurek25f27152022-08-17 16:09:31 -04006236#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006237void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
6238 unsigned char *hash,
6239 size_t *hlen )
6240{
6241#if defined(MBEDTLS_USE_PSA_CRYPTO)
6242 size_t hash_size;
6243 psa_status_t status;
6244 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6245
6246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
6247 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6248 if( status != PSA_SUCCESS )
6249 {
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6251 return;
6252 }
6253
6254 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
6255 if( status != PSA_SUCCESS )
6256 {
6257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6258 return;
6259 }
6260
6261 *hlen = 32;
6262 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6264#else
6265 mbedtls_sha256_context sha256;
6266
6267 mbedtls_sha256_init( &sha256 );
6268
6269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
6270
6271 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
6272 mbedtls_sha256_finish( &sha256, hash );
6273
6274 *hlen = 32;
6275
6276 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6278
6279 mbedtls_sha256_free( &sha256 );
6280#endif /* MBEDTLS_USE_PSA_CRYPTO */
6281 return;
6282}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006283#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006284
Andrzej Kurek25f27152022-08-17 16:09:31 -04006285#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006286void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
6287 unsigned char *hash,
6288 size_t *hlen )
6289{
6290#if defined(MBEDTLS_USE_PSA_CRYPTO)
6291 size_t hash_size;
6292 psa_status_t status;
6293 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6294
6295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
6296 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
6297 if( status != PSA_SUCCESS )
6298 {
6299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6300 return;
6301 }
6302
6303 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
6304 if( status != PSA_SUCCESS )
6305 {
6306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6307 return;
6308 }
6309
6310 *hlen = 48;
6311 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
6312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
6313#else
6314 mbedtls_sha512_context sha512;
6315
6316 mbedtls_sha512_init( &sha512 );
6317
6318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
6319
Andrzej Kureka242e832022-08-11 10:03:14 -04006320 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yuc1cb3842022-02-17 14:13:48 +08006321 mbedtls_sha512_finish( &sha512, hash );
6322
6323 *hlen = 48;
6324
6325 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
6326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
6327
6328 mbedtls_sha512_free( &sha512 );
6329#endif /* MBEDTLS_USE_PSA_CRYPTO */
6330 return;
6331}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006332#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006333
Neil Armstrong80f6f322022-05-03 17:56:38 +02006334#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6335 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006336int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
6337{
6338 unsigned char *p = ssl->handshake->premaster;
6339 unsigned char *end = p + sizeof( ssl->handshake->premaster );
6340 const unsigned char *psk = NULL;
6341 size_t psk_len = 0;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006342 int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
Jerry Yuce3dca42022-02-17 14:16:37 +08006343
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006344 if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
Jerry Yuce3dca42022-02-17 14:16:37 +08006345 {
6346 /*
6347 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006348 * checked before calling this function.
6349 *
6350 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006351 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006352 */
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006353 if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6354 {
6355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6356 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6357 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006358 }
6359
6360 /*
6361 * PMS = struct {
6362 * opaque other_secret<0..2^16-1>;
6363 * opaque psk<0..2^16-1>;
6364 * };
6365 * with "other_secret" depending on the particular key exchange
6366 */
6367#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
6368 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
6369 {
6370 if( end - p < 2 )
6371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6372
6373 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6374 p += 2;
6375
6376 if( end < p || (size_t)( end - p ) < psk_len )
6377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6378
6379 memset( p, 0, psk_len );
6380 p += psk_len;
6381 }
6382 else
6383#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6384#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
6385 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6386 {
6387 /*
6388 * other_secret already set by the ClientKeyExchange message,
6389 * and is 48 bytes long
6390 */
6391 if( end - p < 2 )
6392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6393
6394 *p++ = 0;
6395 *p++ = 48;
6396 p += 48;
6397 }
6398 else
6399#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6400#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
6401 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
6402 {
6403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6404 size_t len;
6405
6406 /* Write length only when we know the actual value */
6407 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
6408 p + 2, end - ( p + 2 ), &len,
6409 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6410 {
6411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
6412 return( ret );
6413 }
6414 MBEDTLS_PUT_UINT16_BE( len, p, 0 );
6415 p += 2 + len;
6416
6417 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
6418 }
6419 else
6420#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006421#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Jerry Yuce3dca42022-02-17 14:16:37 +08006422 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
6423 {
6424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6425 size_t zlen;
6426
6427 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
6428 p + 2, end - ( p + 2 ),
6429 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
6430 {
6431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
6432 return( ret );
6433 }
6434
6435 MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
6436 p += 2 + zlen;
6437
6438 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
6439 MBEDTLS_DEBUG_ECDH_Z );
6440 }
6441 else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006442#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006443 {
6444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6446 }
6447
6448 /* opaque psk<0..2^16-1>; */
6449 if( end - p < 2 )
6450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6451
6452 MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
6453 p += 2;
6454
6455 if( end < p || (size_t)( end - p ) < psk_len )
6456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6457
6458 memcpy( p, psk, psk_len );
6459 p += psk_len;
6460
6461 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6462
6463 return( 0 );
6464}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006465#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006466
6467#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006468MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc2c673d2022-02-17 14:20:39 +08006469static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
6470
6471#if defined(MBEDTLS_SSL_PROTO_DTLS)
6472int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
6473{
6474 /* If renegotiation is not enforced, retransmit until we would reach max
6475 * timeout if we were using the usual handshake doubling scheme */
6476 if( ssl->conf->renego_max_records < 0 )
6477 {
6478 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6479 unsigned char doublings = 1;
6480
6481 while( ratio != 0 )
6482 {
6483 ++doublings;
6484 ratio >>= 1;
6485 }
6486
6487 if( ++ssl->renego_records_seen > doublings )
6488 {
6489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
6490 return( 0 );
6491 }
6492 }
6493
6494 return( ssl_write_hello_request( ssl ) );
6495}
6496#endif
6497#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006498
Jerry Yud9526692022-02-17 14:23:47 +08006499/*
6500 * Handshake functions
6501 */
6502#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6503/* No certificate support -> dummy functions */
6504int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6505{
6506 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6507 ssl->handshake->ciphersuite_info;
6508
6509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6510
6511 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6512 {
6513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6514 ssl->state++;
6515 return( 0 );
6516 }
6517
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6520}
6521
6522int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6523{
6524 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6525 ssl->handshake->ciphersuite_info;
6526
6527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6528
6529 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6530 {
6531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
6532 ssl->state++;
6533 return( 0 );
6534 }
6535
6536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6538}
6539
6540#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6541/* Some certificate support -> implement write and parse */
6542
6543int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
6544{
6545 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6546 size_t i, n;
6547 const mbedtls_x509_crt *crt;
6548 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6549 ssl->handshake->ciphersuite_info;
6550
6551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
6552
6553 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6554 {
6555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6556 ssl->state++;
6557 return( 0 );
6558 }
6559
6560#if defined(MBEDTLS_SSL_CLI_C)
6561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6562 {
6563 if( ssl->handshake->client_auth == 0 )
6564 {
6565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
6566 ssl->state++;
6567 return( 0 );
6568 }
6569 }
6570#endif /* MBEDTLS_SSL_CLI_C */
6571#if defined(MBEDTLS_SSL_SRV_C)
6572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6573 {
6574 if( mbedtls_ssl_own_cert( ssl ) == NULL )
6575 {
6576 /* Should never happen because we shouldn't have picked the
6577 * ciphersuite if we don't have a certificate. */
6578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6579 }
6580 }
6581#endif
6582
6583 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
6584
6585 /*
6586 * 0 . 0 handshake type
6587 * 1 . 3 handshake length
6588 * 4 . 6 length of all certs
6589 * 7 . 9 length of cert. 1
6590 * 10 . n-1 peer certificate
6591 * n . n+2 length of cert. 2
6592 * n+3 . ... upper level cert, etc.
6593 */
6594 i = 7;
6595 crt = mbedtls_ssl_own_cert( ssl );
6596
6597 while( crt != NULL )
6598 {
6599 n = crt->raw.len;
6600 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
6601 {
6602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
6603 " > %" MBEDTLS_PRINTF_SIZET,
6604 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
6605 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
6606 }
6607
6608 ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
6609 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
6610 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
6611
6612 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6613 i += n; crt = crt->next;
6614 }
6615
6616 ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
6617 ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
6618 ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
6619
6620 ssl->out_msglen = i;
6621 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6622 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6623
6624 ssl->state++;
6625
6626 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
6627 {
6628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
6629 return( ret );
6630 }
6631
6632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
6633
6634 return( ret );
6635}
6636
6637#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6638
6639#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006640MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006641static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6642 unsigned char *crt_buf,
6643 size_t crt_buf_len )
6644{
6645 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6646
6647 if( peer_crt == NULL )
6648 return( -1 );
6649
6650 if( peer_crt->raw.len != crt_buf_len )
6651 return( -1 );
6652
6653 return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
6654}
6655#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006656MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006657static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6658 unsigned char *crt_buf,
6659 size_t crt_buf_len )
6660{
6661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6662 unsigned char const * const peer_cert_digest =
6663 ssl->session->peer_cert_digest;
6664 mbedtls_md_type_t const peer_cert_digest_type =
6665 ssl->session->peer_cert_digest_type;
6666 mbedtls_md_info_t const * const digest_info =
6667 mbedtls_md_info_from_type( peer_cert_digest_type );
6668 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6669 size_t digest_len;
6670
6671 if( peer_cert_digest == NULL || digest_info == NULL )
6672 return( -1 );
6673
6674 digest_len = mbedtls_md_get_size( digest_info );
6675 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6676 return( -1 );
6677
6678 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6679 if( ret != 0 )
6680 return( -1 );
6681
6682 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6683}
6684#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6685#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6686
6687/*
6688 * Once the certificate message is read, parse it into a cert chain and
6689 * perform basic checks, but leave actual verification to the caller
6690 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006691MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006692static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6693 mbedtls_x509_crt *chain )
6694{
6695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6696#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6697 int crt_cnt=0;
6698#endif
6699 size_t i, n;
6700 uint8_t alert;
6701
6702 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
6703 {
6704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6706 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6708 }
6709
6710 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE )
6711 {
6712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6713 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
6714 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
6715 }
6716
6717 if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
6718 {
6719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6721 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6722 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6723 }
6724
6725 i = mbedtls_ssl_hs_hdr_len( ssl );
6726
6727 /*
6728 * Same message structure as in mbedtls_ssl_write_certificate()
6729 */
6730 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
6731
6732 if( ssl->in_msg[i] != 0 ||
6733 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
6734 {
6735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6737 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6738 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6739 }
6740
6741 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6742 i += 3;
6743
6744 /* Iterate through and parse the CRTs in the provided chain. */
6745 while( i < ssl->in_hslen )
6746 {
6747 /* Check that there's room for the next CRT's length fields. */
6748 if ( i + 3 > ssl->in_hslen ) {
6749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6750 mbedtls_ssl_send_alert_message( ssl,
6751 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6752 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6753 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6754 }
6755 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6756 * anything beyond 2**16 ~ 64K. */
6757 if( ssl->in_msg[i] != 0 )
6758 {
6759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6760 mbedtls_ssl_send_alert_message( ssl,
6761 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6762 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT );
6763 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6764 }
6765
6766 /* Read length of the next CRT in the chain. */
6767 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6768 | (unsigned int) ssl->in_msg[i + 2];
6769 i += 3;
6770
6771 if( n < 128 || i + n > ssl->in_hslen )
6772 {
6773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
6774 mbedtls_ssl_send_alert_message( ssl,
6775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6776 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
6777 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
6778 }
6779
6780 /* Check if we're handling the first CRT in the chain. */
6781#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6782 if( crt_cnt++ == 0 &&
6783 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6784 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6785 {
6786 /* During client-side renegotiation, check that the server's
6787 * end-CRTs hasn't changed compared to the initial handshake,
6788 * mitigating the triple handshake attack. On success, reuse
6789 * the original end-CRT instead of parsing it again. */
6790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6791 if( ssl_check_peer_crt_unchanged( ssl,
6792 &ssl->in_msg[i],
6793 n ) != 0 )
6794 {
6795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6796 mbedtls_ssl_send_alert_message( ssl,
6797 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6798 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6799 return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE );
6800 }
6801
6802 /* Now we can safely free the original chain. */
6803 ssl_clear_peer_cert( ssl->session );
6804 }
6805#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6806
6807 /* Parse the next certificate in the chain. */
6808#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6809 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
6810#else
6811 /* If we don't need to store the CRT chain permanently, parse
6812 * it in-place from the input buffer instead of making a copy. */
6813 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6814#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6815 switch( ret )
6816 {
6817 case 0: /*ok*/
6818 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6819 /* Ignore certificate with an unknown algorithm: maybe a
6820 prior certificate was already trusted. */
6821 break;
6822
6823 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6824 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6825 goto crt_parse_der_failed;
6826
6827 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6828 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6829 goto crt_parse_der_failed;
6830
6831 default:
6832 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6833 crt_parse_der_failed:
6834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6835 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6836 return( ret );
6837 }
6838
6839 i += n;
6840 }
6841
6842 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
6843 return( 0 );
6844}
6845
6846#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006847MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006848static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6849{
6850 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6851 return( -1 );
6852
6853 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6854 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6855 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6856 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6857 {
Ronald Cron19385882022-06-15 16:26:13 +02006858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
Jerry Yud9526692022-02-17 14:23:47 +08006859 return( 0 );
6860 }
6861 return( -1 );
6862}
6863#endif /* MBEDTLS_SSL_SRV_C */
6864
6865/* Check if a certificate message is expected.
6866 * Return either
6867 * - SSL_CERTIFICATE_EXPECTED, or
6868 * - SSL_CERTIFICATE_SKIP
6869 * indicating whether a Certificate message is expected or not.
6870 */
6871#define SSL_CERTIFICATE_EXPECTED 0
6872#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006873MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006874static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6875 int authmode )
6876{
6877 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6878 ssl->handshake->ciphersuite_info;
6879
6880 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6881 return( SSL_CERTIFICATE_SKIP );
6882
6883#if defined(MBEDTLS_SSL_SRV_C)
6884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6885 {
6886 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6887 return( SSL_CERTIFICATE_SKIP );
6888
6889 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6890 {
6891 ssl->session_negotiate->verify_result =
6892 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6893 return( SSL_CERTIFICATE_SKIP );
6894 }
6895 }
6896#else
6897 ((void) authmode);
6898#endif /* MBEDTLS_SSL_SRV_C */
6899
6900 return( SSL_CERTIFICATE_EXPECTED );
6901}
6902
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006903MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08006904static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6905 int authmode,
6906 mbedtls_x509_crt *chain,
6907 void *rs_ctx )
6908{
6909 int ret = 0;
6910 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6911 ssl->handshake->ciphersuite_info;
6912 int have_ca_chain = 0;
6913
6914 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6915 void *p_vrfy;
6916
6917 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6918 return( 0 );
6919
6920 if( ssl->f_vrfy != NULL )
6921 {
6922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
6923 f_vrfy = ssl->f_vrfy;
6924 p_vrfy = ssl->p_vrfy;
6925 }
6926 else
6927 {
6928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
6929 f_vrfy = ssl->conf->f_vrfy;
6930 p_vrfy = ssl->conf->p_vrfy;
6931 }
6932
6933 /*
6934 * Main check: verify certificate
6935 */
6936#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6937 if( ssl->conf->f_ca_cb != NULL )
6938 {
6939 ((void) rs_ctx);
6940 have_ca_chain = 1;
6941
6942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
6943 ret = mbedtls_x509_crt_verify_with_ca_cb(
6944 chain,
6945 ssl->conf->f_ca_cb,
6946 ssl->conf->p_ca_cb,
6947 ssl->conf->cert_profile,
6948 ssl->hostname,
6949 &ssl->session_negotiate->verify_result,
6950 f_vrfy, p_vrfy );
6951 }
6952 else
6953#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6954 {
6955 mbedtls_x509_crt *ca_chain;
6956 mbedtls_x509_crl *ca_crl;
6957
6958#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6959 if( ssl->handshake->sni_ca_chain != NULL )
6960 {
6961 ca_chain = ssl->handshake->sni_ca_chain;
6962 ca_crl = ssl->handshake->sni_ca_crl;
6963 }
6964 else
6965#endif
6966 {
6967 ca_chain = ssl->conf->ca_chain;
6968 ca_crl = ssl->conf->ca_crl;
6969 }
6970
6971 if( ca_chain != NULL )
6972 have_ca_chain = 1;
6973
6974 ret = mbedtls_x509_crt_verify_restartable(
6975 chain,
6976 ca_chain, ca_crl,
6977 ssl->conf->cert_profile,
6978 ssl->hostname,
6979 &ssl->session_negotiate->verify_result,
6980 f_vrfy, p_vrfy, rs_ctx );
6981 }
6982
6983 if( ret != 0 )
6984 {
6985 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6986 }
6987
6988#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
6989 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6990 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6991#endif
6992
6993 /*
6994 * Secondary checks: always done, but change 'ret' only if it was 0
6995 */
6996
6997#if defined(MBEDTLS_ECP_C)
6998 {
6999 const mbedtls_pk_context *pk = &chain->pk;
7000
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007001 /* If certificate uses an EC key, make sure the curve is OK.
7002 * This is a public key, so it can't be opaque, so can_do() is a good
7003 * enough check to ensure pk_ec() is safe to use here. */
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007004 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Jerry Yud9526692022-02-17 14:23:47 +08007005 {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007006 /* and in the unlikely case the above assumption no longer holds
7007 * we are making sure that pk_ec() here does not return a NULL
7008 */
7009 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *pk );
7010 if( ec == NULL )
7011 {
Tom Cosgrove20c11372022-08-24 15:06:13 +01007012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_pk_ec() returned NULL" ) );
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7014 }
Jerry Yud9526692022-02-17 14:23:47 +08007015
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007016 if( mbedtls_ssl_check_curve( ssl, ec->grp.id ) != 0 )
7017 {
7018 ssl->session_negotiate->verify_result |=
7019 MBEDTLS_X509_BADCERT_BAD_KEY;
7020
7021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7022 if( ret == 0 )
7023 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7024 }
Jerry Yud9526692022-02-17 14:23:47 +08007025 }
7026 }
7027#endif /* MBEDTLS_ECP_C */
7028
7029 if( mbedtls_ssl_check_cert_usage( chain,
7030 ciphersuite_info,
7031 ! ssl->conf->endpoint,
7032 &ssl->session_negotiate->verify_result ) != 0 )
7033 {
7034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7035 if( ret == 0 )
7036 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
7037 }
7038
7039 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7040 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7041 * with details encoded in the verification flags. All other kinds
7042 * of error codes, including those from the user provided f_vrfy
7043 * functions, are treated as fatal and lead to a failure of
7044 * ssl_parse_certificate even if verification was optional. */
7045 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7046 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7047 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
7048 {
7049 ret = 0;
7050 }
7051
7052 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7053 {
7054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7055 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7056 }
7057
7058 if( ret != 0 )
7059 {
7060 uint8_t alert;
7061
7062 /* The certificate may have been rejected for several reasons.
7063 Pick one and send the corresponding alert. Which alert to send
7064 may be a subject of debate in some cases. */
7065 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7066 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7067 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7068 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7069 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7070 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7071 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7072 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7073 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7074 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7075 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7076 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7077 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7078 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7079 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7080 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7081 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7082 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7083 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7084 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7085 else
7086 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7088 alert );
7089 }
7090
7091#if defined(MBEDTLS_DEBUG_C)
7092 if( ssl->session_negotiate->verify_result != 0 )
7093 {
7094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
7095 (unsigned int) ssl->session_negotiate->verify_result ) );
7096 }
7097 else
7098 {
7099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7100 }
7101#endif /* MBEDTLS_DEBUG_C */
7102
7103 return( ret );
7104}
7105
7106#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007107MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007108static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7109 unsigned char *start, size_t len )
7110{
7111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7112 /* Remember digest of the peer's end-CRT. */
7113 ssl->session_negotiate->peer_cert_digest =
7114 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7115 if( ssl->session_negotiate->peer_cert_digest == NULL )
7116 {
7117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7118 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
7119 mbedtls_ssl_send_alert_message( ssl,
7120 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7121 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7122
7123 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7124 }
7125
7126 ret = mbedtls_md( mbedtls_md_info_from_type(
7127 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7128 start, len,
7129 ssl->session_negotiate->peer_cert_digest );
7130
7131 ssl->session_negotiate->peer_cert_digest_type =
7132 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7133 ssl->session_negotiate->peer_cert_digest_len =
7134 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7135
7136 return( ret );
7137}
7138
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007139MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yud9526692022-02-17 14:23:47 +08007140static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7141 unsigned char *start, size_t len )
7142{
7143 unsigned char *end = start + len;
7144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7145
7146 /* Make a copy of the peer's raw public key. */
7147 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7148 ret = mbedtls_pk_parse_subpubkey( &start, end,
7149 &ssl->handshake->peer_pubkey );
7150 if( ret != 0 )
7151 {
7152 /* We should have parsed the public key before. */
7153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7154 }
7155
7156 return( 0 );
7157}
7158#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7159
7160int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7161{
7162 int ret = 0;
7163 int crt_expected;
7164#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7165 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7166 ? ssl->handshake->sni_authmode
7167 : ssl->conf->authmode;
7168#else
7169 const int authmode = ssl->conf->authmode;
7170#endif
7171 void *rs_ctx = NULL;
7172 mbedtls_x509_crt *chain = NULL;
7173
7174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7175
7176 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7177 if( crt_expected == SSL_CERTIFICATE_SKIP )
7178 {
7179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
7180 goto exit;
7181 }
7182
7183#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7184 if( ssl->handshake->ecrs_enabled &&
7185 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
7186 {
7187 chain = ssl->handshake->ecrs_peer_cert;
7188 ssl->handshake->ecrs_peer_cert = NULL;
7189 goto crt_verify;
7190 }
7191#endif
7192
7193 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7194 {
7195 /* mbedtls_ssl_read_record may have sent an alert already. We
7196 let it decide whether to alert. */
7197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7198 goto exit;
7199 }
7200
7201#if defined(MBEDTLS_SSL_SRV_C)
7202 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7203 {
7204 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7205
7206 if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
7207 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
7208
7209 goto exit;
7210 }
7211#endif /* MBEDTLS_SSL_SRV_C */
7212
7213 /* Clear existing peer CRT structure in case we tried to
7214 * reuse a session but it failed, and allocate a new one. */
7215 ssl_clear_peer_cert( ssl->session_negotiate );
7216
7217 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7218 if( chain == NULL )
7219 {
7220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7221 sizeof( mbedtls_x509_crt ) ) );
7222 mbedtls_ssl_send_alert_message( ssl,
7223 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7224 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7225
7226 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7227 goto exit;
7228 }
7229 mbedtls_x509_crt_init( chain );
7230
7231 ret = ssl_parse_certificate_chain( ssl, chain );
7232 if( ret != 0 )
7233 goto exit;
7234
7235#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7236 if( ssl->handshake->ecrs_enabled)
7237 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
7238
7239crt_verify:
7240 if( ssl->handshake->ecrs_enabled)
7241 rs_ctx = &ssl->handshake->ecrs_ctx;
7242#endif
7243
7244 ret = ssl_parse_certificate_verify( ssl, authmode,
7245 chain, rs_ctx );
7246 if( ret != 0 )
7247 goto exit;
7248
7249#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7250 {
7251 unsigned char *crt_start, *pk_start;
7252 size_t crt_len, pk_len;
7253
7254 /* We parse the CRT chain without copying, so
7255 * these pointers point into the input buffer,
7256 * and are hence still valid after freeing the
7257 * CRT chain. */
7258
7259 crt_start = chain->raw.p;
7260 crt_len = chain->raw.len;
7261
7262 pk_start = chain->pk_raw.p;
7263 pk_len = chain->pk_raw.len;
7264
7265 /* Free the CRT structures before computing
7266 * digest and copying the peer's public key. */
7267 mbedtls_x509_crt_free( chain );
7268 mbedtls_free( chain );
7269 chain = NULL;
7270
7271 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
7272 if( ret != 0 )
7273 goto exit;
7274
7275 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7276 if( ret != 0 )
7277 goto exit;
7278 }
7279#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7280 /* Pass ownership to session structure. */
7281 ssl->session_negotiate->peer_cert = chain;
7282 chain = NULL;
7283#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7284
7285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
7286
7287exit:
7288
7289 if( ret == 0 )
7290 ssl->state++;
7291
7292#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
7293 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7294 {
7295 ssl->handshake->ecrs_peer_cert = chain;
7296 chain = NULL;
7297 }
7298#endif
7299
7300 if( chain != NULL )
7301 {
7302 mbedtls_x509_crt_free( chain );
7303 mbedtls_free( chain );
7304 }
7305
7306 return( ret );
7307}
7308#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7309
Andrzej Kurek25f27152022-08-17 16:09:31 -04007310#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007311static void ssl_calc_finished_tls_sha256(
7312 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7313{
7314 int len = 12;
7315 const char *sender;
7316 unsigned char padbuf[32];
7317#if defined(MBEDTLS_USE_PSA_CRYPTO)
7318 size_t hash_size;
7319 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7320 psa_status_t status;
7321#else
7322 mbedtls_sha256_context sha256;
7323#endif
7324
7325 mbedtls_ssl_session *session = ssl->session_negotiate;
7326 if( !session )
7327 session = ssl->session;
7328
7329 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7330 ? "client finished"
7331 : "server finished";
7332
7333#if defined(MBEDTLS_USE_PSA_CRYPTO)
7334 sha256_psa = psa_hash_operation_init();
7335
7336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7337
7338 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7339 if( status != PSA_SUCCESS )
7340 {
7341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7342 return;
7343 }
7344
7345 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7346 if( status != PSA_SUCCESS )
7347 {
7348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7349 return;
7350 }
7351 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7352#else
7353
7354 mbedtls_sha256_init( &sha256 );
7355
7356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
7357
7358 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
7359
7360 /*
7361 * TLSv1.2:
7362 * hash = PRF( master, finished_label,
7363 * Hash( handshake ) )[0.11]
7364 */
7365
7366#if !defined(MBEDTLS_SHA256_ALT)
7367 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
7368 sha256.state, sizeof( sha256.state ) );
7369#endif
7370
7371 mbedtls_sha256_finish( &sha256, padbuf );
7372 mbedtls_sha256_free( &sha256 );
7373#endif /* MBEDTLS_USE_PSA_CRYPTO */
7374
7375 ssl->handshake->tls_prf( session->master, 48, sender,
7376 padbuf, 32, buf, len );
7377
7378 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7379
7380 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7381
7382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7383}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007384#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007385
Jerry Yub7ba49e2022-02-17 14:25:53 +08007386
Andrzej Kurek25f27152022-08-17 16:09:31 -04007387#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007388static void ssl_calc_finished_tls_sha384(
7389 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
7390{
7391 int len = 12;
7392 const char *sender;
7393 unsigned char padbuf[48];
7394#if defined(MBEDTLS_USE_PSA_CRYPTO)
7395 size_t hash_size;
7396 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7397 psa_status_t status;
7398#else
7399 mbedtls_sha512_context sha512;
7400#endif
7401
7402 mbedtls_ssl_session *session = ssl->session_negotiate;
7403 if( !session )
7404 session = ssl->session;
7405
7406 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7407 ? "client finished"
7408 : "server finished";
7409
7410#if defined(MBEDTLS_USE_PSA_CRYPTO)
7411 sha384_psa = psa_hash_operation_init();
7412
7413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7414
7415 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
7416 if( status != PSA_SUCCESS )
7417 {
7418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7419 return;
7420 }
7421
7422 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
7423 if( status != PSA_SUCCESS )
7424 {
7425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7426 return;
7427 }
7428 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7429#else
7430 mbedtls_sha512_init( &sha512 );
7431
7432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
7433
Andrzej Kureka242e832022-08-11 10:03:14 -04007434 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha384 );
Jerry Yub7ba49e2022-02-17 14:25:53 +08007435
7436 /*
7437 * TLSv1.2:
7438 * hash = PRF( master, finished_label,
7439 * Hash( handshake ) )[0.11]
7440 */
7441
7442#if !defined(MBEDTLS_SHA512_ALT)
7443 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7444 sha512.state, sizeof( sha512.state ) );
7445#endif
7446 mbedtls_sha512_finish( &sha512, padbuf );
7447
7448 mbedtls_sha512_free( &sha512 );
7449#endif
7450
7451 ssl->handshake->tls_prf( session->master, 48, sender,
7452 padbuf, 48, buf, len );
7453
7454 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
7455
7456 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7457
7458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
7459}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007460#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007461
Jerry Yuaef00152022-02-17 14:27:31 +08007462void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
7463{
7464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
7465
7466 /*
7467 * Free our handshake params
7468 */
7469 mbedtls_ssl_handshake_free( ssl );
7470 mbedtls_free( ssl->handshake );
7471 ssl->handshake = NULL;
7472
7473 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007474 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007475 */
7476 if( ssl->transform )
7477 {
7478 mbedtls_ssl_transform_free( ssl->transform );
7479 mbedtls_free( ssl->transform );
7480 }
7481 ssl->transform = ssl->transform_negotiate;
7482 ssl->transform_negotiate = NULL;
7483
7484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
7485}
7486
Jerry Yu2a9fff52022-02-17 14:28:51 +08007487void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
7488{
7489 int resume = ssl->handshake->resume;
7490
7491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
7492
7493#if defined(MBEDTLS_SSL_RENEGOTIATION)
7494 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
7495 {
7496 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7497 ssl->renego_records_seen = 0;
7498 }
7499#endif
7500
7501 /*
7502 * Free the previous session and switch in the current one
7503 */
7504 if( ssl->session )
7505 {
7506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7507 /* RFC 7366 3.1: keep the EtM state */
7508 ssl->session_negotiate->encrypt_then_mac =
7509 ssl->session->encrypt_then_mac;
7510#endif
7511
7512 mbedtls_ssl_session_free( ssl->session );
7513 mbedtls_free( ssl->session );
7514 }
7515 ssl->session = ssl->session_negotiate;
7516 ssl->session_negotiate = NULL;
7517
7518 /*
7519 * Add cache entry
7520 */
7521 if( ssl->conf->f_set_cache != NULL &&
7522 ssl->session->id_len != 0 &&
7523 resume == 0 )
7524 {
7525 if( ssl->conf->f_set_cache( ssl->conf->p_cache,
7526 ssl->session->id,
7527 ssl->session->id_len,
7528 ssl->session ) != 0 )
7529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
7530 }
7531
7532#if defined(MBEDTLS_SSL_PROTO_DTLS)
7533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7534 ssl->handshake->flight != NULL )
7535 {
7536 /* Cancel handshake timer */
7537 mbedtls_ssl_set_timer( ssl, 0 );
7538
7539 /* Keep last flight around in case we need to resend it:
7540 * we need the handshake and transform structures for that */
7541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
7542 }
7543 else
7544#endif
7545 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
7546
7547 ssl->state++;
7548
7549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
7550}
7551
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007552int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
7553{
7554 int ret, hash_len;
7555
7556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
7557
7558 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
7559
7560 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
7561
7562 /*
7563 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7564 * may define some other value. Currently (early 2016), no defined
7565 * ciphersuite does this (and this is unlikely to change as activity has
7566 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7567 */
7568 hash_len = 12;
7569
7570#if defined(MBEDTLS_SSL_RENEGOTIATION)
7571 ssl->verify_data_len = hash_len;
7572 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
7573#endif
7574
7575 ssl->out_msglen = 4 + hash_len;
7576 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7577 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7578
7579 /*
7580 * In case of session resuming, invert the client and server
7581 * ChangeCipherSpec messages order.
7582 */
7583 if( ssl->handshake->resume != 0 )
7584 {
7585#if defined(MBEDTLS_SSL_CLI_C)
7586 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7587 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7588#endif
7589#if defined(MBEDTLS_SSL_SRV_C)
7590 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7591 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7592#endif
7593 }
7594 else
7595 ssl->state++;
7596
7597 /*
7598 * Switch to our negotiated transform and session parameters for outbound
7599 * data.
7600 */
7601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
7602
7603#if defined(MBEDTLS_SSL_PROTO_DTLS)
7604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7605 {
7606 unsigned char i;
7607
7608 /* Remember current epoch settings for resending */
7609 ssl->handshake->alt_transform_out = ssl->transform_out;
7610 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7611 sizeof( ssl->handshake->alt_out_ctr ) );
7612
7613 /* Set sequence_number to zero */
7614 memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 );
7615
7616
7617 /* Increment epoch */
7618 for( i = 2; i > 0; i-- )
7619 if( ++ssl->cur_out_ctr[i - 1] != 0 )
7620 break;
7621
7622 /* The loop goes to its end iff the counter is wrapping */
7623 if( i == 0 )
7624 {
7625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7626 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
7627 }
7628 }
7629 else
7630#endif /* MBEDTLS_SSL_PROTO_DTLS */
7631 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7632
7633 ssl->transform_out = ssl->transform_negotiate;
7634 ssl->session_out = ssl->session_negotiate;
7635
7636#if defined(MBEDTLS_SSL_PROTO_DTLS)
7637 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7638 mbedtls_ssl_send_flight_completed( ssl );
7639#endif
7640
7641 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
7642 {
7643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
7644 return( ret );
7645 }
7646
7647#if defined(MBEDTLS_SSL_PROTO_DTLS)
7648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7649 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7650 {
7651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7652 return( ret );
7653 }
7654#endif
7655
7656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
7657
7658 return( 0 );
7659}
7660
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007661#define SSL_MAX_HASH_LEN 12
7662
7663int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
7664{
7665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7666 unsigned int hash_len = 12;
7667 unsigned char buf[SSL_MAX_HASH_LEN];
7668
7669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
7670
7671 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
7672
7673 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
7674 {
7675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7676 goto exit;
7677 }
7678
7679 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
7680 {
7681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7683 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7684 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7685 goto exit;
7686 }
7687
7688 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED )
7689 {
7690 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7691 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7692 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7693 goto exit;
7694 }
7695
7696 if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
7697 {
7698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
7701 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7702 goto exit;
7703 }
7704
7705 if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
7706 buf, hash_len ) != 0 )
7707 {
7708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
7709 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7710 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
7711 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7712 goto exit;
7713 }
7714
7715#if defined(MBEDTLS_SSL_RENEGOTIATION)
7716 ssl->verify_data_len = hash_len;
7717 memcpy( ssl->peer_verify_data, buf, hash_len );
7718#endif
7719
7720 if( ssl->handshake->resume != 0 )
7721 {
7722#if defined(MBEDTLS_SSL_CLI_C)
7723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7724 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
7725#endif
7726#if defined(MBEDTLS_SSL_SRV_C)
7727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7728 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
7729#endif
7730 }
7731 else
7732 ssl->state++;
7733
7734#if defined(MBEDTLS_SSL_PROTO_DTLS)
7735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7736 mbedtls_ssl_recv_flight_completed( ssl );
7737#endif
7738
7739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
7740
7741exit:
7742 mbedtls_platform_zeroize( buf, hash_len );
7743 return( ret );
7744}
7745
Jerry Yu392112c2022-02-17 14:34:10 +08007746#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7747/*
7748 * Helper to get TLS 1.2 PRF from ciphersuite
7749 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7750 */
7751static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
7752{
Jerry Yu392112c2022-02-17 14:34:10 +08007753 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Andrzej Kurek68327742022-10-03 06:18:18 -04007754 mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
7755#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Leonid Rozenboime9d8dcd2022-08-08 15:57:48 -07007756 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Jerry Yu392112c2022-02-17 14:34:10 +08007757 return( tls_prf_sha384 );
Andrzej Kurek894edde2022-09-29 06:31:14 -04007758 else
Jerry Yu392112c2022-02-17 14:34:10 +08007759#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007760#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7761 {
7762 if( ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
7763 return( tls_prf_sha256 );
7764 }
7765#endif
7766#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7767 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7768 (void) ciphersuite_info;
7769#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007770
Andrzej Kurek894edde2022-09-29 06:31:14 -04007771 return( NULL );
Jerry Yu392112c2022-02-17 14:34:10 +08007772}
7773#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007774
7775static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
7776{
7777 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007778#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007779 if( tls_prf == tls_prf_sha384 )
7780 {
7781 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
7782 }
7783 else
7784#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007785#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007786 if( tls_prf == tls_prf_sha256 )
7787 {
7788 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
7789 }
7790 else
7791#endif
7792 return( MBEDTLS_SSL_TLS_PRF_NONE );
7793}
7794
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007795/*
7796 * Populate a transform structure with session keys and all the other
7797 * necessary information.
7798 *
7799 * Parameters:
7800 * - [in/out]: transform: structure to populate
7801 * [in] must be just initialised with mbedtls_ssl_transform_init()
7802 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7803 * - [in] ciphersuite
7804 * - [in] master
7805 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007806 * - [in] tls_prf: pointer to PRF to use for key derivation
7807 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007808 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007809 * - [in] endpoint: client or server
7810 * - [in] ssl: used for:
7811 * - ssl->conf->{f,p}_export_keys
7812 * [in] optionally used for:
7813 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7814 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007815MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007816static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform,
7817 int ciphersuite,
7818 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007819#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007820 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007821#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007822 ssl_tls_prf_t tls_prf,
7823 const unsigned char randbytes[64],
Glenn Strauss07c64162022-03-14 12:34:51 -04007824 mbedtls_ssl_protocol_version tls_version,
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007825 unsigned endpoint,
7826 const mbedtls_ssl_context *ssl )
7827{
7828 int ret = 0;
7829 unsigned char keyblk[256];
7830 unsigned char *key1;
7831 unsigned char *key2;
7832 unsigned char *mac_enc;
7833 unsigned char *mac_dec;
7834 size_t mac_key_len = 0;
7835 size_t iv_copy_len;
7836 size_t keylen;
7837 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007838 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01007839#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007840 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007841 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01007842#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007843
7844#if defined(MBEDTLS_USE_PSA_CRYPTO)
7845 psa_key_type_t key_type;
7846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7847 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007848 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007849 size_t key_bits;
7850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7851#endif
7852
7853#if !defined(MBEDTLS_DEBUG_C) && \
7854 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7855 if( ssl->f_export_keys == NULL )
7856 {
7857 ssl = NULL; /* make sure we don't use it except for these cases */
7858 (void) ssl;
7859 }
7860#endif
7861
7862 /*
7863 * Some data just needs copying into the structure
7864 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007865#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007866 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007867#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007868 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007869
7870#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7871 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
7872#endif
7873
7874#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04007875 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007876 {
7877 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7878 * generation separate. This should never happen. */
7879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7880 }
7881#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7882
7883 /*
7884 * Get various info structures
7885 */
7886 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
7887 if( ciphersuite_info == NULL )
7888 {
7889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
7890 ciphersuite ) );
7891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7892 }
7893
Neil Armstrongab555e02022-04-04 11:07:59 +02007894 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007895#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007896 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007897#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007898 ciphersuite_info );
7899
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007900 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
7901 transform->taglen =
7902 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
7903
7904#if defined(MBEDTLS_USE_PSA_CRYPTO)
7905 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
7906 transform->taglen,
7907 &alg,
7908 &key_type,
7909 &key_bits ) ) != PSA_SUCCESS )
7910 {
7911 ret = psa_ssl_status_to_mbedtls( status );
7912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret );
7913 goto end;
7914 }
7915#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007916 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
7917 if( cipher_info == NULL )
7918 {
7919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
7920 ciphersuite_info->cipher ) );
7921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7922 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007923#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007924
Neil Armstronge4512952022-03-08 09:08:22 +01007925#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007926 mac_alg = mbedtls_hash_info_psa_from_md( ciphersuite_info->mac );
Neil Armstronge4512952022-03-08 09:08:22 +01007927 if( mac_alg == 0 )
7928 {
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02007929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_hash_info_psa_from_md for %u not found",
Neil Armstronge4512952022-03-08 09:08:22 +01007930 (unsigned) ciphersuite_info->mac ) );
7931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7932 }
7933#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007934 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
7935 if( md_info == NULL )
7936 {
7937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
7938 (unsigned) ciphersuite_info->mac ) );
7939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7940 }
Neil Armstronge4512952022-03-08 09:08:22 +01007941#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007942
7943#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7944 /* Copy own and peer's CID if the use of the CID
7945 * extension has been negotiated. */
7946 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
7947 {
7948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
7949
7950 transform->in_cid_len = ssl->own_cid_len;
7951 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
7952 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
7953 transform->in_cid_len );
7954
7955 transform->out_cid_len = ssl->handshake->peer_cid_len;
7956 memcpy( transform->out_cid, ssl->handshake->peer_cid,
7957 ssl->handshake->peer_cid_len );
7958 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
7959 transform->out_cid_len );
7960 }
7961#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
7962
7963 /*
7964 * Compute key block using the PRF
7965 */
7966 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
7967 if( ret != 0 )
7968 {
7969 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
7970 return( ret );
7971 }
7972
7973 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
7974 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
7975 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
7976 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
7977 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
7978
7979 /*
7980 * Determine the appropriate key, IV and MAC length.
7981 */
7982
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007983#if defined(MBEDTLS_USE_PSA_CRYPTO)
7984 keylen = PSA_BITS_TO_BYTES(key_bits);
7985#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007986 keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007987#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007988
7989#if defined(MBEDTLS_GCM_C) || \
7990 defined(MBEDTLS_CCM_C) || \
7991 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007992 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007993 {
7994 size_t explicit_ivlen;
7995
7996 transform->maclen = 0;
7997 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007998
7999 /* All modes haves 96-bit IVs, but the length of the static parts vary
8000 * with mode and version:
8001 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8002 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8003 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8004 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8005 * sequence number).
8006 */
8007 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008008
8009 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008010#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann3b2276a2022-10-06 14:49:08 +01008011 is_chachapoly = ( key_type == PSA_KEY_TYPE_CHACHA20 );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008012#else
David Horstmann3b2276a2022-10-06 14:49:08 +01008013 is_chachapoly = ( mbedtls_cipher_info_get_mode( cipher_info )
8014 == MBEDTLS_MODE_CHACHAPOLY );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008015#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008016
8017 if( is_chachapoly )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008018 transform->fixed_ivlen = 12;
8019 else
8020 transform->fixed_ivlen = 4;
8021
8022 /* Minimum length of encrypted record */
8023 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8024 transform->minlen = explicit_ivlen + transform->taglen;
8025 }
8026 else
8027#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8028#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008029 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
8030 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
8031 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008032 {
Neil Armstronge4512952022-03-08 09:08:22 +01008033#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongd1be7672022-04-04 11:21:41 +02008034 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008035#else
8036 size_t block_size = cipher_info->block_size;
8037#endif /* MBEDTLS_USE_PSA_CRYPTO */
8038
8039#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008040 /* Get MAC length */
8041 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8042#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008043 /* Initialize HMAC contexts */
8044 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
8045 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
8046 {
8047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8048 goto end;
8049 }
8050
8051 /* Get MAC length */
8052 mac_key_len = mbedtls_md_get_size( md_info );
Neil Armstronge4512952022-03-08 09:08:22 +01008053#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008054 transform->maclen = mac_key_len;
8055
8056 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008057#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong2230e6c2022-04-27 10:36:14 +02008058 transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg );
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008059#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008060 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008061#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008062
8063 /* Minimum length */
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008064 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008065 transform->minlen = transform->maclen;
8066 else
8067 {
8068 /*
8069 * GenericBlockCipher:
8070 * 1. if EtM is in use: one block plus MAC
8071 * otherwise: * first multiple of blocklen greater than maclen
8072 * 2. IV
8073 */
8074#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008075 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008076 {
8077 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008078 + block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008079 }
8080 else
8081#endif
8082 {
8083 transform->minlen = transform->maclen
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008084 + block_size
8085 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008086 }
8087
Glenn Strauss07c64162022-03-14 12:34:51 -04008088 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 )
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008089 {
8090 transform->minlen += transform->ivlen;
8091 }
8092 else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008093 {
8094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8095 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8096 goto end;
8097 }
8098 }
8099 }
8100 else
8101#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8102 {
8103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8105 }
8106
8107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8108 (unsigned) keylen,
8109 (unsigned) transform->minlen,
8110 (unsigned) transform->ivlen,
8111 (unsigned) transform->maclen ) );
8112
8113 /*
8114 * Finally setup the cipher contexts, IVs and MAC secrets.
8115 */
8116#if defined(MBEDTLS_SSL_CLI_C)
8117 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8118 {
8119 key1 = keyblk + mac_key_len * 2;
8120 key2 = keyblk + mac_key_len * 2 + keylen;
8121
8122 mac_enc = keyblk;
8123 mac_dec = keyblk + mac_key_len;
8124
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008125 iv_copy_len = ( transform->fixed_ivlen ) ?
8126 transform->fixed_ivlen : transform->ivlen;
8127 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
8128 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
8129 iv_copy_len );
8130 }
8131 else
8132#endif /* MBEDTLS_SSL_CLI_C */
8133#if defined(MBEDTLS_SSL_SRV_C)
8134 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8135 {
8136 key1 = keyblk + mac_key_len * 2 + keylen;
8137 key2 = keyblk + mac_key_len * 2;
8138
8139 mac_enc = keyblk + mac_key_len;
8140 mac_dec = keyblk;
8141
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008142 iv_copy_len = ( transform->fixed_ivlen ) ?
8143 transform->fixed_ivlen : transform->ivlen;
8144 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
8145 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
8146 iv_copy_len );
8147 }
8148 else
8149#endif /* MBEDTLS_SSL_SRV_C */
8150 {
8151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8152 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8153 goto end;
8154 }
8155
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008156 if( ssl != NULL && ssl->f_export_keys != NULL )
8157 {
8158 ssl->f_export_keys( ssl->p_export_keys,
8159 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8160 master, 48,
8161 randbytes + 32,
8162 randbytes,
8163 tls_prf_get_type( tls_prf ) );
8164 }
8165
8166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008167 transform->psa_alg = alg;
8168
8169 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
8170 {
8171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
8172 psa_set_key_algorithm( &attributes, alg );
8173 psa_set_key_type( &attributes, key_type );
8174
8175 if( ( status = psa_import_key( &attributes,
8176 key1,
8177 PSA_BITS_TO_BYTES( key_bits ),
8178 &transform->psa_key_enc ) ) != PSA_SUCCESS )
8179 {
8180 MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status );
8181 ret = psa_ssl_status_to_mbedtls( status );
8182 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8183 goto end;
8184 }
8185
8186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
8187
8188 if( ( status = psa_import_key( &attributes,
8189 key2,
8190 PSA_BITS_TO_BYTES( key_bits ),
8191 &transform->psa_key_dec ) ) != PSA_SUCCESS )
8192 {
8193 ret = psa_ssl_status_to_mbedtls( status );
8194 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret );
8195 goto end;
8196 }
8197 }
8198#else
8199 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
8200 cipher_info ) ) != 0 )
8201 {
8202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8203 goto end;
8204 }
8205
8206 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
8207 cipher_info ) ) != 0 )
8208 {
8209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
8210 goto end;
8211 }
8212
8213 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
8214 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8215 MBEDTLS_ENCRYPT ) ) != 0 )
8216 {
8217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8218 goto end;
8219 }
8220
8221 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
8222 (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ),
8223 MBEDTLS_DECRYPT ) ) != 0 )
8224 {
8225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
8226 goto end;
8227 }
8228
8229#if defined(MBEDTLS_CIPHER_MODE_CBC)
8230 if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC )
8231 {
8232 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
8233 MBEDTLS_PADDING_NONE ) ) != 0 )
8234 {
8235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8236 goto end;
8237 }
8238
8239 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
8240 MBEDTLS_PADDING_NONE ) ) != 0 )
8241 {
8242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
8243 goto end;
8244 }
8245 }
8246#endif /* MBEDTLS_CIPHER_MODE_CBC */
8247#endif /* MBEDTLS_USE_PSA_CRYPTO */
8248
Neil Armstrong29c0c042022-03-17 17:47:28 +01008249#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8250 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8251 For AEAD-based ciphersuites, there is nothing to do here. */
8252 if( mac_key_len != 0 )
8253 {
8254#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008255 transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008256
8257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstronge4512952022-03-08 09:08:22 +01008258 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) );
Neil Armstrong29c0c042022-03-17 17:47:28 +01008259 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
8260
8261 if( ( status = psa_import_key( &attributes,
8262 mac_enc, mac_key_len,
8263 &transform->psa_mac_enc ) ) != PSA_SUCCESS )
8264 {
8265 ret = psa_ssl_status_to_mbedtls( status );
8266 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8267 goto end;
8268 }
8269
Ronald Cronfb39f152022-03-25 14:36:28 +01008270 if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) ||
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008271 ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
8272#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
8273 && ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED )
8274#endif
8275 ) )
Neil Armstrong29c0c042022-03-17 17:47:28 +01008276 /* mbedtls_ct_hmac() requires the key to be exportable */
8277 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT |
8278 PSA_KEY_USAGE_VERIFY_HASH );
8279 else
8280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
8281
8282 if( ( status = psa_import_key( &attributes,
8283 mac_dec, mac_key_len,
8284 &transform->psa_mac_dec ) ) != PSA_SUCCESS )
8285 {
8286 ret = psa_ssl_status_to_mbedtls( status );
8287 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret );
8288 goto end;
8289 }
8290#else
8291 ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
8292 if( ret != 0 )
8293 goto end;
8294 ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
8295 if( ret != 0 )
8296 goto end;
8297#endif /* MBEDTLS_USE_PSA_CRYPTO */
8298 }
8299#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8300
8301 ((void) mac_dec);
8302 ((void) mac_enc);
8303
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008304end:
8305 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
8306 return( ret );
8307}
8308
Jerry Yuee40f9d2022-02-17 14:55:16 +08008309#if defined(MBEDTLS_USE_PSA_CRYPTO)
8310int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8311 unsigned char *hash, size_t *hashlen,
8312 unsigned char *data, size_t data_len,
8313 mbedtls_md_type_t md_alg )
8314{
8315 psa_status_t status;
8316 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008317 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yuee40f9d2022-02-17 14:55:16 +08008318
8319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
8320
8321 if( ( status = psa_hash_setup( &hash_operation,
8322 hash_alg ) ) != PSA_SUCCESS )
8323 {
8324 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
8325 goto exit;
8326 }
8327
8328 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
8329 64 ) ) != PSA_SUCCESS )
8330 {
8331 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8332 goto exit;
8333 }
8334
8335 if( ( status = psa_hash_update( &hash_operation,
8336 data, data_len ) ) != PSA_SUCCESS )
8337 {
8338 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
8339 goto exit;
8340 }
8341
8342 if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
8343 hashlen ) ) != PSA_SUCCESS )
8344 {
8345 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
8346 goto exit;
8347 }
8348
8349exit:
8350 if( status != PSA_SUCCESS )
8351 {
8352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8353 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8354 switch( status )
8355 {
8356 case PSA_ERROR_NOT_SUPPORTED:
8357 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
8358 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8359 case PSA_ERROR_BUFFER_TOO_SMALL:
8360 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
8361 case PSA_ERROR_INSUFFICIENT_MEMORY:
8362 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
8363 default:
8364 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
8365 }
8366 }
8367 return( 0 );
8368}
8369
8370#else
8371
8372int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
8373 unsigned char *hash, size_t *hashlen,
8374 unsigned char *data, size_t data_len,
8375 mbedtls_md_type_t md_alg )
8376{
8377 int ret = 0;
8378 mbedtls_md_context_t ctx;
8379 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
8380 *hashlen = mbedtls_md_get_size( md_info );
8381
8382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
8383
8384 mbedtls_md_init( &ctx );
8385
8386 /*
8387 * digitally-signed struct {
8388 * opaque client_random[32];
8389 * opaque server_random[32];
8390 * ServerDHParams params;
8391 * };
8392 */
8393 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8394 {
8395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8396 goto exit;
8397 }
8398 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
8399 {
8400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
8401 goto exit;
8402 }
8403 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
8404 {
8405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8406 goto exit;
8407 }
8408 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
8409 {
8410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
8411 goto exit;
8412 }
8413 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
8414 {
8415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
8416 goto exit;
8417 }
8418
8419exit:
8420 mbedtls_md_free( &ctx );
8421
8422 if( ret != 0 )
8423 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8424 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8425
8426 return( ret );
8427}
8428#endif /* MBEDTLS_USE_PSA_CRYPTO */
8429
Jerry Yud9d91da2022-02-17 14:57:06 +08008430#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8431
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008432/* Find the preferred hash for a given signature algorithm. */
8433unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
8434 mbedtls_ssl_context *ssl,
8435 unsigned int sig_alg )
Jerry Yud9d91da2022-02-17 14:57:06 +08008436{
Gabor Mezei078e8032022-04-27 21:17:56 +02008437 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008438 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008439
8440 if( sig_alg == MBEDTLS_SSL_SIG_ANON )
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008441 return( MBEDTLS_SSL_HASH_NONE );
Gabor Mezei078e8032022-04-27 21:17:56 +02008442
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008443 for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ )
Jerry Yud9d91da2022-02-17 14:57:06 +08008444 {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008445 unsigned int hash_alg_received =
8446 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8447 received_sig_algs[i] );
8448 unsigned int sig_alg_received =
8449 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8450 received_sig_algs[i] );
8451
8452 if( sig_alg == sig_alg_received )
8453 {
8454#if defined(MBEDTLS_USE_PSA_CRYPTO)
8455 if( ssl->handshake->key_cert && ssl->handshake->key_cert->key )
8456 {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008457 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02008458 mbedtls_hash_info_psa_from_md( hash_alg_received );
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008459
Neil Armstrong96eceb82022-06-30 18:05:05 +02008460 if( sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8461 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8462 PSA_ALG_ECDSA( psa_hash_alg ),
8463 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008464 continue;
8465
Neil Armstrong96eceb82022-06-30 18:05:05 +02008466 if( sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
Neil Armstrong971f30d2022-07-01 16:23:50 +02008467 ! mbedtls_pk_can_do_ext( ssl->handshake->key_cert->key,
8468 PSA_ALG_RSA_PKCS1V15_SIGN(
8469 psa_hash_alg ),
8470 PSA_KEY_USAGE_SIGN_HASH ) )
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008471 continue;
8472 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008473#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008474
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008475 return( hash_alg_received );
8476 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008477 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008478
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008479 return( MBEDTLS_SSL_HASH_NONE );
Jerry Yud9d91da2022-02-17 14:57:06 +08008480}
8481
8482#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8483
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008484/* Serialization of TLS 1.2 sessions:
8485 *
8486 * struct {
8487 * uint64 start_time;
8488 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008489 * uint8 session_id_len; // at most 32
8490 * opaque session_id[32];
8491 * opaque master[48]; // fixed length in the standard
8492 * uint32 verify_result;
8493 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8494 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8495 * uint32 ticket_lifetime;
8496 * uint8 mfl_code; // up to 255 according to standard
8497 * uint8 encrypt_then_mac; // 0 or 1
8498 * } serialized_session_tls12;
8499 *
8500 */
Jerry Yu438ddd82022-07-07 06:55:50 +00008501static size_t ssl_tls12_session_save( const mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008502 unsigned char *buf,
8503 size_t buf_len )
8504{
8505 unsigned char *p = buf;
8506 size_t used = 0;
8507
8508#if defined(MBEDTLS_HAVE_TIME)
8509 uint64_t start;
8510#endif
8511#if defined(MBEDTLS_X509_CRT_PARSE_C)
8512#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8513 size_t cert_len;
8514#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8515#endif /* MBEDTLS_X509_CRT_PARSE_C */
8516
8517 /*
8518 * Time
8519 */
8520#if defined(MBEDTLS_HAVE_TIME)
8521 used += 8;
8522
8523 if( used <= buf_len )
8524 {
8525 start = (uint64_t) session->start;
8526
8527 MBEDTLS_PUT_UINT64_BE( start, p, 0 );
8528 p += 8;
8529 }
8530#endif /* MBEDTLS_HAVE_TIME */
8531
8532 /*
8533 * Basic mandatory fields
8534 */
8535 used += 2 /* ciphersuite */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008536 + 1 /* id_len */
8537 + sizeof( session->id )
8538 + sizeof( session->master )
8539 + 4; /* verify_result */
8540
8541 if( used <= buf_len )
8542 {
8543 MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
8544 p += 2;
8545
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008546 *p++ = MBEDTLS_BYTE_0( session->id_len );
8547 memcpy( p, session->id, 32 );
8548 p += 32;
8549
8550 memcpy( p, session->master, 48 );
8551 p += 48;
8552
8553 MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
8554 p += 4;
8555 }
8556
8557 /*
8558 * Peer's end-entity certificate
8559 */
8560#if defined(MBEDTLS_X509_CRT_PARSE_C)
8561#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8562 if( session->peer_cert == NULL )
8563 cert_len = 0;
8564 else
8565 cert_len = session->peer_cert->raw.len;
8566
8567 used += 3 + cert_len;
8568
8569 if( used <= buf_len )
8570 {
8571 *p++ = MBEDTLS_BYTE_2( cert_len );
8572 *p++ = MBEDTLS_BYTE_1( cert_len );
8573 *p++ = MBEDTLS_BYTE_0( cert_len );
8574
8575 if( session->peer_cert != NULL )
8576 {
8577 memcpy( p, session->peer_cert->raw.p, cert_len );
8578 p += cert_len;
8579 }
8580 }
8581#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8582 if( session->peer_cert_digest != NULL )
8583 {
8584 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
8585 if( used <= buf_len )
8586 {
8587 *p++ = (unsigned char) session->peer_cert_digest_type;
8588 *p++ = (unsigned char) session->peer_cert_digest_len;
8589 memcpy( p, session->peer_cert_digest,
8590 session->peer_cert_digest_len );
8591 p += session->peer_cert_digest_len;
8592 }
8593 }
8594 else
8595 {
8596 used += 2;
8597 if( used <= buf_len )
8598 {
8599 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8600 *p++ = 0;
8601 }
8602 }
8603#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8604#endif /* MBEDTLS_X509_CRT_PARSE_C */
8605
8606 /*
8607 * Session ticket if any, plus associated data
8608 */
8609#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8610 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8611
8612 if( used <= buf_len )
8613 {
8614 *p++ = MBEDTLS_BYTE_2( session->ticket_len );
8615 *p++ = MBEDTLS_BYTE_1( session->ticket_len );
8616 *p++ = MBEDTLS_BYTE_0( session->ticket_len );
8617
8618 if( session->ticket != NULL )
8619 {
8620 memcpy( p, session->ticket, session->ticket_len );
8621 p += session->ticket_len;
8622 }
8623
8624 MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
8625 p += 4;
8626 }
8627#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8628
8629 /*
8630 * Misc extension-related info
8631 */
8632#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8633 used += 1;
8634
8635 if( used <= buf_len )
8636 *p++ = session->mfl_code;
8637#endif
8638
8639#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8640 used += 1;
8641
8642 if( used <= buf_len )
8643 *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
8644#endif
8645
8646 return( used );
8647}
8648
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008649MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu438ddd82022-07-07 06:55:50 +00008650static int ssl_tls12_session_load( mbedtls_ssl_session *session,
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008651 const unsigned char *buf,
8652 size_t len )
8653{
8654#if defined(MBEDTLS_HAVE_TIME)
8655 uint64_t start;
8656#endif
8657#if defined(MBEDTLS_X509_CRT_PARSE_C)
8658#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8659 size_t cert_len;
8660#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8661#endif /* MBEDTLS_X509_CRT_PARSE_C */
8662
8663 const unsigned char *p = buf;
8664 const unsigned char * const end = buf + len;
8665
8666 /*
8667 * Time
8668 */
8669#if defined(MBEDTLS_HAVE_TIME)
8670 if( 8 > (size_t)( end - p ) )
8671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8672
8673 start = ( (uint64_t) p[0] << 56 ) |
8674 ( (uint64_t) p[1] << 48 ) |
8675 ( (uint64_t) p[2] << 40 ) |
8676 ( (uint64_t) p[3] << 32 ) |
8677 ( (uint64_t) p[4] << 24 ) |
8678 ( (uint64_t) p[5] << 16 ) |
8679 ( (uint64_t) p[6] << 8 ) |
8680 ( (uint64_t) p[7] );
8681 p += 8;
8682
8683 session->start = (time_t) start;
8684#endif /* MBEDTLS_HAVE_TIME */
8685
8686 /*
8687 * Basic mandatory fields
8688 */
Thomas Daubney20f89a92022-06-20 15:12:19 +01008689 if( 2 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8691
8692 session->ciphersuite = ( p[0] << 8 ) | p[1];
8693 p += 2;
8694
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008695 session->id_len = *p++;
8696 memcpy( session->id, p, 32 );
8697 p += 32;
8698
8699 memcpy( session->master, p, 48 );
8700 p += 48;
8701
8702 session->verify_result = ( (uint32_t) p[0] << 24 ) |
8703 ( (uint32_t) p[1] << 16 ) |
8704 ( (uint32_t) p[2] << 8 ) |
8705 ( (uint32_t) p[3] );
8706 p += 4;
8707
8708 /* Immediately clear invalid pointer values that have been read, in case
8709 * we exit early before we replaced them with valid ones. */
8710#if defined(MBEDTLS_X509_CRT_PARSE_C)
8711#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8712 session->peer_cert = NULL;
8713#else
8714 session->peer_cert_digest = NULL;
8715#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8716#endif /* MBEDTLS_X509_CRT_PARSE_C */
8717#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8718 session->ticket = NULL;
8719#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8720
8721 /*
8722 * Peer certificate
8723 */
8724#if defined(MBEDTLS_X509_CRT_PARSE_C)
8725#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8726 /* Deserialize CRT from the end of the ticket. */
8727 if( 3 > (size_t)( end - p ) )
8728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8729
8730 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8731 p += 3;
8732
8733 if( cert_len != 0 )
8734 {
8735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8736
8737 if( cert_len > (size_t)( end - p ) )
8738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8739
8740 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
8741
8742 if( session->peer_cert == NULL )
8743 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8744
8745 mbedtls_x509_crt_init( session->peer_cert );
8746
8747 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
8748 p, cert_len ) ) != 0 )
8749 {
8750 mbedtls_x509_crt_free( session->peer_cert );
8751 mbedtls_free( session->peer_cert );
8752 session->peer_cert = NULL;
8753 return( ret );
8754 }
8755
8756 p += cert_len;
8757 }
8758#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8759 /* Deserialize CRT digest from the end of the ticket. */
8760 if( 2 > (size_t)( end - p ) )
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8762
8763 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8764 session->peer_cert_digest_len = (size_t) *p++;
8765
8766 if( session->peer_cert_digest_len != 0 )
8767 {
8768 const mbedtls_md_info_t *md_info =
8769 mbedtls_md_info_from_type( session->peer_cert_digest_type );
8770 if( md_info == NULL )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8772 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
8773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8774
8775 if( session->peer_cert_digest_len > (size_t)( end - p ) )
8776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8777
8778 session->peer_cert_digest =
8779 mbedtls_calloc( 1, session->peer_cert_digest_len );
8780 if( session->peer_cert_digest == NULL )
8781 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8782
8783 memcpy( session->peer_cert_digest, p,
8784 session->peer_cert_digest_len );
8785 p += session->peer_cert_digest_len;
8786 }
8787#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8788#endif /* MBEDTLS_X509_CRT_PARSE_C */
8789
8790 /*
8791 * Session ticket and associated data
8792 */
8793#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8794 if( 3 > (size_t)( end - p ) )
8795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8796
8797 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
8798 p += 3;
8799
8800 if( session->ticket_len != 0 )
8801 {
8802 if( session->ticket_len > (size_t)( end - p ) )
8803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8804
8805 session->ticket = mbedtls_calloc( 1, session->ticket_len );
8806 if( session->ticket == NULL )
8807 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8808
8809 memcpy( session->ticket, p, session->ticket_len );
8810 p += session->ticket_len;
8811 }
8812
8813 if( 4 > (size_t)( end - p ) )
8814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8815
8816 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
8817 ( (uint32_t) p[1] << 16 ) |
8818 ( (uint32_t) p[2] << 8 ) |
8819 ( (uint32_t) p[3] );
8820 p += 4;
8821#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8822
8823 /*
8824 * Misc extension-related info
8825 */
8826#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8827 if( 1 > (size_t)( end - p ) )
8828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8829
8830 session->mfl_code = *p++;
8831#endif
8832
8833#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8834 if( 1 > (size_t)( end - p ) )
8835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8836
8837 session->encrypt_then_mac = *p++;
8838#endif
8839
8840 /* Done, should have consumed entire buffer */
8841 if( p != end )
8842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8843
8844 return( 0 );
8845}
Jerry Yudc7bd172022-02-17 13:44:15 +08008846#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8847
XiaokangQian75d40ef2022-04-20 11:05:24 +00008848int mbedtls_ssl_validate_ciphersuite(
8849 const mbedtls_ssl_context *ssl,
8850 const mbedtls_ssl_ciphersuite_t *suite_info,
8851 mbedtls_ssl_protocol_version min_tls_version,
8852 mbedtls_ssl_protocol_version max_tls_version )
8853{
8854 (void) ssl;
8855
8856 if( suite_info == NULL )
8857 return( -1 );
8858
8859 if( ( suite_info->min_tls_version > max_tls_version ) ||
8860 ( suite_info->max_tls_version < min_tls_version ) )
8861 {
8862 return( -1 );
8863 }
8864
XiaokangQian060d8672022-04-21 09:24:56 +00008865#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008866#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
8867 if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
8868 mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
8869 {
8870 return( -1 );
8871 }
8872#endif
8873
8874 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8875#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
8876 if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
8877 mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 )
8878 {
8879 return( -1 );
8880 }
8881#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8882#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8883
8884 return( 0 );
8885}
8886
Ronald Crone68ab4f2022-10-05 12:46:29 +02008887#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008888/*
8889 * Function for writing a signature algorithm extension.
8890 *
8891 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8892 * value (TLS 1.3 RFC8446):
8893 * enum {
8894 * ....
8895 * ecdsa_secp256r1_sha256( 0x0403 ),
8896 * ecdsa_secp384r1_sha384( 0x0503 ),
8897 * ecdsa_secp521r1_sha512( 0x0603 ),
8898 * ....
8899 * } SignatureScheme;
8900 *
8901 * struct {
8902 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8903 * } SignatureSchemeList;
8904 *
8905 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8906 * value (TLS 1.2 RFC5246):
8907 * enum {
8908 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8909 * sha512(6), (255)
8910 * } HashAlgorithm;
8911 *
8912 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8913 * SignatureAlgorithm;
8914 *
8915 * struct {
8916 * HashAlgorithm hash;
8917 * SignatureAlgorithm signature;
8918 * } SignatureAndHashAlgorithm;
8919 *
8920 * SignatureAndHashAlgorithm
8921 * supported_signature_algorithms<2..2^16-2>;
8922 *
8923 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8924 * generalization of the TLS 1.2 signature algorithm extension.
8925 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8926 * `SignatureScheme` field of TLS 1.3
8927 *
8928 */
8929int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf,
8930 const unsigned char *end, size_t *out_len )
8931{
8932 unsigned char *p = buf;
8933 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8934 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8935
8936 *out_len = 0;
8937
8938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
8939
8940 /* Check if we have space for header and length field:
8941 * - extension_type (2 bytes)
8942 * - extension_data_length (2 bytes)
8943 * - supported_signature_algorithms_length (2 bytes)
8944 */
8945 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
8946 p += 6;
8947
8948 /*
8949 * Write supported_signature_algorithms
8950 */
8951 supported_sig_alg = p;
8952 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
8953 if( sig_alg == NULL )
8954 return( MBEDTLS_ERR_SSL_BAD_CONFIG );
8955
8956 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
8957 {
Jerry Yu53f5c152022-06-22 20:24:38 +08008958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got signature scheme [%x] %s",
8959 *sig_alg,
8960 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008961 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) )
8962 continue;
8963 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
8964 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
8965 p += 2;
Jerry Yu80dd5db2022-06-22 19:30:32 +08008966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "sent signature scheme [%x] %s",
Jerry Yuf3b46b52022-06-19 16:52:27 +08008967 *sig_alg,
8968 mbedtls_ssl_sig_alg_to_str( *sig_alg ) ) );
XiaokangQianeaf36512022-04-24 09:07:44 +00008969 }
8970
8971 /* Length of supported_signature_algorithms */
8972 supported_sig_alg_len = p - supported_sig_alg;
8973 if( supported_sig_alg_len == 0 )
8974 {
8975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
8976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
8977 }
8978
XiaokangQianeaf36512022-04-24 09:07:44 +00008979 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008980 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
XiaokangQianeaf36512022-04-24 09:07:44 +00008981 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
8982
XiaokangQianeaf36512022-04-24 09:07:44 +00008983 *out_len = p - buf;
8984
8985#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yuc4bf5d62022-10-29 09:08:47 +08008986 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_SIG_ALG );
XiaokangQianeaf36512022-04-24 09:07:44 +00008987#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008988
XiaokangQianeaf36512022-04-24 09:07:44 +00008989 return( 0 );
8990}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008991#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008992
XiaokangQian40a35232022-05-07 09:02:40 +00008993#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008994/*
8995 * mbedtls_ssl_parse_server_name_ext
8996 *
8997 * Structure of server_name extension:
8998 *
8999 * enum {
9000 * host_name(0), (255)
9001 * } NameType;
9002 * opaque HostName<1..2^16-1>;
9003 *
9004 * struct {
9005 * NameType name_type;
9006 * select (name_type) {
9007 * case host_name: HostName;
9008 * } name;
9009 * } ServerName;
9010 * struct {
9011 * ServerName server_name_list<1..2^16-1>
9012 * } ServerNameList;
9013 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009014MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian9b2b7712022-05-17 02:57:00 +00009015int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl,
9016 const unsigned char *buf,
9017 const unsigned char *end )
XiaokangQian40a35232022-05-07 09:02:40 +00009018{
9019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9020 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009021 size_t server_name_list_len, hostname_len;
9022 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009023
XiaokangQianf2a94202022-05-20 06:44:24 +00009024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
XiaokangQian40a35232022-05-07 09:02:40 +00009025
9026 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009027 server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009028 p += 2;
9029
XiaokangQian9b2b7712022-05-17 02:57:00 +00009030 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len );
9031 server_name_list_end = p + server_name_list_len;
XiaokangQian75fe8c72022-06-15 09:42:45 +00009032 while( p < server_name_list_end )
XiaokangQian40a35232022-05-07 09:02:40 +00009033 {
XiaokangQian9b2b7712022-05-17 02:57:00 +00009034 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009035 hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 );
XiaokangQian9b2b7712022-05-17 02:57:00 +00009036 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end,
9037 hostname_len + 3 );
XiaokangQian40a35232022-05-07 09:02:40 +00009038
9039 if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME )
9040 {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009041 /* sni_name is intended to be used only during the parsing of the
9042 * ClientHello message (it is reset to NULL before the end of
9043 * the message parsing). Thus it is ok to just point to the
9044 * reception buffer and not make a copy of it.
9045 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009046 ssl->handshake->sni_name = p + 3;
9047 ssl->handshake->sni_name_len = hostname_len;
9048 if( ssl->conf->f_sni == NULL )
9049 return( 0 );
XiaokangQian40a35232022-05-07 09:02:40 +00009050 ret = ssl->conf->f_sni( ssl->conf->p_sni,
XiaokangQian9b2b7712022-05-17 02:57:00 +00009051 ssl, p + 3, hostname_len );
XiaokangQian40a35232022-05-07 09:02:40 +00009052 if( ret != 0 )
9053 {
XiaokangQianf2a94202022-05-20 06:44:24 +00009054 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
XiaokangQian129aeb92022-06-02 09:29:18 +00009055 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9056 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
XiaokangQian40a35232022-05-07 09:02:40 +00009057 return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME );
9058 }
9059 return( 0 );
9060 }
9061
9062 p += hostname_len + 3;
9063 }
9064
9065 return( 0 );
9066}
9067#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9068
XiaokangQianacb39922022-06-17 10:18:48 +00009069#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009070MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianacb39922022-06-17 10:18:48 +00009071int mbedtls_ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
9072 const unsigned char *buf,
9073 const unsigned char *end )
9074{
9075 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009076 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009077 const unsigned char *protocol_name_list;
9078 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009079 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009080
9081 /* If ALPN not configured, just ignore the extension */
9082 if( ssl->conf->alpn_list == NULL )
9083 return( 0 );
9084
9085 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009086 * RFC7301, section 3.1
9087 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009088 *
XiaokangQianc7403452022-06-23 03:24:12 +00009089 * struct {
9090 * ProtocolName protocol_name_list<2..2^16-1>
9091 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009092 */
9093
XiaokangQianc7403452022-06-23 03:24:12 +00009094 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009095 * protocol_name_list_len 2 bytes
9096 * protocol_name_len 1 bytes
9097 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009098 */
XiaokangQianacb39922022-06-17 10:18:48 +00009099 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
9100
XiaokangQianc7403452022-06-23 03:24:12 +00009101 protocol_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 );
XiaokangQianacb39922022-06-17 10:18:48 +00009102 p += 2;
XiaokangQianc7403452022-06-23 03:24:12 +00009103 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, protocol_name_list_len );
XiaokangQian95d5f542022-06-24 02:29:26 +00009104 protocol_name_list = p;
9105 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009106
9107 /* Validate peer's list (lengths) */
XiaokangQian95d5f542022-06-24 02:29:26 +00009108 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009109 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009110 protocol_name_len = *p++;
9111 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, protocol_name_list_end,
9112 protocol_name_len );
XiaokangQianc7403452022-06-23 03:24:12 +00009113 if( protocol_name_len == 0 )
XiaokangQian95d5f542022-06-24 02:29:26 +00009114 {
9115 MBEDTLS_SSL_PEND_FATAL_ALERT(
9116 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
9117 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQianacb39922022-06-17 10:18:48 +00009118 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
XiaokangQian95d5f542022-06-24 02:29:26 +00009119 }
9120
9121 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009122 }
9123
9124 /* Use our order of preference */
9125 for( const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++ )
9126 {
9127 size_t const alpn_len = strlen( *alpn );
XiaokangQian95d5f542022-06-24 02:29:26 +00009128 p = protocol_name_list;
9129 while( p < protocol_name_list_end )
XiaokangQianacb39922022-06-17 10:18:48 +00009130 {
XiaokangQian95d5f542022-06-24 02:29:26 +00009131 protocol_name_len = *p++;
XiaokangQianc7403452022-06-23 03:24:12 +00009132 if( protocol_name_len == alpn_len &&
XiaokangQian95d5f542022-06-24 02:29:26 +00009133 memcmp( p, *alpn, alpn_len ) == 0 )
XiaokangQianacb39922022-06-17 10:18:48 +00009134 {
9135 ssl->alpn_chosen = *alpn;
9136 return( 0 );
9137 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009138
9139 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009140 }
9141 }
9142
XiaokangQian95d5f542022-06-24 02:29:26 +00009143 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009144 MBEDTLS_SSL_PEND_FATAL_ALERT(
9145 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9146 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9147 return( MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL );
9148}
9149
9150int mbedtls_ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
9151 unsigned char *buf,
9152 unsigned char *end,
XiaokangQianc7403452022-06-23 03:24:12 +00009153 size_t *out_len )
XiaokangQianacb39922022-06-17 10:18:48 +00009154{
9155 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009156 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009157 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009158
9159 if( ssl->alpn_chosen == NULL )
9160 {
9161 return( 0 );
9162 }
9163
XiaokangQian95d5f542022-06-24 02:29:26 +00009164 protocol_name_len = strlen( ssl->alpn_chosen );
9165 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 + protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009166
9167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "server side, adding alpn extension" ) );
9168 /*
9169 * 0 . 1 ext identifier
9170 * 2 . 3 ext length
9171 * 4 . 5 protocol list length
9172 * 6 . 6 protocol name length
9173 * 7 . 7+n protocol name
9174 */
9175 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
9176
XiaokangQian95d5f542022-06-24 02:29:26 +00009177 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009178
XiaokangQian95d5f542022-06-24 02:29:26 +00009179 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 3, p, 2 );
9180 MBEDTLS_PUT_UINT16_BE( protocol_name_len + 1, p, 4 );
XiaokangQian0b776e22022-06-24 09:04:59 +00009181 /* Note: the length of the chosen protocol has been checked to be less
9182 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9183 */
XiaokangQian95d5f542022-06-24 02:29:26 +00009184 p[6] = MBEDTLS_BYTE_0( protocol_name_len );
XiaokangQianacb39922022-06-17 10:18:48 +00009185
XiaokangQian95d5f542022-06-24 02:29:26 +00009186 memcpy( p + 7, ssl->alpn_chosen, protocol_name_len );
Jerry Yub95dd362022-11-08 21:19:34 +08009187
9188#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
9189 mbedtls_ssl_tls13_set_hs_sent_ext_mask( ssl, MBEDTLS_TLS_EXT_ALPN );
9190#endif
9191
XiaokangQianacb39922022-06-17 10:18:48 +00009192 return ( 0 );
9193}
9194#endif /* MBEDTLS_SSL_ALPN */
9195
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009196#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009197 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009198 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009199 defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009200int mbedtls_ssl_session_set_hostname( mbedtls_ssl_session *session,
9201 const char *hostname )
9202{
9203 /* Initialize to suppress unnecessary compiler warning */
9204 size_t hostname_len = 0;
9205
9206 /* Check if new hostname is valid before
9207 * making any change to current one */
9208 if( hostname != NULL )
9209 {
9210 hostname_len = strlen( hostname );
9211
9212 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9214 }
9215
9216 /* Now it's clear that we will overwrite the old hostname,
9217 * so we can free it safely */
9218 if( session->hostname != NULL )
9219 {
9220 mbedtls_platform_zeroize( session->hostname,
9221 strlen( session->hostname ) );
9222 mbedtls_free( session->hostname );
9223 }
9224
9225 /* Passing NULL as hostname shall clear the old one */
9226 if( hostname == NULL )
9227 {
9228 session->hostname = NULL;
9229 }
9230 else
9231 {
9232 session->hostname = mbedtls_calloc( 1, hostname_len + 1 );
9233 if( session->hostname == NULL )
9234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9235
9236 memcpy( session->hostname, hostname, hostname_len );
9237 }
9238
9239 return( 0 );
9240}
Xiaokang Qian03409292022-10-12 02:49:52 +00009241#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9242 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009243 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009244 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#endif /* MBEDTLS_SSL_TLS_C */